typos and other minor things
[p5sagit/p5-mst-13.2.git] / lib / strict.pm
CommitLineData
a0d0e21e 1package strict;
2
4b2eca7a 3$strict::VERSION = "1.02";
4
5my %bitmask = (
6refs => 0x00000002,
7subs => 0x00000200,
8vars => 0x00000400
9);
10
11sub bits {
12 my $bits = 0;
13 my @wrong;
14 foreach my $s (@_) {
15 push @wrong, $s unless exists $bitmask{$s};
16 $bits |= $bitmask{$s} || 0;
17 }
18 if (@wrong) {
4b2eca7a 19 require Carp;
e279cb0b 20 Carp::croak("Unknown 'strict' tag(s) '@wrong'");
4b2eca7a 21 }
22 $bits;
23}
24
25sub import {
26 shift;
27 $^H |= bits(@_ ? @_ : qw(refs subs vars));
28}
29
30sub unimport {
31 shift;
32 $^H &= ~ bits(@_ ? @_ : qw(refs subs vars));
33}
34
351;
36__END__
37
f06db76b 38=head1 NAME
39
40strict - Perl pragma to restrict unsafe constructs
41
42=head1 SYNOPSIS
43
44 use strict;
45
46 use strict "vars";
47 use strict "refs";
48 use strict "subs";
49
50 use strict;
51 no strict "vars";
52
53=head1 DESCRIPTION
54
55If no import list is supplied, all possible restrictions are assumed.
56(This is the safest mode to operate in, but is sometimes too strict for
55497cff 57casual programming.) Currently, there are three possible things to be
58strict about: "subs", "vars", and "refs".
f06db76b 59
60=over 6
61
62=item C<strict refs>
63
64This generates a runtime error if you
65use symbolic references (see L<perlref>).
66
67 use strict 'refs';
68 $ref = \$foo;
69 print $$ref; # ok
70 $ref = "foo";
71 print $$ref; # runtime error; normally ok
d6fd2b02 72 $file = "STDOUT";
73 print $file "Hi!"; # error; note: no comma after $file
f06db76b 74
cec39fc8 75There is one exception to this rule:
76
77 $bar = \&{'foo'};
78 &$bar;
79
80is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
81
82
f06db76b 83=item C<strict vars>
84
85This generates a compile-time error if you access a variable that wasn't
17f410f9 86declared via "our" or C<use vars>,
87localized via C<my()>, or wasn't fully qualified. Because this is to avoid
f06db76b 88variable suicide problems and subtle dynamic scoping issues, a merely
89local() variable isn't good enough. See L<perlfunc/my> and
90L<perlfunc/local>.
91
92 use strict 'vars';
93 $X::foo = 1; # ok, fully qualified
94 my $foo = 10; # ok, my() var
95 local $foo = 9; # blows up
96
535b5725 97 package Cinna;
17f410f9 98 our $bar; # Declares $bar in current package
535b5725 99 $bar = 'HgS'; # ok, global declared via pragma
100
f06db76b 101The local() generated a compile-time error because you just touched a global
102name without fully qualifying it.
103
3ce0d271 104Because of their special use by sort(), the variables $a and $b are
105exempted from this check.
106
f06db76b 107=item C<strict subs>
108
cb1a09d0 109This disables the poetry optimization, generating a compile-time error if
110you try to use a bareword identifier that's not a subroutine, unless it
1fef88e7 111appears in curly braces or on the left hand side of the "=E<gt>" symbol.
cb1a09d0 112
f06db76b 113
114 use strict 'subs';
115 $SIG{PIPE} = Plumber; # blows up
cb1a09d0 116 $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok
117 $SIG{PIPE} = \&Plumber; # preferred form
118
119
f06db76b 120
121=back
122
ee580363 123See L<perlmodlib/Pragmatic Modules>.
f06db76b 124
f06db76b 125=cut