Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | package strict; |
2 | ||
1c389bff | 3 | $strict::VERSION = "1.07"; |
e3def60f JV |
4 | |
5 | # Verify that we're called correctly so that strictures will work. | |
5108dc18 | 6 | unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) { |
e3def60f JV |
7 | # Can't use Carp, since Carp uses us! |
8 | my (undef, $f, $l) = caller; | |
5108dc18 | 9 | die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n"); |
e3def60f | 10 | } |
4b2eca7a NC |
11 | |
12 | my %bitmask = ( | |
13 | refs => 0x00000002, | |
14 | subs => 0x00000200, | |
15 | vars => 0x00000400 | |
16 | ); | |
d1718a7c | 17 | my %explicit_bitmask = ( |
894ea76b FC |
18 | refs => 0x00000020, |
19 | subs => 0x00000040, | |
20 | vars => 0x00000080 | |
d1718a7c | 21 | ); |
4b2eca7a NC |
22 | |
23 | sub bits { | |
24 | my $bits = 0; | |
25 | my @wrong; | |
26 | foreach my $s (@_) { | |
b50b2058 | 27 | if (exists $bitmask{$s}) { |
d1718a7c | 28 | $^H |= $explicit_bitmask{$s}; |
b50b2058 FC |
29 | } |
30 | else { push @wrong, $s }; | |
4b2eca7a NC |
31 | $bits |= $bitmask{$s} || 0; |
32 | } | |
33 | if (@wrong) { | |
4b2eca7a | 34 | require Carp; |
e279cb0b | 35 | Carp::croak("Unknown 'strict' tag(s) '@wrong'"); |
4b2eca7a NC |
36 | } |
37 | $bits; | |
38 | } | |
39 | ||
b50b2058 | 40 | my @default_bits = qw(refs subs vars); |
08d31bcd | 41 | |
4b2eca7a NC |
42 | sub import { |
43 | shift; | |
b50b2058 | 44 | $^H |= bits(@_ ? @_ : @default_bits); |
4b2eca7a NC |
45 | } |
46 | ||
47 | sub unimport { | |
48 | shift; | |
b50b2058 | 49 | $^H &= ~ bits(@_ ? @_ : @default_bits); |
4b2eca7a NC |
50 | } |
51 | ||
52 | 1; | |
53 | __END__ | |
54 | ||
f06db76b AD |
55 | =head1 NAME |
56 | ||
57 | strict - Perl pragma to restrict unsafe constructs | |
58 | ||
59 | =head1 SYNOPSIS | |
60 | ||
61 | use strict; | |
62 | ||
63 | use strict "vars"; | |
64 | use strict "refs"; | |
65 | use strict "subs"; | |
66 | ||
67 | use strict; | |
68 | no strict "vars"; | |
69 | ||
70 | =head1 DESCRIPTION | |
71 | ||
72 | If no import list is supplied, all possible restrictions are assumed. | |
73 | (This is the safest mode to operate in, but is sometimes too strict for | |
55497cff | 74 | casual programming.) Currently, there are three possible things to be |
75 | strict about: "subs", "vars", and "refs". | |
f06db76b AD |
76 | |
77 | =over 6 | |
78 | ||
79 | =item C<strict refs> | |
80 | ||
81 | This generates a runtime error if you | |
82 | use symbolic references (see L<perlref>). | |
83 | ||
84 | use strict 'refs'; | |
85 | $ref = \$foo; | |
86 | print $$ref; # ok | |
87 | $ref = "foo"; | |
88 | print $$ref; # runtime error; normally ok | |
d6fd2b02 GS |
89 | $file = "STDOUT"; |
90 | print $file "Hi!"; # error; note: no comma after $file | |
f06db76b | 91 | |
cec39fc8 RS |
92 | There is one exception to this rule: |
93 | ||
94 | $bar = \&{'foo'}; | |
95 | &$bar; | |
96 | ||
97 | is allowed so that C<goto &$AUTOLOAD> would not break under stricture. | |
98 | ||
99 | ||
f06db76b AD |
100 | =item C<strict vars> |
101 | ||
657c2f9b AC |
102 | This generates a compile-time error if you access a variable that was |
103 | neither explicitly declared (using any of C<my>, C<our>, C<state>, or C<use | |
104 | vars>) nor fully qualified. (Because this is to avoid variable suicide | |
105 | problems and subtle dynamic scoping issues, a merely C<local> variable isn't | |
106 | good enough.) See L<perlfunc/my>, L<perlfunc/our>, L<perlfunc/state>, | |
107 | L<perlfunc/local>, and L<vars>. | |
f06db76b AD |
108 | |
109 | use strict 'vars'; | |
110 | $X::foo = 1; # ok, fully qualified | |
111 | my $foo = 10; # ok, my() var | |
97631e60 | 112 | local $baz = 9; # blows up, $baz not declared before |
f06db76b | 113 | |
535b5725 | 114 | package Cinna; |
17f410f9 | 115 | our $bar; # Declares $bar in current package |
535b5725 TP |
116 | $bar = 'HgS'; # ok, global declared via pragma |
117 | ||
f06db76b AD |
118 | The local() generated a compile-time error because you just touched a global |
119 | name without fully qualifying it. | |
120 | ||
3ce0d271 GS |
121 | Because of their special use by sort(), the variables $a and $b are |
122 | exempted from this check. | |
123 | ||
f06db76b AD |
124 | =item C<strict subs> |
125 | ||
cb1a09d0 AD |
126 | This disables the poetry optimization, generating a compile-time error if |
127 | you try to use a bareword identifier that's not a subroutine, unless it | |
d66e832e RGS |
128 | is a simple identifier (no colons) and that it appears in curly braces or |
129 | on the left hand side of the C<< => >> symbol. | |
f06db76b AD |
130 | |
131 | use strict 'subs'; | |
132 | $SIG{PIPE} = Plumber; # blows up | |
5438961c | 133 | $SIG{PIPE} = "Plumber"; # just fine: quoted string is always ok |
cb1a09d0 AD |
134 | $SIG{PIPE} = \&Plumber; # preferred form |
135 | ||
f06db76b AD |
136 | =back |
137 | ||
ee580363 | 138 | See L<perlmodlib/Pragmatic Modules>. |
f06db76b | 139 | |
d66e832e RGS |
140 | =head1 HISTORY |
141 | ||
cbbb4974 | 142 | C<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted |
d66e832e RGS |
143 | compound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or |
144 | inside curlies), but without forcing it always to a literal string. | |
145 | ||
cbbb4974 JH |
146 | Starting with Perl 5.8.1 strict is strict about its restrictions: |
147 | if unknown restrictions are used, the strict pragma will abort with | |
148 | ||
149 | Unknown 'strict' tag(s) '...' | |
150 | ||
e3def60f JV |
151 | As of version 1.04 (Perl 5.10), strict verifies that it is used as |
152 | "strict" to avoid the dreaded Strict trap on case insensitive file | |
153 | systems. | |
154 | ||
f06db76b | 155 | =cut |