summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-06-23 17:08:54 -0500
committerJesse Luehrs <doy@tozt.net>2012-06-23 17:08:54 -0500
commitefb4439ddf61def3dda4edd4af4b9c237e88d20f (patch)
treeccc1ff46077201b99ea94dda45cd20ac885fe514 /lib
parent28a8bed6e1effb76068f9d83d912669a20269bcd (diff)
downloadsmartmatch-efb4439ddf61def3dda4edd4af4b9c237e88d20f.tar.gz
smartmatch-efb4439ddf61def3dda4edd4af4b9c237e88d20f.zip
use real function calls instead of anon subs
Diffstat (limited to 'lib')
-rw-r--r--lib/smartmatch.pm24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/smartmatch.pm b/lib/smartmatch.pm
index fb6e038..0524799 100644
--- a/lib/smartmatch.pm
+++ b/lib/smartmatch.pm
@@ -6,6 +6,8 @@ use 5.010;
use parent 'DynaLoader';
use B::Hooks::OP::Check;
+use Module::Runtime 'use_package_optimistically';
+use Package::Stash;
sub dl_load_flags { 0x01 }
@@ -51,18 +53,28 @@ the core perl smart matching behavior.
=cut
+my $anon = 1;
+
sub import {
my $package = shift;
my ($cb) = @_;
- if (!ref($cb)) {
- my $engine = "smartmatch::engine::$cb";
- eval "require $engine; 1"
- or die "Couldn't load smartmatch engine $engine: $@";
- $cb = $engine->can('match') unless ref($cb);
+ my $engine;
+
+ if (ref($cb)) {
+ $engine = 'smartmatch::engine::__ANON__::' . $anon;
+ my $anon_stash = Package::Stash->new($engine);
+ $anon_stash->add_symbol('&match' => $cb);
+ $anon++;
+ }
+ else {
+ $engine = "smartmatch::engine::$cb";
+ use_package_optimistically($engine);
+ die "$engine does not implement a 'match' function"
+ unless $engine->can('match');
}
- register($cb);
+ register($engine);
}
sub unimport {