1 # See Plugin.pod for documentation
\r
2 package re::engine::Plugin;
\r
7 our $VERSION = '0.04';
\r
9 # All engines should subclass the core Regexp package
\r
10 our @ISA = 'Regexp';
\r
12 XSLoader::load __PACKAGE__, $VERSION;
\r
14 my $RE_ENGINE_PLUGIN = ENGINE();
\r
16 # How many? Used to cheat %^H
\r
19 # Where we store our CODE refs
\r
22 # Generate a key to use in the %^H hash from a string, prefix the
\r
23 # package name like L<pragma> does
\r
24 my $key = sub { __PACKAGE__ . "::" . $_[0] };
\r
28 my ($pkg, %sub) = @_;
\r
31 my @callback = qw(comp exec);
\r
34 next unless exists $sub{$_};
\r
35 my $cb = delete $sub{$_};
\r
37 unless (ref $cb eq 'CODE') {
\r
39 Carp::croak("'$_' is not CODE");
\r
43 my $id = $callback ++;
\r
45 # Insert into our callback storage,
\r
46 $callback{$_}->{$id} = $cb;
\r
48 # Instert into our cache with a key we can retrive later
\r
49 # knowing the ID in %^H and what callback we're getting
\r
50 $^H{ $key->($_) } = $id;
\r
53 $^H{regcomp} = $RE_ENGINE_PLUGIN;
\r
58 # Delete the regcomp hook
\r
60 if $^H{regcomp} == $RE_ENGINE_PLUGIN;
\r
63 # Minimal function to get CODE for a given key to be called by the
\r
64 # get_H_callback C function.
\r
67 my ($name) = @_; # 'comp', 'exec', ...
\r
69 my $h = (caller(0))[10];
\r
70 my $id = $h->{ $key->($name) };
\r
72 my $cb = defined $id ? $callback{$name}->{$id} : 0;
\r
79 my ($re, %callback) = @_;
\r
81 for my $key (keys %callback) {
\r
82 $key =~ y/a-z/A-Z/; # ASCII uc
\r
83 my $name = '_num_capture_buff_' . $key;
\r
84 $re->$name( $callback{$key} );
\r