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