strict.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package strict;
  2. $strict::VERSION = "1.09";
  3. # Verify that we're called correctly so that strictures will work.
  4. unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
  5. # Can't use Carp, since Carp uses us!
  6. my (undef, $f, $l) = caller;
  7. die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
  8. }
  9. my ( %bitmask, %explicit_bitmask );
  10. BEGIN {
  11. %bitmask = (
  12. refs => 0x00000002,
  13. subs => 0x00000200,
  14. vars => 0x00000400,
  15. );
  16. %explicit_bitmask = (
  17. refs => 0x00000020,
  18. subs => 0x00000040,
  19. vars => 0x00000080,
  20. );
  21. my $bits = 0;
  22. $bits |= $_ for values %bitmask;
  23. my $inline_all_bits = $bits;
  24. *all_bits = sub () { $inline_all_bits };
  25. $bits = 0;
  26. $bits |= $_ for values %explicit_bitmask;
  27. my $inline_all_explicit_bits = $bits;
  28. *all_explicit_bits = sub () { $inline_all_explicit_bits };
  29. }
  30. sub bits {
  31. my $bits = 0;
  32. my @wrong;
  33. foreach my $s (@_) {
  34. if (exists $bitmask{$s}) {
  35. $^H |= $explicit_bitmask{$s};
  36. $bits |= $bitmask{$s};
  37. }
  38. else {
  39. push @wrong, $s;
  40. }
  41. }
  42. if (@wrong) {
  43. require Carp;
  44. Carp::croak("Unknown 'strict' tag(s) '@wrong'");
  45. }
  46. $bits;
  47. }
  48. sub import {
  49. shift;
  50. $^H |= @_ ? &bits : all_bits | all_explicit_bits;
  51. }
  52. sub unimport {
  53. shift;
  54. if (@_) {
  55. $^H &= ~&bits;
  56. }
  57. else {
  58. $^H &= ~all_bits;
  59. $^H |= all_explicit_bits;
  60. }
  61. }
  62. 1;
  63. __END__
  64. =head1 NAME
  65. strict - Perl pragma to restrict unsafe constructs
  66. =head1 SYNOPSIS
  67. use strict;
  68. use strict "vars";
  69. use strict "refs";
  70. use strict "subs";
  71. use strict;
  72. no strict "vars";
  73. =head1 DESCRIPTION
  74. If no import list is supplied, all possible restrictions are assumed.
  75. (This is the safest mode to operate in, but is sometimes too strict for
  76. casual programming.) Currently, there are three possible things to be
  77. strict about: "subs", "vars", and "refs".
  78. =over 6
  79. =item C<strict refs>
  80. This generates a runtime error if you
  81. use symbolic references (see L<perlref>).
  82. use strict 'refs';
  83. $ref = \$foo;
  84. print $$ref; # ok
  85. $ref = "foo";
  86. print $$ref; # runtime error; normally ok
  87. $file = "STDOUT";
  88. print $file "Hi!"; # error; note: no comma after $file
  89. There is one exception to this rule:
  90. $bar = \&{'foo'};
  91. &$bar;
  92. is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
  93. =item C<strict vars>
  94. This generates a compile-time error if you access a variable that was
  95. neither explicitly declared (using any of C<my>, C<our>, C<state>, or C<use
  96. vars>) nor fully qualified. (Because this is to avoid variable suicide
  97. problems and subtle dynamic scoping issues, a merely C<local> variable isn't
  98. good enough.) See L<perlfunc/my>, L<perlfunc/our>, L<perlfunc/state>,
  99. L<perlfunc/local>, and L<vars>.
  100. use strict 'vars';
  101. $X::foo = 1; # ok, fully qualified
  102. my $foo = 10; # ok, my() var
  103. local $baz = 9; # blows up, $baz not declared before
  104. package Cinna;
  105. our $bar; # Declares $bar in current package
  106. $bar = 'HgS'; # ok, global declared via pragma
  107. The local() generated a compile-time error because you just touched a global
  108. name without fully qualifying it.
  109. Because of their special use by sort(), the variables $a and $b are
  110. exempted from this check.
  111. =item C<strict subs>
  112. This disables the poetry optimization, generating a compile-time error if
  113. you try to use a bareword identifier that's not a subroutine, unless it
  114. is a simple identifier (no colons) and that it appears in curly braces or
  115. on the left hand side of the C<< => >> symbol.
  116. use strict 'subs';
  117. $SIG{PIPE} = Plumber; # blows up
  118. $SIG{PIPE} = "Plumber"; # fine: quoted string is always ok
  119. $SIG{PIPE} = \&Plumber; # preferred form
  120. =back
  121. See L<perlmodlib/Pragmatic Modules>.
  122. =head1 HISTORY
  123. C<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted
  124. compound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or
  125. inside curlies), but without forcing it always to a literal string.
  126. Starting with Perl 5.8.1 strict is strict about its restrictions:
  127. if unknown restrictions are used, the strict pragma will abort with
  128. Unknown 'strict' tag(s) '...'
  129. As of version 1.04 (Perl 5.10), strict verifies that it is used as
  130. "strict" to avoid the dreaded Strict trap on case insensitive file
  131. systems.
  132. =cut