-# See Plugin.pod for documentation\r
-package re::engine::Plugin;\r
-use 5.009005;\r
-use base 'Regexp';\r
-use strict;\r
-use XSLoader ();\r
-\r
-our $VERSION = '0.03';\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 intuit checkstr free dupe);\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;