This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix accidental RE-de-optimization
[perl5.git] / lib / re.pm
1 package re;
2
3 =head1 NAME
4
5 re - Perl pragma to alter regular expression behaviour
6
7 =head1 SYNOPSIS
8
9     use re 'taint';
10     ($x) = ($^X =~ /^(.*)$/s);     # $x is tainted here
11
12     use re 'eval';
13     /foo(?{ $foo = 1 })bar/;       # won't fail (when not under -T switch)
14
15     {
16         no re 'taint';             # the default
17         ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here
18
19         no re 'eval';              # the default
20         /foo(?{ $foo = 1 })bar/;   # disallowed (with or without -T switch)
21     }
22
23 =head1 DESCRIPTION
24
25 When C<use re 'taint'> is in effect, and a tainted string is the target
26 of a regex, the regex memories (or values returned by the m// operator
27 in list context) are tainted.  This feature is useful when regex operations
28 on tainted data aren't meant to extract safe substrings, but to perform
29 other transformations.
30
31 When C<use re 'eval'> is in effect, a regex is allowed to contain
32 C<(?{ ... })> zero-width assertions (which may not be interpolated in
33 the regex).  That is normally disallowed, since it is a potential security
34 risk.  Note that this pragma is ignored when perl detects tainted data,
35 i.e.  evaluation is always disallowed with tainted data.  See
36 L<perlre/(?{ code })>.
37
38 See L<perlmodlib/Pragmatic Modules>.
39
40 =cut
41
42 my %bitmask = (
43 taint   => 0x00100000,
44 eval    => 0x00200000,
45 );
46
47 sub bits {
48     my $bits = 0;
49     unless(@_) {
50         require Carp;
51         Carp::carp("Useless use of \"re\" pragma");
52     }
53     foreach my $s (@_){ $bits |= $bitmask{$s} || 0; };
54     $bits;
55 }
56
57 sub import {
58     shift;
59     $^H |= bits(@_);
60 }
61
62 sub unimport {
63     shift;
64     $^H &= ~ bits(@_);
65 }
66
67 1;