Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.7 KiB

  1. package re;
  2. $VERSION = 0.02;
  3. =head1 NAME
  4. re - Perl pragma to alter regular expression behaviour
  5. =head1 SYNOPSIS
  6. use re 'taint';
  7. ($x) = ($^X =~ /^(.*)$/s); # $x is tainted here
  8. $pat = '(?{ $foo = 1 })';
  9. use re 'eval';
  10. /foo${pat}bar/; # won't fail (when not under -T switch)
  11. {
  12. no re 'taint'; # the default
  13. ($x) = ($^X =~ /^(.*)$/s); # $x is not tainted here
  14. no re 'eval'; # the default
  15. /foo${pat}bar/; # disallowed (with or without -T switch)
  16. }
  17. use re 'debug'; # NOT lexically scoped (as others are)
  18. /^(.*)$/s; # output debugging info during
  19. # compile and run time
  20. use re 'debugcolor'; # same as 'debug', but with colored output
  21. ...
  22. (We use $^X in these examples because it's tainted by default.)
  23. =head1 DESCRIPTION
  24. When C<use re 'taint'> is in effect, and a tainted string is the target
  25. of a regex, the regex memories (or values returned by the m// operator
  26. in list context) are tainted. This feature is useful when regex operations
  27. on tainted data aren't meant to extract safe substrings, but to perform
  28. other transformations.
  29. When C<use re 'eval'> is in effect, a regex is allowed to contain
  30. C<(?{ ... })> zero-width assertions even if the regex contains
  31. variable interpolation. This is normally disallowed, since it is a
  32. potential security risk. Note that this pragma is ignored when the regular
  33. expression is obtained from tainted data, i.e. evaluation is always
  34. disallowed with tainted regular expressions. See L<perlre/(?{ code })>.
  35. For the purpose of this pragma, interpolation of precompiled regular
  36. expressions (i.e., the result of C<qr//>) is I<not> considered variable
  37. interpolation. Thus:
  38. /foo${pat}bar/
  39. I<is> allowed if $pat is a precompiled regular expression, even
  40. if $pat contains C<(?{ ... })> assertions.
  41. When C<use re 'debug'> is in effect, perl emits debugging messages when
  42. compiling and using regular expressions. The output is the same as that
  43. obtained by running a C<-DDEBUGGING>-enabled perl interpreter with the
  44. B<-Dr> switch. It may be quite voluminous depending on the complexity
  45. of the match. Using C<debugcolor> instead of C<debug> enables a
  46. form of output that can be used to get a colorful display on terminals
  47. that understand termcap color sequences. Set C<$ENV{PERL_RE_TC}> to a
  48. comma-separated list of C<termcap> properties to use for highlighting
  49. strings on/off, pre-point part on/off.
  50. See L<perldebug/"Debugging regular expressions"> for additional info.
  51. The directive C<use re 'debug'> is I<not lexically scoped>, as the
  52. other directives are. It has both compile-time and run-time effects.
  53. See L<perlmodlib/Pragmatic Modules>.
  54. =cut
  55. my %bitmask = (
  56. taint => 0x00100000,
  57. eval => 0x00200000,
  58. );
  59. sub setcolor {
  60. eval { # Ignore errors
  61. require Term::Cap;
  62. my $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
  63. my $props = $ENV{PERL_RE_TC} || 'md,me,so,se'; # can use us/ue later
  64. my @props = split /,/, $props;
  65. $ENV{TERMCAP_COLORS} = join "\t", map {$terminal->Tputs($_,1)} @props;
  66. };
  67. not defined $ENV{TERMCAP_COLORS} or ($ENV{TERMCAP_COLORS} =~ tr/\t/\t/) >= 4
  68. or not defined $ENV{PERL_RE_TC}
  69. or die "Not enough fields in \$ENV{PERL_RE_TC}=`$ENV{PERL_RE_TC}'";
  70. }
  71. sub bits {
  72. my $on = shift;
  73. my $bits = 0;
  74. unless(@_) {
  75. require Carp;
  76. Carp::carp("Useless use of \"re\" pragma");
  77. }
  78. foreach my $s (@_){
  79. if ($s eq 'debug' or $s eq 'debugcolor') {
  80. setcolor() if $s eq 'debugcolor';
  81. require DynaLoader;
  82. @ISA = ('DynaLoader');
  83. bootstrap re;
  84. install() if $on;
  85. uninstall() unless $on;
  86. next;
  87. }
  88. $bits |= $bitmask{$s} || 0;
  89. }
  90. $bits;
  91. }
  92. sub import {
  93. shift;
  94. $^H |= bits(1,@_);
  95. }
  96. sub unimport {
  97. shift;
  98. $^H &= ~ bits(0,@_);
  99. }
  100. 1;