Leaked source code of windows server 2003
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.

117 lines
2.7 KiB

  1. package strict;
  2. =head1 NAME
  3. strict - Perl pragma to restrict unsafe constructs
  4. =head1 SYNOPSIS
  5. use strict;
  6. use strict "vars";
  7. use strict "refs";
  8. use strict "subs";
  9. use strict;
  10. no strict "vars";
  11. =head1 DESCRIPTION
  12. If no import list is supplied, all possible restrictions are assumed.
  13. (This is the safest mode to operate in, but is sometimes too strict for
  14. casual programming.) Currently, there are three possible things to be
  15. strict about: "subs", "vars", and "refs".
  16. =over 6
  17. =item C<strict refs>
  18. This generates a runtime error if you
  19. use symbolic references (see L<perlref>).
  20. use strict 'refs';
  21. $ref = \$foo;
  22. print $$ref; # ok
  23. $ref = "foo";
  24. print $$ref; # runtime error; normally ok
  25. $file = "STDOUT";
  26. print $file "Hi!"; # error; note: no comma after $file
  27. There is one exception to this rule:
  28. $bar = \&{'foo'};
  29. &$bar;
  30. is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
  31. =item C<strict vars>
  32. This generates a compile-time error if you access a variable that wasn't
  33. declared via "our" or C<use vars>,
  34. localized via C<my()>, or wasn't fully qualified. Because this is to avoid
  35. variable suicide problems and subtle dynamic scoping issues, a merely
  36. local() variable isn't good enough. See L<perlfunc/my> and
  37. L<perlfunc/local>.
  38. use strict 'vars';
  39. $X::foo = 1; # ok, fully qualified
  40. my $foo = 10; # ok, my() var
  41. local $foo = 9; # blows up
  42. package Cinna;
  43. our $bar; # Declares $bar in current package
  44. $bar = 'HgS'; # ok, global declared via pragma
  45. The local() generated a compile-time error because you just touched a global
  46. name without fully qualifying it.
  47. Because of their special use by sort(), the variables $a and $b are
  48. exempted from this check.
  49. =item C<strict subs>
  50. This disables the poetry optimization, generating a compile-time error if
  51. you try to use a bareword identifier that's not a subroutine, unless it
  52. appears in curly braces or on the left hand side of the "=E<gt>" symbol.
  53. use strict 'subs';
  54. $SIG{PIPE} = Plumber; # blows up
  55. $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok
  56. $SIG{PIPE} = \&Plumber; # preferred form
  57. =back
  58. See L<perlmodlib/Pragmatic Modules>.
  59. =cut
  60. $strict::VERSION = "1.01";
  61. my %bitmask = (
  62. refs => 0x00000002,
  63. subs => 0x00000200,
  64. vars => 0x00000400
  65. );
  66. sub bits {
  67. my $bits = 0;
  68. foreach my $s (@_){ $bits |= $bitmask{$s} || 0; };
  69. $bits;
  70. }
  71. sub import {
  72. shift;
  73. $^H |= bits(@_ ? @_ : qw(refs subs vars));
  74. }
  75. sub unimport {
  76. shift;
  77. $^H &= ~ bits(@_ ? @_ : qw(refs subs vars));
  78. }
  79. 1;