Commit | Line | Data |
b3eb6a9b |
1 | package re; |
2 | |
a3621e74 |
3 | our $VERSION = 0.05; |
56953603 |
4 | |
b3eb6a9b |
5 | =head1 NAME |
6 | |
7 | re - Perl pragma to alter regular expression behaviour |
8 | |
9 | =head1 SYNOPSIS |
10 | |
e4d48cc9 |
11 | use re 'taint'; |
12 | ($x) = ($^X =~ /^(.*)$/s); # $x is tainted here |
b3eb6a9b |
13 | |
2cd61cdb |
14 | $pat = '(?{ $foo = 1 })'; |
e4d48cc9 |
15 | use re 'eval'; |
2cd61cdb |
16 | /foo${pat}bar/; # won't fail (when not under -T switch) |
e4d48cc9 |
17 | |
18 | { |
19 | no re 'taint'; # the default |
20 | ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here |
21 | |
22 | no re 'eval'; # the default |
2cd61cdb |
23 | /foo${pat}bar/; # disallowed (with or without -T switch) |
e4d48cc9 |
24 | } |
b3eb6a9b |
25 | |
0a92e3a8 |
26 | use re 'debug'; # NOT lexically scoped (as others are) |
27 | /^(.*)$/s; # output debugging info during |
28 | # compile and run time |
2cd61cdb |
29 | |
02ea72ae |
30 | use re 'debugcolor'; # same as 'debug', but with colored output |
31 | ... |
32 | |
a3621e74 |
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 | |
3ffabb8c |
37 | (We use $^X in these examples because it's tainted by default.) |
38 | |
b3eb6a9b |
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 |
e4d48cc9 |
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. |
b3eb6a9b |
46 | |
e4d48cc9 |
47 | When C<use re 'eval'> is in effect, a regex is allowed to contain |
2cd61cdb |
48 | C<(?{ ... })> zero-width assertions even if regular expression contains |
ffbc6a93 |
49 | variable interpolation. That is normally disallowed, since it is a |
2cd61cdb |
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 expresssions. See L<perlre/(?{ code })>. |
53 | |
ffbc6a93 |
54 | For the purpose of this pragma, interpolation of precompiled regular |
0a92e3a8 |
55 | expressions (i.e., the result of C<qr//>) is I<not> considered variable |
56 | interpolation. Thus: |
2cd61cdb |
57 | |
58 | /foo${pat}bar/ |
59 | |
ffbc6a93 |
60 | I<is> allowed if $pat is a precompiled regular expression, even |
2cd61cdb |
61 | if $pat contains C<(?{ ... })> assertions. |
62 | |
ffbc6a93 |
63 | When C<use re 'debug'> is in effect, perl emits debugging messages when |
2cd61cdb |
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 |
02ea72ae |
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 |
ffbc6a93 |
71 | strings on/off, pre-point part on/off. |
2cd61cdb |
72 | See L<perldebug/"Debugging regular expressions"> for additional info. |
73 | |
a3621e74 |
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 | RE_DEBUG_COMPILE 1 |
87 | RE_DEBUG_EXECUTE 2 |
88 | RE_DEBUG_TRIE_COMPILE 4 |
89 | RE_DEBUG_TRIE_EXECUTE 8 |
90 | RE_DEBUG_TRIE_MORE 16 |
91 | RE_DEBUG_OPTIMISE 32 |
92 | RE_DEBUG_OFFSETS 64 |
93 | |
94 | The directive C<use re 'debug'> and its equivalents are I<not> lexically |
95 | scoped, as the other directives are. They have both compile-time and run-time |
96 | effects. |
b3eb6a9b |
97 | |
98 | See L<perlmodlib/Pragmatic Modules>. |
99 | |
100 | =cut |
101 | |
918c0b2d |
102 | # N.B. File::Basename contains a literal for 'taint' as a fallback. If |
103 | # taint is changed here, File::Basename must be updated as well. |
b3eb6a9b |
104 | my %bitmask = ( |
9cfe5470 |
105 | taint => 0x00100000, # HINT_RE_TAINT |
106 | eval => 0x00200000, # HINT_RE_EVAL |
b3eb6a9b |
107 | ); |
108 | |
02ea72ae |
109 | sub setcolor { |
110 | eval { # Ignore errors |
111 | require Term::Cap; |
112 | |
113 | my $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning. |
8d300b32 |
114 | my $props = $ENV{PERL_RE_TC} || 'md,me,so,se,us,ue'; |
02ea72ae |
115 | my @props = split /,/, $props; |
c712d376 |
116 | my $colors = join "\t", map {$terminal->Tputs($_,1)} @props; |
02ea72ae |
117 | |
c712d376 |
118 | $colors =~ s/\0//g; |
119 | $ENV{PERL_RE_COLORS} = $colors; |
02ea72ae |
120 | }; |
02ea72ae |
121 | } |
122 | |
a3621e74 |
123 | my %flags = ( |
124 | COMPILE => 1, |
125 | EXECUTE => 2, |
126 | TRIE_COMPILE => 4, |
127 | TRIE_EXECUTE => 8, |
128 | TRIE_MORE => 16, |
129 | OPTIMISE => 32, |
130 | OPTIMIZE => 32, # alias |
131 | OFFSETS => 64, |
132 | ALL => 127, |
133 | All => 15, |
134 | More => 31, |
135 | ); |
136 | |
137 | my $installed = 0; |
138 | |
b3eb6a9b |
139 | sub bits { |
56953603 |
140 | my $on = shift; |
b3eb6a9b |
141 | my $bits = 0; |
2570cdf1 |
142 | unless (@_) { |
b3eb6a9b |
143 | require Carp; |
144 | Carp::carp("Useless use of \"re\" pragma"); |
145 | } |
a3621e74 |
146 | foreach my $idx (0..$#_){ |
147 | my $s=$_[$idx]; |
148 | if ($s eq 'Debug' or $s eq 'Debugcolor') { |
149 | setcolor() if $s eq 'Debugcolor'; |
150 | ${^RE_DEBUG_FLAGS} = 0 unless defined ${^RE_DEBUG_FLAGS}; |
151 | require XSLoader; |
152 | XSLoader::load('re'); |
153 | for my $idx ($idx+1..$#_) { |
154 | if ($flags{$_[$idx]}) { |
155 | if ($on) { |
156 | ${^RE_DEBUG_FLAGS} |= $flags{$_[$idx]}; |
157 | } else { |
158 | ${^RE_DEBUG_FLAGS} &= ~ $flags{$_[$idx]}; |
159 | } |
160 | } else { |
161 | require Carp; |
162 | Carp::carp("Unknown \"re\" Debug flag '$_[$idx]', possible flags: ", |
163 | join(", ",sort { $flags{$a} <=> $flags{$b} } keys %flags ) ); |
164 | } |
165 | } |
166 | if ($on) { |
167 | install() unless $installed; |
168 | $installed = 1; |
169 | } elsif (!${^RE_DEBUG_FLAGS}) { |
170 | uninstall() if $installed; |
171 | $installed = 0; |
172 | } |
173 | last; |
174 | } elsif ($s eq 'debug' or $s eq 'debugcolor') { |
175 | setcolor() if $s eq 'debugcolor'; |
176 | require XSLoader; |
177 | XSLoader::load('re'); |
178 | if ($on) { |
179 | install() unless $installed; |
180 | $installed=1; |
181 | } else { |
182 | uninstall() if $installed; |
183 | $installed=0; |
184 | } |
185 | } elsif (exists $bitmask{$s}) { |
186 | $bits |= $bitmask{$s}; |
187 | } else { |
188 | require Carp; |
189 | Carp::carp("Unknown \"re\" subpragma '$s' (known ones are: ", |
190 | join(', ', map {qq('$_')} 'debug', 'debugcolor', sort keys %bitmask), |
191 | ")"); |
192 | } |
56953603 |
193 | } |
b3eb6a9b |
194 | $bits; |
195 | } |
196 | |
197 | sub import { |
198 | shift; |
2570cdf1 |
199 | $^H |= bits(1, @_); |
b3eb6a9b |
200 | } |
201 | |
202 | sub unimport { |
203 | shift; |
2570cdf1 |
204 | $^H &= ~ bits(0, @_); |
b3eb6a9b |
205 | } |
206 | |
207 | 1; |