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