1*0Sstevel@tonic-gatepackage strict; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate$strict::VERSION = "1.03"; 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gatemy %bitmask = ( 6*0Sstevel@tonic-gaterefs => 0x00000002, 7*0Sstevel@tonic-gatesubs => 0x00000200, 8*0Sstevel@tonic-gatevars => 0x00000400 9*0Sstevel@tonic-gate); 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gatesub bits { 12*0Sstevel@tonic-gate my $bits = 0; 13*0Sstevel@tonic-gate my @wrong; 14*0Sstevel@tonic-gate foreach my $s (@_) { 15*0Sstevel@tonic-gate push @wrong, $s unless exists $bitmask{$s}; 16*0Sstevel@tonic-gate $bits |= $bitmask{$s} || 0; 17*0Sstevel@tonic-gate } 18*0Sstevel@tonic-gate if (@wrong) { 19*0Sstevel@tonic-gate require Carp; 20*0Sstevel@tonic-gate Carp::croak("Unknown 'strict' tag(s) '@wrong'"); 21*0Sstevel@tonic-gate } 22*0Sstevel@tonic-gate $bits; 23*0Sstevel@tonic-gate} 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gatemy $default_bits = bits(qw(refs subs vars)); 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gatesub import { 28*0Sstevel@tonic-gate shift; 29*0Sstevel@tonic-gate $^H |= @_ ? bits(@_) : $default_bits; 30*0Sstevel@tonic-gate} 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gatesub unimport { 33*0Sstevel@tonic-gate shift; 34*0Sstevel@tonic-gate $^H &= ~ (@_ ? bits(@_) : $default_bits); 35*0Sstevel@tonic-gate} 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate1; 38*0Sstevel@tonic-gate__END__ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate=head1 NAME 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gatestrict - Perl pragma to restrict unsafe constructs 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate=head1 SYNOPSIS 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate use strict; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate use strict "vars"; 49*0Sstevel@tonic-gate use strict "refs"; 50*0Sstevel@tonic-gate use strict "subs"; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate use strict; 53*0Sstevel@tonic-gate no strict "vars"; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate=head1 DESCRIPTION 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gateIf no import list is supplied, all possible restrictions are assumed. 58*0Sstevel@tonic-gate(This is the safest mode to operate in, but is sometimes too strict for 59*0Sstevel@tonic-gatecasual programming.) Currently, there are three possible things to be 60*0Sstevel@tonic-gatestrict about: "subs", "vars", and "refs". 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate=over 6 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate=item C<strict refs> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gateThis generates a runtime error if you 67*0Sstevel@tonic-gateuse symbolic references (see L<perlref>). 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate use strict 'refs'; 70*0Sstevel@tonic-gate $ref = \$foo; 71*0Sstevel@tonic-gate print $$ref; # ok 72*0Sstevel@tonic-gate $ref = "foo"; 73*0Sstevel@tonic-gate print $$ref; # runtime error; normally ok 74*0Sstevel@tonic-gate $file = "STDOUT"; 75*0Sstevel@tonic-gate print $file "Hi!"; # error; note: no comma after $file 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gateThere is one exception to this rule: 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate $bar = \&{'foo'}; 80*0Sstevel@tonic-gate &$bar; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gateis allowed so that C<goto &$AUTOLOAD> would not break under stricture. 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate=item C<strict vars> 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateThis generates a compile-time error if you access a variable that wasn't 88*0Sstevel@tonic-gatedeclared via C<our> or C<use vars>, 89*0Sstevel@tonic-gatelocalized via C<my()>, or wasn't fully qualified. Because this is to avoid 90*0Sstevel@tonic-gatevariable suicide problems and subtle dynamic scoping issues, a merely 91*0Sstevel@tonic-gatelocal() variable isn't good enough. See L<perlfunc/my> and 92*0Sstevel@tonic-gateL<perlfunc/local>. 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate use strict 'vars'; 95*0Sstevel@tonic-gate $X::foo = 1; # ok, fully qualified 96*0Sstevel@tonic-gate my $foo = 10; # ok, my() var 97*0Sstevel@tonic-gate local $foo = 9; # blows up 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate package Cinna; 100*0Sstevel@tonic-gate our $bar; # Declares $bar in current package 101*0Sstevel@tonic-gate $bar = 'HgS'; # ok, global declared via pragma 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gateThe local() generated a compile-time error because you just touched a global 104*0Sstevel@tonic-gatename without fully qualifying it. 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gateBecause of their special use by sort(), the variables $a and $b are 107*0Sstevel@tonic-gateexempted from this check. 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate=item C<strict subs> 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gateThis disables the poetry optimization, generating a compile-time error if 112*0Sstevel@tonic-gateyou try to use a bareword identifier that's not a subroutine, unless it 113*0Sstevel@tonic-gateis a simple identifier (no colons) and that it appears in curly braces or 114*0Sstevel@tonic-gateon the left hand side of the C<< => >> symbol. 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate use strict 'subs'; 117*0Sstevel@tonic-gate $SIG{PIPE} = Plumber; # blows up 118*0Sstevel@tonic-gate $SIG{PIPE} = "Plumber"; # just fine: quoted string is always ok 119*0Sstevel@tonic-gate $SIG{PIPE} = \&Plumber; # preferred form 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate=back 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gateSee L<perlmodlib/Pragmatic Modules>. 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate=head1 HISTORY 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gateC<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted 128*0Sstevel@tonic-gatecompound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or 129*0Sstevel@tonic-gateinside curlies), but without forcing it always to a literal string. 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gateStarting with Perl 5.8.1 strict is strict about its restrictions: 132*0Sstevel@tonic-gateif unknown restrictions are used, the strict pragma will abort with 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate Unknown 'strict' tag(s) '...' 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate=cut 137