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 | |
37 | ||
38 | =item C<strict vars> | |
39 | ||
40 | This generates a compile-time error if you access a variable that wasn't | |
41 | localized via C<my()> or wasn't fully qualified. Because this is to avoid | |
42 | variable suicide problems and subtle dynamic scoping issues, a merely | |
43 | local() variable isn't good enough. See L<perlfunc/my> and | |
44 | L<perlfunc/local>. | |
45 | ||
46 | use strict 'vars'; | |
47 | $X::foo = 1; # ok, fully qualified | |
48 | my $foo = 10; # ok, my() var | |
49 | local $foo = 9; # blows up | |
50 | ||
51 | The local() generated a compile-time error because you just touched a global | |
52 | name without fully qualifying it. | |
53 | ||
54 | =item C<strict subs> | |
55 | ||
cb1a09d0 AD |
56 | This disables the poetry optimization, generating a compile-time error if |
57 | you try to use a bareword identifier that's not a subroutine, unless it | |
1fef88e7 | 58 | appears in curly braces or on the left hand side of the "=E<gt>" symbol. |
cb1a09d0 | 59 | |
f06db76b AD |
60 | |
61 | use strict 'subs'; | |
62 | $SIG{PIPE} = Plumber; # blows up | |
cb1a09d0 AD |
63 | $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok |
64 | $SIG{PIPE} = \&Plumber; # preferred form | |
65 | ||
66 | ||
f06db76b AD |
67 | |
68 | =back | |
69 | ||
70 | See L<perlmod/Pragmatic Modules>. | |
71 | ||
72 | ||
73 | =cut | |
74 | ||
a0d0e21e LW |
75 | sub bits { |
76 | my $bits = 0; | |
7a4c00b4 | 77 | my $sememe; |
a0d0e21e | 78 | foreach $sememe (@_) { |
7a4c00b4 | 79 | $bits |= 0x00000002, next if $sememe eq 'refs'; |
80 | $bits |= 0x00000200, next if $sememe eq 'subs'; | |
81 | $bits |= 0x00000400, next if $sememe eq 'vars'; | |
a0d0e21e LW |
82 | } |
83 | $bits; | |
84 | } | |
85 | ||
86 | sub import { | |
87 | shift; | |
55497cff | 88 | $^H |= bits(@_ ? @_ : qw(refs subs vars)); |
a0d0e21e LW |
89 | } |
90 | ||
91 | sub unimport { | |
92 | shift; | |
55497cff | 93 | $^H &= ~ bits(@_ ? @_ : qw(refs subs vars)); |
a0d0e21e LW |
94 | } |
95 | ||
96 | 1; |