X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Fre-engine-Plugin.git;a=blobdiff_plain;f=Plugin.pm;h=5b84612f64e9e5aab60691897fa13bdb868164ad;hp=d8fa3228907fb93a2913975be35c7a4769e8a26d;hb=7fb1e66881f572d36b7c22624b7f09672170b990;hpb=74d4856719cde7c6dc286b0c3e5dc78112d2fbff diff --git a/Plugin.pm b/Plugin.pm index d8fa322..5b84612 100644 --- a/Plugin.pm +++ b/Plugin.pm @@ -1,88 +1,88 @@ -# See Plugin.pod for documentation -package re::engine::Plugin; -use 5.009005; -use strict; -use XSLoader (); - -our $VERSION = '0.04'; - -# All engines should subclass the core Regexp package -our @ISA = 'Regexp'; - -XSLoader::load __PACKAGE__, $VERSION; - -my $RE_ENGINE_PLUGIN = ENGINE(); - -# How many? Used to cheat %^H -my $callback = 1; - -# Where we store our CODE refs -my %callback; - -# Generate a key to use in the %^H hash from a string, prefix the -# package name like L does -my $key = sub { __PACKAGE__ . "::" . $_[0] }; - -sub import -{ - my ($pkg, %sub) = @_; - - # Valid callbacks - my @callback = qw(comp exec); - - for (@callback) { - next unless exists $sub{$_}; - my $cb = delete $sub{$_}; - - unless (ref $cb eq 'CODE') { - require Carp; - Carp::croak("'$_' is not CODE"); - } - - # Get an ID to use - my $id = $callback ++; - - # Insert into our callback storage, - $callback{$_}->{$id} = $cb; - - # Instert into our cache with a key we can retrive later - # knowing the ID in %^H and what callback we're getting - $^H{ $key->($_) } = $id; - } - - $^H{regcomp} = $RE_ENGINE_PLUGIN; -} - -sub unimport -{ - # Delete the regcomp hook - delete $^H{regcomp} - if $^H{regcomp} == $RE_ENGINE_PLUGIN; -} - -# Minimal function to get CODE for a given key to be called by the -# get_H_callback C function. -sub _get_callback -{ - my ($name) = @_; # 'comp', 'exec', ... - - my $h = (caller(0))[10]; - my $id = $h->{ $key->($name) }; - - my $cb = defined $id ? $callback{$name}->{$id} : 0; - - return $cb; -} - -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; +# See Plugin.pod for documentation +package re::engine::Plugin; +use 5.009005; +use strict; +use XSLoader (); + +our $VERSION = '0.05'; + +# All engines should subclass the core Regexp package +our @ISA = 'Regexp'; + +XSLoader::load __PACKAGE__, $VERSION; + +my $RE_ENGINE_PLUGIN = ENGINE(); + +# How many? Used to cheat %^H +my $callback = 1; + +# Where we store our CODE refs +my %callback; + +# Generate a key to use in the %^H hash from a string, prefix the +# package name like L does +my $key = sub { __PACKAGE__ . "::" . $_[0] }; + +sub import +{ + my ($pkg, %sub) = @_; + + # Valid callbacks + my @callback = qw(comp exec); + + for (@callback) { + next unless exists $sub{$_}; + my $cb = delete $sub{$_}; + + unless (ref $cb eq 'CODE') { + require Carp; + Carp::croak("'$_' is not CODE"); + } + + # Get an ID to use + my $id = $callback ++; + + # Insert into our callback storage, + $callback{$_}->{$id} = $cb; + + # Instert into our cache with a key we can retrive later + # knowing the ID in %^H and what callback we're getting + $^H{ $key->($_) } = $id; + } + + $^H{regcomp} = $RE_ENGINE_PLUGIN; +} + +sub unimport +{ + # Delete the regcomp hook + delete $^H{regcomp} + if $^H{regcomp} == $RE_ENGINE_PLUGIN; +} + +# Minimal function to get CODE for a given key to be called by the +# get_H_callback C function. +sub _get_callback +{ + my ($name) = @_; # 'comp', 'exec', ... + + my $h = (caller(0))[10]; + my $id = $h->{ $key->($name) }; + + my $cb = defined $id ? $callback{$name}->{$id} : 0; + + return $cb; +} + +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;