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.

104 lines
2.2 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. =item C<strict vars>
  26. This generates a compile-time error if you access a variable that wasn't
  27. declared via C<use vars>,
  28. localized via C<my()> or wasn't fully qualified. Because this is to avoid
  29. variable suicide problems and subtle dynamic scoping issues, a merely
  30. local() variable isn't good enough. See L<perlfunc/my> and
  31. L<perlfunc/local>.
  32. use strict 'vars';
  33. $X::foo = 1; # ok, fully qualified
  34. my $foo = 10; # ok, my() var
  35. local $foo = 9; # blows up
  36. package Cinna;
  37. use vars qw/ $bar /; # Declares $bar in current package
  38. $bar = 'HgS'; # ok, global declared via pragma
  39. The local() generated a compile-time error because you just touched a global
  40. name without fully qualifying it.
  41. =item C<strict subs>
  42. This disables the poetry optimization, generating a compile-time error if
  43. you try to use a bareword identifier that's not a subroutine, unless it
  44. appears in curly braces or on the left hand side of the "=E<gt>" symbol.
  45. use strict 'subs';
  46. $SIG{PIPE} = Plumber; # blows up
  47. $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok
  48. $SIG{PIPE} = \&Plumber; # preferred form
  49. =back
  50. See L<perlmodlib/Pragmatic Modules>.
  51. =cut
  52. $strict::VERSION = "1.01";
  53. my %bitmask = (
  54. refs => 0x00000002,
  55. subs => 0x00000200,
  56. vars => 0x00000400
  57. );
  58. sub bits {
  59. my $bits = 0;
  60. foreach my $s (@_){ $bits |= $bitmask{$s} || 0; };
  61. $bits;
  62. }
  63. sub import {
  64. shift;
  65. $^H |= bits(@_ ? @_ : qw(refs subs vars));
  66. }
  67. sub unimport {
  68. shift;
  69. $^H &= ~ bits(@_ ? @_ : qw(refs subs vars));
  70. }
  71. 1;