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"; |
4a2eeee9 |
14 | use strict "untie"; |
f06db76b |
15 | |
16 | use strict; |
17 | no strict "vars"; |
18 | |
19 | =head1 DESCRIPTION |
20 | |
21 | If no import list is supplied, all possible restrictions are assumed. |
22 | (This is the safest mode to operate in, but is sometimes too strict for |
4a2eeee9 |
23 | casual programming.) Currently, there are four possible things to be |
24 | strict about: "subs", "vars", "refs", and "untie". |
f06db76b |
25 | |
26 | =over 6 |
27 | |
28 | =item C<strict refs> |
29 | |
30 | This generates a runtime error if you |
31 | use symbolic references (see L<perlref>). |
32 | |
33 | use strict 'refs'; |
34 | $ref = \$foo; |
35 | print $$ref; # ok |
36 | $ref = "foo"; |
37 | print $$ref; # runtime error; normally ok |
38 | |
39 | =item C<strict vars> |
40 | |
41 | This generates a compile-time error if you access a variable that wasn't |
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 | |
52 | The local() generated a compile-time error because you just touched a global |
53 | name without fully qualifying it. |
54 | |
55 | =item C<strict subs> |
56 | |
cb1a09d0 |
57 | This disables the poetry optimization, generating a compile-time error if |
58 | you try to use a bareword identifier that's not a subroutine, unless it |
59 | appears in curly braces or on the left hand side of the "=>" symbol. |
60 | |
f06db76b |
61 | |
62 | use strict 'subs'; |
63 | $SIG{PIPE} = Plumber; # blows up |
cb1a09d0 |
64 | $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok |
65 | $SIG{PIPE} = \&Plumber; # preferred form |
66 | |
67 | |
f06db76b |
68 | |
4a2eeee9 |
69 | =item C<strict untie> |
70 | |
71 | This generates a runtime error if any references to the object returned |
72 | by C<tie> (or C<tied>) still exist when C<untie> is called. Note that |
73 | to get this strict behaviour, the C<use strict 'untie'> statement must |
74 | be in the same scope as the C<untie>. See L<perlfunc/tie>, |
75 | L<perlfunc/untie>, L<perlfunc/tied> and L<perltie>. |
76 | |
77 | use strict 'untie'; |
78 | $a = tie %a, 'SOME_PKG'; |
79 | $b = tie %b, 'SOME_PKG'; |
80 | $b = 0; |
81 | tie %c, PKG; |
82 | $c = tied %c; |
83 | untie %a ; # blows up, $a is a valid object reference. |
84 | untie %b; # ok, $b is not a reference to the object. |
85 | untie %c ; # blows up, $c is a valid object reference. |
86 | |
f06db76b |
87 | =back |
88 | |
89 | See L<perlmod/Pragmatic Modules>. |
90 | |
91 | |
92 | =cut |
93 | |
a0d0e21e |
94 | sub bits { |
95 | my $bits = 0; |
96 | foreach $sememe (@_) { |
97 | $bits |= 0x00000002 if $sememe eq 'refs'; |
98 | $bits |= 0x00000200 if $sememe eq 'subs'; |
99 | $bits |= 0x00000400 if $sememe eq 'vars'; |
4a2eeee9 |
100 | $bits |= 0x00000800 if $sememe eq 'untie'; |
a0d0e21e |
101 | } |
102 | $bits; |
103 | } |
104 | |
105 | sub import { |
106 | shift; |
4a2eeee9 |
107 | $^H |= bits(@_ ? @_ : qw(refs subs vars untie)); |
a0d0e21e |
108 | } |
109 | |
110 | sub unimport { |
111 | shift; |
4a2eeee9 |
112 | $^H &= ~ bits(@_ ? @_ : qw(refs subs vars untie)); |
a0d0e21e |
113 | } |
114 | |
115 | 1; |