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