1package strict; 2 3$strict::VERSION = "1.13"; 4 5my ( %bitmask, %explicit_bitmask ); 6 7BEGIN { 8 # Verify that we're called correctly so that strictures will work. 9 # Can't use Carp, since Carp uses us! 10 # see also warnings.pm. 11 die sprintf "Incorrect use of pragma '%s' at %s line %d.\n", __PACKAGE__, +(caller)[1,2] 12 if __FILE__ !~ ( '(?x) \b '.__PACKAGE__.' \.pmc? \z' ) 13 && __FILE__ =~ ( '(?x) \b (?i:'.__PACKAGE__.') \.pmc? \z' ); 14 15 %bitmask = ( 16 refs => 0x00000002, 17 subs => 0x00000200, 18 vars => 0x00000400, 19 ); 20 21 %explicit_bitmask = ( 22 refs => 0x00000020, 23 subs => 0x00000040, 24 vars => 0x00000080, 25 ); 26 27 my $bits = 0; 28 $bits |= $_ for values %bitmask; 29 30 my $inline_all_bits = $bits; 31 *all_bits = sub () { $inline_all_bits }; 32 33 $bits = 0; 34 $bits |= $_ for values %explicit_bitmask; 35 36 my $inline_all_explicit_bits = $bits; 37 *all_explicit_bits = sub () { $inline_all_explicit_bits }; 38} 39 40sub bits { 41 my $bits = 0; 42 my @wrong; 43 foreach my $s (@_) { 44 if (exists $bitmask{$s}) { 45 $^H |= $explicit_bitmask{$s}; 46 47 $bits |= $bitmask{$s}; 48 } 49 else { 50 push @wrong, $s; 51 } 52 } 53 if (@wrong) { 54 require Carp; 55 Carp::croak("Unknown 'strict' tag(s) '@wrong'"); 56 } 57 $bits; 58} 59 60sub import { 61 shift; 62 $^H |= @_ ? &bits : all_bits | all_explicit_bits; 63} 64 65sub unimport { 66 shift; 67 68 if (@_) { 69 $^H &= ~&bits; 70 } 71 else { 72 $^H &= ~all_bits; 73 $^H |= all_explicit_bits; 74 } 75} 76 771; 78__END__ 79 80=head1 NAME 81 82strict - Perl pragma to restrict unsafe constructs 83 84=head1 SYNOPSIS 85 86 use strict; 87 88 use strict "vars"; 89 use strict "refs"; 90 use strict "subs"; 91 92 use strict; 93 no strict "vars"; 94 95=head1 DESCRIPTION 96 97The C<strict> pragma disables certain Perl expressions that could behave 98unexpectedly or are difficult to debug, turning them into errors. The 99effect of this pragma is limited to the current file or scope block. 100 101If no import list is supplied, all possible restrictions are assumed. 102(This is the safest mode to operate in, but is sometimes too strict for 103casual programming.) Currently, there are three possible things to be 104strict about: "subs", "vars", and "refs". 105 106=over 6 107 108=item C<strict refs> 109 110This generates a runtime error if you 111use symbolic references (see L<perlref>). 112 113 use strict 'refs'; 114 $ref = \$foo; 115 print $$ref; # ok 116 $ref = "foo"; 117 print $$ref; # runtime error; normally ok 118 $file = "STDOUT"; 119 print $file "Hi!"; # error; note: no comma after $file 120 121There is one exception to this rule: 122 123 $bar = \&{'foo'}; 124 &$bar; 125 126is allowed so that C<goto &$AUTOLOAD> would not break under stricture. 127 128 129=item C<strict vars> 130 131This generates a compile-time error if you access a variable that was 132neither explicitly declared (using any of C<my>, C<our>, C<state>, or C<use 133vars>) nor fully qualified. (Because this is to avoid variable suicide 134problems and subtle dynamic scoping issues, a merely C<local> variable isn't 135good enough.) See L<perlfunc/my>, L<perlfunc/our>, L<perlfunc/state>, 136L<perlfunc/local>, and L<vars>. 137 138 use strict 'vars'; 139 $X::foo = 1; # ok, fully qualified 140 my $foo = 10; # ok, my() var 141 local $baz = 9; # blows up, $baz not declared before 142 143 package Cinna; 144 our $bar; # Declares $bar in current package 145 $bar = 'HgS'; # ok, global declared via pragma 146 147The local() generated a compile-time error because you just touched a global 148name without fully qualifying it. 149 150Because of their special use by sort(), the variables $a and $b are 151exempted from this check. 152 153=item C<strict subs> 154 155This disables the poetry optimization, generating a compile-time error if 156you try to use a bareword identifier that's not a subroutine, unless it 157is a simple identifier (no colons) and that it appears in curly braces, 158on the left hand side of the C<< => >> symbol, or has the unary minus 159operator applied to it. 160 161 use strict 'subs'; 162 $SIG{PIPE} = Plumber; # blows up 163 $SIG{PIPE} = "Plumber"; # fine: quoted string is always ok 164 $SIG{PIPE} = \&Plumber; # preferred form 165 166=back 167 168See L<perlmodlib/Pragmatic Modules>. 169 170=head1 HISTORY 171 172C<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted 173compound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or 174inside curlies), but without forcing it always to a literal string. 175 176Starting with Perl 5.8.1 strict is strict about its restrictions: 177if unknown restrictions are used, the strict pragma will abort with 178 179 Unknown 'strict' tag(s) '...' 180 181As of version 1.04 (Perl 5.10), strict verifies that it is used as 182"strict" to avoid the dreaded Strict trap on case insensitive file 183systems. 184 185Beginning with Perl 5.12, use of "use VERSION" (where VERSION >= 5.11.0) now 186lexically enables strictures just like "use strict" (in addition to the normal 187"use VERSION" effects and features.) In other words, "use v5.011" or higher 188now implies "use strict" automatically, as noted in 189L<perl5120delta/"Implicit strictures"> and L<C<use VERSION>|perlfunc/use VERSION>. 190 191=cut 192