1 package re::engine::Hooks;
10 re::engine::Hooks - Hookable variant of the Perl core regular expression engine.
20 sub dl_load_flags { 0x01 }
25 push @ISA, qw<Regexp DynaLoader>;
26 __PACKAGE__->bootstrap($VERSION);
33 #include "re_engine_hooks.h"
35 STATIC void dri_comp_node_hook(pTHX_ regexp *rx, regnode *node) {
39 STATIC void dri_exec_node_hook(pTHX_
40 regexp *rx, regnode *node, regmatch_info *info, regmatch_state *state) {
44 MODULE = Devel::Regexp::Instrument PACKAGE = Devel::Regexp::Instrument
49 cfg.comp_node = dri_comp_node_hook;
50 cfg.exec_node = dri_exec_node_hook;
51 reh_register("Devel::Regexp::Instrument", &cfg);
54 In your Perl module file :
56 package Devel::Regexp::Instrument;
63 use re::engine::Hooks; # Before loading our own shared library
68 push @ISA, 'DynaLoader';
69 __PACKAGE__->bootstrap($VERSION);
72 sub import { re::engine::Hooks::enable(__PACKAGE__) }
74 sub unimport { re::engine::Hooks::disable(__PACKAGE__) }
78 In your F<Makefile.PL>
80 use ExtUtils::Depends;
82 my $ed = ExtUtils::Depends->new(
83 'Devel::Regexp::Instrument' => 're::engine::Hooks',
87 $ed->get_makefile_vars,
93 This module provides a version of the perl regexp engine that can call user-defined XS callbacks at the compilation and at the execution of each regexp node.
97 The C API is made available through the F<re_engine_hooks.h> header file.
99 =head2 C<reh_comp_node_hook>
101 The typedef for the regexp node compilation phase hook.
102 Currently evaluates to :
104 typedef void (*reh_comp_node_hook)(pTHX_ regexp *, regnode *);
106 =head2 C<reh_exec_node_hook>
108 The typedef for the regexp node_execution phase hook.
109 Currently evaluates to :
111 typedef void (*reh_exec_node_hook)(pTHX_ regexp *, regnode *, regmatch_info *, regmatch_state *);
115 A typedef'd struct that holds a set of all the different callbacks publicized by this module.
116 It has the following members :
124 A function pointer of type C<reh_comp_node_hook> that will be called each time a regnode is compiled.
125 Allowed to be C<NULL> if you don't want to call anything for this phase.
131 A function pointer of type C<reh_exec_node_hook> that will be called each time a regnode is executed.
132 Allowed to be C<NULL> if you don't want to call anything for this phase.
136 =head2 C<reh_register>
138 void reh_register(pTHX_ const char *key, reh_config *cfg);
140 Registers the callbacks specified by the C<reh_config *> object C<cfg> under the given name C<key>.
141 C<cfg> can be a pointer to a static object of type C<reh_config>.
142 C<key> is expected to be a nul-terminated string and should match the argument passed to L</enable> and L</disable> in Perl land.
143 An exception will be thrown if C<key> has already been used to register callbacks.
147 my $RE_ENGINE = _ENGINE();
160 Lexically enables the hooks associated with the key C<$key>.
166 Lexically disables the hooks associated with the key C<$key>.
173 s/^\s+//, s/\s+$// for $key;
174 $croak->('Invalid key') if $key =~ /\s/ or not _registered($key);
175 $croak->('Another regexp engine is in use') if $^H{regcomp}
176 and $^H{regcomp} != $RE_ENGINE;
180 my $hint = $^H{+(__PACKAGE__)} // '';
181 $hint = "$key $hint";
182 $^H{+(__PACKAGE__)} = $hint;
184 $^H{regcomp} = $RE_ENGINE;
192 s/^\s+//, s/\s+$// for $key;
193 $croak->('Invalid key') if $key =~ /\s/ or not _registered($key);
197 my @other_keys = grep !/^\Q$key\E$/, split /\s+/, $^H{+(__PACKAGE__)} // '';
198 $^H{+(__PACKAGE__)} = join ' ', @other_keys, '';
200 delete $^H{regcomp} if $^H{regcomp} and $^{regcomp} == $RE_ENGINE
208 Please refer to the F<t/re-engine-Hooks-TestDist/> directory in the distribution.
209 It implements a couple of simple examples.
215 L<ExtUtils::Depends>.
223 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
225 You can contact me by mail or on C<irc.perl.org> (vincent).
229 Please report any bugs or feature requests to C<bug-re-engine-hooks at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=re-engine-Hooks>.
230 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
234 You can find documentation for this module with the perldoc command :
236 perldoc re::engine::Hooks
238 =head1 COPYRIGHT & LICENSE
240 Copyright 2012 Vincent Pit, all rights reserved.
242 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.