]> git.vpit.fr Git - perl/modules/re-engine-Plugin.git/blobdiff - Plugin.pm
This is 0.12
[perl/modules/re-engine-Plugin.git] / Plugin.pm
index d8fa3228907fb93a2913975be35c7a4769e8a26d..eee5dadd5c24d679a3166432b47106450ce4015c 100644 (file)
--- a/Plugin.pm
+++ b/Plugin.pm
@@ -1,88 +1,78 @@
-# See Plugin.pod for documentation\r
-package re::engine::Plugin;\r
-use 5.009005;\r
-use strict;\r
-use XSLoader ();\r
-\r
-our $VERSION = '0.04';\r
-\r
-# All engines should subclass the core Regexp package\r
-our @ISA = 'Regexp';\r
-\r
-XSLoader::load __PACKAGE__, $VERSION;\r
-\r
-my $RE_ENGINE_PLUGIN = ENGINE();\r
-\r
-# How many? Used to cheat %^H\r
-my $callback = 1;\r
-\r
-# Where we store our CODE refs\r
-my %callback;\r
-\r
-# Generate a key to use in the %^H hash from a string, prefix the\r
-# package name like L<pragma> does\r
-my $key = sub { __PACKAGE__ . "::" . $_[0] };\r
-\r
-sub import\r
-{\r
-    my ($pkg, %sub) = @_;\r
-\r
-    # Valid callbacks\r
-    my @callback = qw(comp exec);\r
-\r
-    for (@callback) {\r
-        next unless exists $sub{$_};\r
-        my $cb = delete $sub{$_};\r
-\r
-        unless (ref $cb eq 'CODE') {\r
-            require Carp;\r
-            Carp::croak("'$_' is not CODE");\r
-        }\r
-\r
-        # Get an ID to use\r
-        my $id = $callback ++;\r
-\r
-        # Insert into our callback storage,\r
-        $callback{$_}->{$id} = $cb;\r
-\r
-        # Instert into our cache with a key we can retrive later\r
-        # knowing the ID in %^H and what callback we're getting\r
-        $^H{ $key->($_) } = $id;\r
-    }\r
-\r
-    $^H{regcomp} = $RE_ENGINE_PLUGIN;\r
-}\r
-\r
-sub unimport\r
-{\r
-    # Delete the regcomp hook\r
-    delete $^H{regcomp}\r
-        if $^H{regcomp} == $RE_ENGINE_PLUGIN;\r
-}\r
-\r
-# Minimal function to get CODE for a given key to be called by the\r
-# get_H_callback C function.\r
-sub _get_callback\r
-{\r
-    my ($name) = @_; # 'comp', 'exec', ...\r
-\r
-    my $h = (caller(0))[10];\r
-    my $id = $h->{ $key->($name) };\r
-\r
-    my $cb = defined $id ? $callback{$name}->{$id} : 0;\r
-\r
-    return $cb;\r
-}\r
-\r
-sub num_captures\r
-{\r
-    my ($re, %callback) = @_;\r
-\r
-    for my $key (keys %callback) {\r
-        $key =~ y/a-z/A-Z/; # ASCII uc\r
-        my $name = '_num_capture_buff_' . $key;\r
-        $re->$name( $callback{$key} );\r
-    }\r
-}\r
-\r
-1;\r
+# See Plugin.pod for documentation
+package re::engine::Plugin;
+use 5.010;
+use strict;
+
+our ($VERSION, @ISA);
+
+BEGIN {
+ $VERSION = '0.12';
+ # All engines should subclass the core Regexp package
+ @ISA = 'Regexp';
+ require XSLoader;
+ XSLoader::load(__PACKAGE__, $VERSION);
+}
+
+my $RE_ENGINE_PLUGIN = ENGINE();
+
+sub import
+{
+    my ($pkg, %sub) = @_;
+
+    # Valid callbacks
+    my @callback = qw<comp exec free>;
+
+    for (@callback) {
+        next unless exists $sub{$_};
+        my $cb = $sub{$_};
+
+        unless (ref $cb eq 'CODE') {
+            require Carp;
+            Carp::croak("'$_' is not CODE");
+        }
+    }
+
+    $^H |= 0x020000;
+
+    $^H{+(__PACKAGE__)} = _tag(@sub{@callback});
+    $^H{regcomp}        = $RE_ENGINE_PLUGIN;
+
+    return;
+}
+
+sub unimport
+{
+    # Delete the regcomp hook
+    delete $^H{regcomp}
+        if $^H{regcomp} == $RE_ENGINE_PLUGIN;
+
+    delete $^H{+(__PACKAGE__)};
+
+    return;
+}
+
+sub callbacks
+{
+    my ($re, %callback) = @_;
+
+    my %map = map { $_ => "_$_" } qw<exec free>;
+
+    for my $key (keys %callback) {
+        my $name = $map{$key};
+        next unless defined $name;
+        $re->$name($callback{$key});
+    }
+}
+
+sub num_captures
+{
+    my ($re, %callback) = @_;
+
+    for my $key (keys %callback) {
+        $key =~ y/a-z/A-Z/; # ASCII uc
+        my $name = '_num_capture_buff_' . $key;
+        $re->$name( $callback{$key} );
+    }
+}
+
+1;