3 our $VERSION = 0.06_02;
7 re - Perl pragma to alter regular expression behaviour
12 ($x) = ($^X =~ /^(.*)$/s); # $x is tainted here
14 $pat = '(?{ $foo = 1 })';
16 /foo${pat}bar/; # won't fail (when not under -T switch)
19 no re 'taint'; # the default
20 ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here
22 no re 'eval'; # the default
23 /foo${pat}bar/; # disallowed (with or without -T switch)
26 use re 'debug'; # output debugging info during
27 /^(.*)$/s; # compile and run time
30 use re 'debugcolor'; # same as 'debug', but with colored output
33 use re qw(Debug All); # Finer tuned debugging options.
34 use re qw(Debug More);
35 no re qw(Debug ALL); # Turn of all re debugging in this scope
37 (We use $^X in these examples because it's tainted by default.)
41 When C<use re 'taint'> is in effect, and a tainted string is the target
42 of a regex, the regex memories (or values returned by the m// operator
43 in list context) are tainted. This feature is useful when regex operations
44 on tainted data aren't meant to extract safe substrings, but to perform
45 other transformations.
47 When C<use re 'eval'> is in effect, a regex is allowed to contain
48 C<(?{ ... })> zero-width assertions even if regular expression contains
49 variable interpolation. That is normally disallowed, since it is a
50 potential security risk. Note that this pragma is ignored when the regular
51 expression is obtained from tainted data, i.e. evaluation is always
52 disallowed with tainted regular expressions. See L<perlre/(?{ code })>.
54 For the purpose of this pragma, interpolation of precompiled regular
55 expressions (i.e., the result of C<qr//>) is I<not> considered variable
60 I<is> allowed if $pat is a precompiled regular expression, even
61 if $pat contains C<(?{ ... })> assertions.
63 When C<use re 'debug'> is in effect, perl emits debugging messages when
64 compiling and using regular expressions. The output is the same as that
65 obtained by running a C<-DDEBUGGING>-enabled perl interpreter with the
66 B<-Dr> switch. It may be quite voluminous depending on the complexity
67 of the match. Using C<debugcolor> instead of C<debug> enables a
68 form of output that can be used to get a colorful display on terminals
69 that understand termcap color sequences. Set C<$ENV{PERL_RE_TC}> to a
70 comma-separated list of C<termcap> properties to use for highlighting
71 strings on/off, pre-point part on/off.
72 See L<perldebug/"Debugging regular expressions"> for additional info.
74 Similarly C<use re 'Debug'> produces debugging output, the difference
75 being that it allows the fine tuning of what debugging output will be
76 emitted. Options are divided into three groups, those related to
77 compilation, those related to execution and those related to special
78 purposes. The options are as follows:
82 =item Compile related options
88 Turns on all compile related debug options.
92 Turns on debug output related to the process of parsing the pattern.
96 Enables output related to the optimisation phase of compilation.
100 Detailed info about trie compilation.
104 Dump the final program out after it is compiled and optimised.
109 =item Execute related options
115 Turns on all execute related debug options.
119 Turns on debugging of the main matching loop.
123 Extra debugging of how tries execute.
127 Enable debugging of start point optimisations.
131 =item Extra debugging options
137 Turns on all "extra" debugging options.
141 Enable enhanced TRIE debugging. Enhances both TRIEE
146 Enable debugging of states in the engine.
150 Enable debugging of the recursion stack in the engine. Enabling
151 or disabling this option automatically does the same for debugging
152 states as well. This output from this can be quite large.
156 Enable enhanced optimisation debugging and start point optimisations.
157 Probably not useful except when debugging the regex engine itself.
161 Dump offset information. This can be used to see how regops correlate
162 to the pattern. Output format is
164 NODENUM:POSITION[LENGTH]
166 Where 1 is the position of the first char in the string. Note that position
167 can be 0, or larger than the actual length of the pattern, likewise length
172 Enable debugging of offsets information. This emits copious
173 amounts of trace information and doesn't mesh well with other
176 Almost definitely only useful to people hacking
177 on the offsets part of the debug engine.
181 =item Other useful flags
183 These are useful shortcuts to save on the typing.
189 Enable all compile and execute options at once.
193 Enable DUMP and all execute options. Equivalent to:
201 Enable TRIEM and all execute compile and execute options.
207 As of 5.9.5 the directive C<use re 'debug'> and its equivalents are
208 lexically scoped, as the other directives are. However they have both
209 compile-time and run-time effects.
211 See L<perlmodlib/Pragmatic Modules>.
215 # N.B. File::Basename contains a literal for 'taint' as a fallback. If
216 # taint is changed here, File::Basename must be updated as well.
218 taint => 0x00100000, # HINT_RE_TAINT
219 eval => 0x00200000, # HINT_RE_EVAL
223 eval { # Ignore errors
226 my $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
227 my $props = $ENV{PERL_RE_TC} || 'md,me,so,se,us,ue';
228 my @props = split /,/, $props;
229 my $colors = join "\t", map {$terminal->Tputs($_,1)} @props;
232 $ENV{PERL_RE_COLORS} = $colors;
235 $ENV{PERL_RE_COLORS}||=qq'\t\t> <\t> <\t\t'
243 OPTIMISE => 0x000002,
255 OFFSETSDBG => 0x040000,
257 OPTIMISEM => 0x100000,
261 $flags{All} = $flags{all} = $flags{DUMP} | $flags{EXECUTE};
262 $flags{Extra} = $flags{EXECUTE} | $flags{COMPILE};
263 $flags{More} = $flags{MORE} = $flags{All} | $flags{TRIEC} | $flags{TRIEM} | $flags{STATE};
264 $flags{State} = $flags{DUMP} | $flags{EXECUTE} | $flags{STATE};
265 $flags{TRIE} = $flags{DUMP} | $flags{EXECUTE} | $flags{TRIEC};
273 if ( ! defined($installed) ) {
275 $installed = eval { XSLoader::load('re') } || 0;
276 $installed_error = $@;
278 if ( ! $installed ) {
279 die "'re' not installed!? ($installed_error)";
281 # We call install() every time, as if we didn't, we wouldn't
282 # "see" any changes to the color environment var since
283 # the last time it was called.
285 # install() returns an integer, which if casted properly
286 # in C resolves to a structure containing the regex
287 # hooks. Setting it to a random integer will guarantee
289 $^H{regcomp} = install();
302 foreach my $idx (0..$#_){
304 if ($s eq 'Debug' or $s eq 'Debugcolor') {
305 setcolor() if $s =~/color/i;
306 ${^RE_DEBUG_FLAGS} = 0 unless defined ${^RE_DEBUG_FLAGS};
307 for my $idx ($idx+1..$#_) {
308 if ($flags{$_[$idx]}) {
310 ${^RE_DEBUG_FLAGS} |= $flags{$_[$idx]};
312 ${^RE_DEBUG_FLAGS} &= ~ $flags{$_[$idx]};
316 Carp::carp("Unknown \"re\" Debug flag '$_[$idx]', possible flags: ",
317 join(", ",sort keys %flags ) );
320 _load_unload($on ? 1 : ${^RE_DEBUG_FLAGS});
322 } elsif ($s eq 'debug' or $s eq 'debugcolor') {
323 setcolor() if $s =~/color/i;
325 } elsif (exists $bitmask{$s}) {
326 $bits |= $bitmask{$s};
329 Carp::carp("Unknown \"re\" subpragma '$s' (known ones are: ",
330 join(', ', map {qq('$_')} 'debug', 'debugcolor', sort keys %bitmask),
344 $^H &= ~ bits(0, @_);