1N/Apackage strict;
1N/A
1N/A$strict::VERSION = "1.03";
1N/A
1N/Amy %bitmask = (
1N/Arefs => 0x00000002,
1N/Asubs => 0x00000200,
1N/Avars => 0x00000400
1N/A);
1N/A
1N/Asub bits {
1N/A my $bits = 0;
1N/A my @wrong;
1N/A foreach my $s (@_) {
1N/A push @wrong, $s unless exists $bitmask{$s};
1N/A $bits |= $bitmask{$s} || 0;
1N/A }
1N/A if (@wrong) {
1N/A require Carp;
1N/A Carp::croak("Unknown 'strict' tag(s) '@wrong'");
1N/A }
1N/A $bits;
1N/A}
1N/A
1N/Amy $default_bits = bits(qw(refs subs vars));
1N/A
1N/Asub import {
1N/A shift;
1N/A $^H |= @_ ? bits(@_) : $default_bits;
1N/A}
1N/A
1N/Asub unimport {
1N/A shift;
1N/A $^H &= ~ (@_ ? bits(@_) : $default_bits);
1N/A}
1N/A
1N/A1;
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/Astrict - Perl pragma to restrict unsafe constructs
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use strict;
1N/A
1N/A use strict "vars";
1N/A use strict "refs";
1N/A use strict "subs";
1N/A
1N/A use strict;
1N/A no strict "vars";
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AIf no import list is supplied, all possible restrictions are assumed.
1N/A(This is the safest mode to operate in, but is sometimes too strict for
1N/Acasual programming.) Currently, there are three possible things to be
1N/Astrict about: "subs", "vars", and "refs".
1N/A
1N/A=over 6
1N/A
1N/A=item C<strict refs>
1N/A
1N/AThis generates a runtime error if you
1N/Ause symbolic references (see L<perlref>).
1N/A
1N/A use strict 'refs';
1N/A $ref = \$foo;
1N/A print $$ref; # ok
1N/A $ref = "foo";
1N/A print $$ref; # runtime error; normally ok
1N/A $file = "STDOUT";
1N/A print $file "Hi!"; # error; note: no comma after $file
1N/A
1N/AThere is one exception to this rule:
1N/A
1N/A $bar = \&{'foo'};
1N/A &$bar;
1N/A
1N/Ais allowed so that C<goto &$AUTOLOAD> would not break under stricture.
1N/A
1N/A
1N/A=item C<strict vars>
1N/A
1N/AThis generates a compile-time error if you access a variable that wasn't
1N/Adeclared via C<our> or C<use vars>,
1N/Alocalized via C<my()>, or wasn't fully qualified. Because this is to avoid
1N/Avariable suicide problems and subtle dynamic scoping issues, a merely
1N/Alocal() variable isn't good enough. See L<perlfunc/my> and
1N/AL<perlfunc/local>.
1N/A
1N/A use strict 'vars';
1N/A $X::foo = 1; # ok, fully qualified
1N/A my $foo = 10; # ok, my() var
1N/A local $foo = 9; # blows up
1N/A
1N/A package Cinna;
1N/A our $bar; # Declares $bar in current package
1N/A $bar = 'HgS'; # ok, global declared via pragma
1N/A
1N/AThe local() generated a compile-time error because you just touched a global
1N/Aname without fully qualifying it.
1N/A
1N/ABecause of their special use by sort(), the variables $a and $b are
1N/Aexempted from this check.
1N/A
1N/A=item C<strict subs>
1N/A
1N/AThis disables the poetry optimization, generating a compile-time error if
1N/Ayou try to use a bareword identifier that's not a subroutine, unless it
1N/Ais a simple identifier (no colons) and that it appears in curly braces or
1N/Aon the left hand side of the C<< => >> symbol.
1N/A
1N/A use strict 'subs';
1N/A $SIG{PIPE} = Plumber; # blows up
1N/A $SIG{PIPE} = "Plumber"; # just fine: quoted string is always ok
1N/A $SIG{PIPE} = \&Plumber; # preferred form
1N/A
1N/A=back
1N/A
1N/ASee L<perlmodlib/Pragmatic Modules>.
1N/A
1N/A=head1 HISTORY
1N/A
1N/AC<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted
1N/Acompound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or
1N/Ainside curlies), but without forcing it always to a literal string.
1N/A
1N/AStarting with Perl 5.8.1 strict is strict about its restrictions:
1N/Aif unknown restrictions are used, the strict pragma will abort with
1N/A
1N/A Unknown 'strict' tag(s) '...'
1N/A
1N/A=cut