2da3a25dbdcba9621bc11cd600dfddcc291fe695
[p5sagit/p5-mst-13.2.git] / ext / re / re.pm
1 package re;
2
3 our $VERSION = 0.06_01;
4
5 =head1 NAME
6
7 re - Perl pragma to alter regular expression behaviour
8
9 =head1 SYNOPSIS
10
11     use re 'taint';
12     ($x) = ($^X =~ /^(.*)$/s);     # $x is tainted here
13
14     $pat = '(?{ $foo = 1 })';
15     use re 'eval';
16     /foo${pat}bar/;                # won't fail (when not under -T switch)
17
18     {
19         no re 'taint';             # the default
20         ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here
21
22         no re 'eval';              # the default
23         /foo${pat}bar/;            # disallowed (with or without -T switch)
24     }
25
26     use re 'debug';                # NOT lexically scoped (as others are)
27     /^(.*)$/s;                     # output debugging info during
28                                    #     compile and run time
29
30     use re 'debugcolor';           # same as 'debug', but with colored output
31     ...
32
33     use re qw(Debug All);          # Finer tuned debugging options.
34     use re qw(Debug More);         # Similarly not lexically scoped.
35     no re qw(Debug ALL);           # Turn of all re dugging and unload the module.
36
37 (We use $^X in these examples because it's tainted by default.)
38
39 =head1 DESCRIPTION
40
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.
46
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 })>.
53
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
56 interpolation.  Thus:
57
58     /foo${pat}bar/
59
60 I<is> allowed if $pat is a precompiled regular expression, even
61 if $pat contains C<(?{ ... })> assertions.
62
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.
73
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. Following the 'Debug' keyword one of several options may be
77 provided: COMPILE, EXECUTE, TRIE_COMPILE, TRIE_EXECUTE, TRIE_MORE,
78 OPTIMISE, OFFSETS and ALL. Additionally the special keywords 'All' and
79 'More' may be provided. 'All' represents everything but OPTIMISE and
80 OFFSETS and TRIE_MORE, and 'More' is similar but include TRIE_MORE.
81 Saying C<< no re Debug => 'EXECUTE' >> will disable executing debug
82 statements and saying C<< use re Debug => 'EXECUTE' >> will turn it on. Note
83 that these flags can be set directly via ${^RE_DEBUG_FLAGS} by using the
84 following flag values:
85
86
87     RE_DEBUG_COMPILE       0x01
88     RE_DEBUG_EXECUTE       0x02
89     RE_DEBUG_TRIE_COMPILE  0x04
90     RE_DEBUG_TRIE_EXECUTE  0x08
91     RE_DEBUG_TRIE_MORE     0x10
92     RE_DEBUG_OPTIMISE      0x20
93     RE_DEBUG_OFFSETS       0x40
94     RE_DEBUG_PARSE         0x80
95
96 The directive C<use re 'debug'> and its equivalents are I<not> lexically
97 scoped, as the other directives are.  They have both compile-time and run-time
98 effects.
99
100 See L<perlmodlib/Pragmatic Modules>.
101
102 =cut
103
104 # N.B. File::Basename contains a literal for 'taint' as a fallback.  If
105 # taint is changed here, File::Basename must be updated as well.
106 my %bitmask = (
107 taint           => 0x00100000, # HINT_RE_TAINT
108 eval            => 0x00200000, # HINT_RE_EVAL
109 );
110
111 sub setcolor {
112  eval {                         # Ignore errors
113   require Term::Cap;
114
115   my $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
116   my $props = $ENV{PERL_RE_TC} || 'md,me,so,se,us,ue';
117   my @props = split /,/, $props;
118   my $colors = join "\t", map {$terminal->Tputs($_,1)} @props;
119
120   $colors =~ s/\0//g;
121   $ENV{PERL_RE_COLORS} = $colors;
122  };
123 }
124
125 my %flags = (
126     COMPILE      => 1,
127     EXECUTE      => 2,
128     TRIE_COMPILE => 4,
129     TRIE_EXECUTE => 8,
130     TRIE_MORE    => 16,
131     OPTIMISE     => 32,
132     OPTIMIZE     => 32, # alias
133     OFFSETS      => 64,
134     PARSE        => 128,
135     ALL          => 255,
136     All          => 15,
137     More         => 31,
138 );
139
140 my $installed = 0;
141
142 sub _load_unload {
143     my $on = shift;
144     require XSLoader;
145     XSLoader::load('re');
146     install($on);
147 }
148
149 sub bits {
150     my $on = shift;
151     my $bits = 0;
152     unless (@_) {
153         require Carp;
154         Carp::carp("Useless use of \"re\" pragma");
155     }
156     foreach my $idx (0..$#_){
157         my $s=$_[$idx];
158         if ($s eq 'Debug' or $s eq 'Debugcolor') {
159             setcolor() if $s eq 'Debugcolor';
160             ${^RE_DEBUG_FLAGS} = 0 unless defined ${^RE_DEBUG_FLAGS};
161             for my $idx ($idx+1..$#_) {
162                 if ($flags{$_[$idx]}) {
163                     if ($on) {
164                         ${^RE_DEBUG_FLAGS} |= $flags{$_[$idx]};
165                         ${^RE_DEBUG_FLAGS} |= 1
166                                 if $flags{$_[$idx]}>2;
167                     } else {
168                         ${^RE_DEBUG_FLAGS} &= ~ $flags{$_[$idx]};
169                     }
170                 } else {
171                     require Carp;
172                     Carp::carp("Unknown \"re\" Debug flag '$_[$idx]', possible flags: ",
173                                join(", ",sort { $flags{$a} <=> $flags{$b} } keys %flags ) );
174                 }
175             }
176             _load_unload($on ? 1 : ${^RE_DEBUG_FLAGS});
177             last;
178         } elsif ($s eq 'debug' or $s eq 'debugcolor') {
179             setcolor() if $s eq 'debugcolor';
180             _load_unload($on);
181         } elsif (exists $bitmask{$s}) {
182             $bits |= $bitmask{$s};
183         } else {
184             require Carp;
185             Carp::carp("Unknown \"re\" subpragma '$s' (known ones are: ",
186                        join(', ', map {qq('$_')} 'debug', 'debugcolor', sort keys %bitmask),
187                        ")");
188         }
189     }
190     $bits;
191 }
192
193 sub import {
194     shift;
195     $^H |= bits(1, @_);
196 }
197
198 sub unimport {
199     shift;
200     $^H &= ~ bits(0, @_);
201 }
202
203 1;