1*0Sstevel@tonic-gatepackage re; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateour $VERSION = 0.04; 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 NAME 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gatere - Perl pragma to alter regular expression behaviour 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate=head1 SYNOPSIS 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gate use re 'taint'; 12*0Sstevel@tonic-gate ($x) = ($^X =~ /^(.*)$/s); # $x is tainted here 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gate $pat = '(?{ $foo = 1 })'; 15*0Sstevel@tonic-gate use re 'eval'; 16*0Sstevel@tonic-gate /foo${pat}bar/; # won't fail (when not under -T switch) 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate { 19*0Sstevel@tonic-gate no re 'taint'; # the default 20*0Sstevel@tonic-gate ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate no re 'eval'; # the default 23*0Sstevel@tonic-gate /foo${pat}bar/; # disallowed (with or without -T switch) 24*0Sstevel@tonic-gate } 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate use re 'debug'; # NOT lexically scoped (as others are) 27*0Sstevel@tonic-gate /^(.*)$/s; # output debugging info during 28*0Sstevel@tonic-gate # compile and run time 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate use re 'debugcolor'; # same as 'debug', but with colored output 31*0Sstevel@tonic-gate ... 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate(We use $^X in these examples because it's tainted by default.) 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate=head1 DESCRIPTION 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gateWhen C<use re 'taint'> is in effect, and a tainted string is the target 38*0Sstevel@tonic-gateof a regex, the regex memories (or values returned by the m// operator 39*0Sstevel@tonic-gatein list context) are tainted. This feature is useful when regex operations 40*0Sstevel@tonic-gateon tainted data aren't meant to extract safe substrings, but to perform 41*0Sstevel@tonic-gateother transformations. 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gateWhen C<use re 'eval'> is in effect, a regex is allowed to contain 44*0Sstevel@tonic-gateC<(?{ ... })> zero-width assertions even if regular expression contains 45*0Sstevel@tonic-gatevariable interpolation. That is normally disallowed, since it is a 46*0Sstevel@tonic-gatepotential security risk. Note that this pragma is ignored when the regular 47*0Sstevel@tonic-gateexpression is obtained from tainted data, i.e. evaluation is always 48*0Sstevel@tonic-gatedisallowed with tainted regular expresssions. See L<perlre/(?{ code })>. 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gateFor the purpose of this pragma, interpolation of precompiled regular 51*0Sstevel@tonic-gateexpressions (i.e., the result of C<qr//>) is I<not> considered variable 52*0Sstevel@tonic-gateinterpolation. Thus: 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate /foo${pat}bar/ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gateI<is> allowed if $pat is a precompiled regular expression, even 57*0Sstevel@tonic-gateif $pat contains C<(?{ ... })> assertions. 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gateWhen C<use re 'debug'> is in effect, perl emits debugging messages when 60*0Sstevel@tonic-gatecompiling and using regular expressions. The output is the same as that 61*0Sstevel@tonic-gateobtained by running a C<-DDEBUGGING>-enabled perl interpreter with the 62*0Sstevel@tonic-gateB<-Dr> switch. It may be quite voluminous depending on the complexity 63*0Sstevel@tonic-gateof the match. Using C<debugcolor> instead of C<debug> enables a 64*0Sstevel@tonic-gateform of output that can be used to get a colorful display on terminals 65*0Sstevel@tonic-gatethat understand termcap color sequences. Set C<$ENV{PERL_RE_TC}> to a 66*0Sstevel@tonic-gatecomma-separated list of C<termcap> properties to use for highlighting 67*0Sstevel@tonic-gatestrings on/off, pre-point part on/off. 68*0Sstevel@tonic-gateSee L<perldebug/"Debugging regular expressions"> for additional info. 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gateThe directive C<use re 'debug'> is I<not lexically scoped>, as the 71*0Sstevel@tonic-gateother directives are. It has both compile-time and run-time effects. 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gateSee L<perlmodlib/Pragmatic Modules>. 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate=cut 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate# N.B. File::Basename contains a literal for 'taint' as a fallback. If 78*0Sstevel@tonic-gate# taint is changed here, File::Basename must be updated as well. 79*0Sstevel@tonic-gatemy %bitmask = ( 80*0Sstevel@tonic-gatetaint => 0x00100000, # HINT_RE_TAINT 81*0Sstevel@tonic-gateeval => 0x00200000, # HINT_RE_EVAL 82*0Sstevel@tonic-gate); 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gatesub setcolor { 85*0Sstevel@tonic-gate eval { # Ignore errors 86*0Sstevel@tonic-gate require Term::Cap; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate my $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning. 89*0Sstevel@tonic-gate my $props = $ENV{PERL_RE_TC} || 'md,me,so,se,us,ue'; 90*0Sstevel@tonic-gate my @props = split /,/, $props; 91*0Sstevel@tonic-gate my $colors = join "\t", map {$terminal->Tputs($_,1)} @props; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate $colors =~ s/\0//g; 94*0Sstevel@tonic-gate $ENV{PERL_RE_COLORS} = $colors; 95*0Sstevel@tonic-gate }; 96*0Sstevel@tonic-gate} 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gatesub bits { 99*0Sstevel@tonic-gate my $on = shift; 100*0Sstevel@tonic-gate my $bits = 0; 101*0Sstevel@tonic-gate unless (@_) { 102*0Sstevel@tonic-gate require Carp; 103*0Sstevel@tonic-gate Carp::carp("Useless use of \"re\" pragma"); 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate foreach my $s (@_){ 106*0Sstevel@tonic-gate if ($s eq 'debug' or $s eq 'debugcolor') { 107*0Sstevel@tonic-gate setcolor() if $s eq 'debugcolor'; 108*0Sstevel@tonic-gate require XSLoader; 109*0Sstevel@tonic-gate XSLoader::load('re'); 110*0Sstevel@tonic-gate install() if $on; 111*0Sstevel@tonic-gate uninstall() unless $on; 112*0Sstevel@tonic-gate next; 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate if (exists $bitmask{$s}) { 115*0Sstevel@tonic-gate $bits |= $bitmask{$s}; 116*0Sstevel@tonic-gate } else { 117*0Sstevel@tonic-gate require Carp; 118*0Sstevel@tonic-gate Carp::carp("Unknown \"re\" subpragma '$s' (known ones are: @{[join(', ', map {qq('$_')} 'debug', 'debugcolor', sort keys %bitmask)]})"); 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate $bits; 122*0Sstevel@tonic-gate} 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gatesub import { 125*0Sstevel@tonic-gate shift; 126*0Sstevel@tonic-gate $^H |= bits(1, @_); 127*0Sstevel@tonic-gate} 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gatesub unimport { 130*0Sstevel@tonic-gate shift; 131*0Sstevel@tonic-gate $^H &= ~ bits(0, @_); 132*0Sstevel@tonic-gate} 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate1; 135