This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20000121.007] XXX documentation in man ExtUtils::MakeMaker
[perl5.git] / lib / strict.pm
CommitLineData
a0d0e21e
LW
1package strict;
2
f06db76b
AD
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";
14
15 use strict;
16 no strict "vars";
17
18=head1 DESCRIPTION
19
20If 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
PP
22casual programming.) Currently, there are three possible things to be
23strict about: "subs", "vars", and "refs".
f06db76b
AD
24
25=over 6
26
27=item C<strict refs>
28
29This generates a runtime error if you
30use 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
d6fd2b02
GS
37 $file = "STDOUT";
38 print $file "Hi!"; # error; note: no comma after $file
f06db76b
AD
39
40=item C<strict vars>
41
42This generates a compile-time error if you access a variable that wasn't
17f410f9
GS
43declared via "our" or C<use vars>,
44localized via C<my()>, or wasn't fully qualified. Because this is to avoid
f06db76b
AD
45variable suicide problems and subtle dynamic scoping issues, a merely
46local() variable isn't good enough. See L<perlfunc/my> and
47L<perlfunc/local>.
48
49 use strict 'vars';
50 $X::foo = 1; # ok, fully qualified
51 my $foo = 10; # ok, my() var
52 local $foo = 9; # blows up
53
535b5725 54 package Cinna;
17f410f9 55 our $bar; # Declares $bar in current package
535b5725
TP
56 $bar = 'HgS'; # ok, global declared via pragma
57
f06db76b
AD
58The local() generated a compile-time error because you just touched a global
59name without fully qualifying it.
60
3ce0d271
GS
61Because of their special use by sort(), the variables $a and $b are
62exempted from this check.
63
f06db76b
AD
64=item C<strict subs>
65
cb1a09d0
AD
66This disables the poetry optimization, generating a compile-time error if
67you try to use a bareword identifier that's not a subroutine, unless it
1fef88e7 68appears in curly braces or on the left hand side of the "=E<gt>" symbol.
cb1a09d0 69
f06db76b
AD
70
71 use strict 'subs';
72 $SIG{PIPE} = Plumber; # blows up
cb1a09d0
AD
73 $SIG{PIPE} = "Plumber"; # just fine: bareword in curlies always ok
74 $SIG{PIPE} = \&Plumber; # preferred form
75
76
f06db76b
AD
77
78=back
79
ee580363 80See L<perlmodlib/Pragmatic Modules>.
f06db76b
AD
81
82
83=cut
84
4682965a
MB
85$strict::VERSION = "1.01";
86
87my %bitmask = (
88refs => 0x00000002,
89subs => 0x00000200,
90vars => 0x00000400
91);
92
a0d0e21e
LW
93sub bits {
94 my $bits = 0;
20408e3c 95 foreach my $s (@_){ $bits |= $bitmask{$s} || 0; };
a0d0e21e
LW
96 $bits;
97}
98
99sub import {
100 shift;
55497cff 101 $^H |= bits(@_ ? @_ : qw(refs subs vars));
a0d0e21e
LW
102}
103
104sub unimport {
105 shift;
55497cff 106 $^H &= ~ bits(@_ ? @_ : qw(refs subs vars));
a0d0e21e
LW
107}
108
1091;