Commit | Line | Data |
a0d0e21e |
1 | package strict; |
2 | |
4b2eca7a |
3 | $strict::VERSION = "1.02"; |
4 | |
5 | my %bitmask = ( |
6 | refs => 0x00000002, |
7 | subs => 0x00000200, |
8 | vars => 0x00000400 |
9 | ); |
10 | |
11 | sub 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 | |
25 | sub import { |
26 | shift; |
27 | $^H |= bits(@_ ? @_ : qw(refs subs vars)); |
28 | } |
29 | |
30 | sub unimport { |
31 | shift; |
32 | $^H &= ~ bits(@_ ? @_ : qw(refs subs vars)); |
33 | } |
34 | |
35 | 1; |
36 | __END__ |
37 | |
f06db76b |
38 | =head1 NAME |
39 | |
40 | strict - 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 | |
55 | If 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 |
57 | casual programming.) Currently, there are three possible things to be |
58 | strict about: "subs", "vars", and "refs". |
f06db76b |
59 | |
60 | =over 6 |
61 | |
62 | =item C<strict refs> |
63 | |
64 | This generates a runtime error if you |
65 | use 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 |
75 | There is one exception to this rule: |
76 | |
77 | $bar = \&{'foo'}; |
78 | &$bar; |
79 | |
80 | is allowed so that C<goto &$AUTOLOAD> would not break under stricture. |
81 | |
82 | |
f06db76b |
83 | =item C<strict vars> |
84 | |
85 | This generates a compile-time error if you access a variable that wasn't |
17f410f9 |
86 | declared via "our" or C<use vars>, |
87 | localized via C<my()>, or wasn't fully qualified. Because this is to avoid |
f06db76b |
88 | variable suicide problems and subtle dynamic scoping issues, a merely |
89 | local() variable isn't good enough. See L<perlfunc/my> and |
90 | L<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 |
101 | The local() generated a compile-time error because you just touched a global |
102 | name without fully qualifying it. |
103 | |
3ce0d271 |
104 | Because of their special use by sort(), the variables $a and $b are |
105 | exempted from this check. |
106 | |
f06db76b |
107 | =item C<strict subs> |
108 | |
cb1a09d0 |
109 | This disables the poetry optimization, generating a compile-time error if |
110 | you try to use a bareword identifier that's not a subroutine, unless it |
1fef88e7 |
111 | appears 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 |
123 | See L<perlmodlib/Pragmatic Modules>. |
f06db76b |
124 | |
f06db76b |
125 | =cut |