Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | package strict; |
2 | ||
f06db76b AD |
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 AD |
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 | |
d6fd2b02 GS |
37 | $file = "STDOUT"; |
38 | print $file "Hi!"; # error; note: no comma after $file | |
f06db76b | 39 | |
cec39fc8 RS |
40 | There is one exception to this rule: |
41 | ||
42 | $bar = \&{'foo'}; | |
43 | &$bar; | |
44 | ||
45 | is allowed so that C<goto &$AUTOLOAD> would not break under stricture. | |
46 | ||
47 | ||
f06db76b AD |
48 | =item C<strict vars> |
49 | ||
50 | This generates a compile-time error if you access a variable that wasn't | |
17f410f9 GS |
51 | declared via "our" or C<use vars>, |
52 | localized via C<my()>, or wasn't fully qualified. Because this is to avoid | |
f06db76b AD |
53 | variable suicide problems and subtle dynamic scoping issues, a merely |
54 | local() variable isn't good enough. See L<perlfunc/my> and | |
55 | L<perlfunc/local>. | |
56 | ||
57 | use strict 'vars'; | |
58 | $X::foo = 1; # ok, fully qualified | |
59 | my $foo = 10; # ok, my() var | |
60 | local $foo = 9; # blows up | |
61 | ||
535b5725 | 62 | package Cinna; |
17f410f9 | 63 | our $bar; # Declares $bar in current package |
535b5725 TP |
64 | $bar = 'HgS'; # ok, global declared via pragma |
65 | ||
f06db76b AD |
66 | The local() generated a compile-time error because you just touched a global |
67 | name without fully qualifying it. | |
68 | ||
3ce0d271 GS |
69 | Because of their special use by sort(), the variables $a and $b are |
70 | exempted from this check. | |
71 | ||
f06db76b AD |
72 | =item C<strict subs> |
73 | ||
cb1a09d0 AD |
74 | This disables the poetry optimization, generating a compile-time error if |
75 | you try to use a bareword identifier that's not a subroutine, unless it | |
1fef88e7 | 76 | appears in curly braces or on the left hand side of the "=E<gt>" symbol. |
cb1a09d0 | 77 | |
f06db76b AD |
78 | |
79 | use strict 'subs'; | |
80 | $SIG{PIPE} = Plumber; # blows up | |
cb1a09d0 AD |
81 | $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok |
82 | $SIG{PIPE} = \&Plumber; # preferred form | |
83 | ||
84 | ||
f06db76b AD |
85 | |
86 | =back | |
87 | ||
ee580363 | 88 | See L<perlmodlib/Pragmatic Modules>. |
f06db76b AD |
89 | |
90 | ||
91 | =cut | |
92 | ||
d6a466d7 | 93 | $strict::VERSION = "1.02"; |
4682965a MB |
94 | |
95 | my %bitmask = ( | |
96 | refs => 0x00000002, | |
97 | subs => 0x00000200, | |
98 | vars => 0x00000400 | |
99 | ); | |
100 | ||
a0d0e21e LW |
101 | sub bits { |
102 | my $bits = 0; | |
20408e3c | 103 | foreach my $s (@_){ $bits |= $bitmask{$s} || 0; }; |
a0d0e21e LW |
104 | $bits; |
105 | } | |
106 | ||
107 | sub import { | |
108 | shift; | |
55497cff | 109 | $^H |= bits(@_ ? @_ : qw(refs subs vars)); |
a0d0e21e LW |
110 | } |
111 | ||
112 | sub unimport { | |
113 | shift; | |
55497cff | 114 | $^H &= ~ bits(@_ ? @_ : qw(refs subs vars)); |
a0d0e21e LW |
115 | } |
116 | ||
117 | 1; |