perl 5.003_06: pod/perlcall.pod pod/perldata.pod pod/perldebug.pod pod/perlembed...
[p5sagit/p5-mst-13.2.git] / lib / strict.pm
CommitLineData
a0d0e21e 1package strict;
2
f06db76b 3=head1 NAME
4
5strict - 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
21If 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 23casual programming.) Currently, there are four possible things to be
24strict about: "subs", "vars", "refs", and "untie".
f06db76b 25
26=over 6
27
28=item C<strict refs>
29
30This generates a runtime error if you
31use 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
41This generates a compile-time error if you access a variable that wasn't
42localized via C<my()> or wasn't fully qualified. Because this is to avoid
43variable suicide problems and subtle dynamic scoping issues, a merely
44local() variable isn't good enough. See L<perlfunc/my> and
45L<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
52The local() generated a compile-time error because you just touched a global
53name without fully qualifying it.
54
55=item C<strict subs>
56
cb1a09d0 57This disables the poetry optimization, generating a compile-time error if
58you try to use a bareword identifier that's not a subroutine, unless it
1fef88e7 59appears in curly braces or on the left hand side of the "=E<gt>" symbol.
cb1a09d0 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
71This generates a runtime error if any references to the object returned
72by C<tie> (or C<tied>) still exist when C<untie> is called. Note that
73to get this strict behaviour, the C<use strict 'untie'> statement must
74be in the same scope as the C<untie>. See L<perlfunc/tie>,
75L<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
89See L<perlmod/Pragmatic Modules>.
90
91
92=cut
93
a0d0e21e 94sub 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
105sub import {
106 shift;
4a2eeee9 107 $^H |= bits(@_ ? @_ : qw(refs subs vars untie));
a0d0e21e 108}
109
110sub unimport {
111 shift;
4a2eeee9 112 $^H &= ~ bits(@_ ? @_ : qw(refs subs vars untie));
a0d0e21e 113}
114
1151;