This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Analysis of problems with mixed encoding case insensitive matches in regex engine.
[perl5.git] / lib / strict.pm
CommitLineData
a0d0e21e
LW
1package strict;
2
08d31bcd 3$strict::VERSION = "1.03";
4b2eca7a
NC
4
5my %bitmask = (
6refs => 0x00000002,
7subs => 0x00000200,
8vars => 0x00000400
9);
10
11sub bits {
12 my $bits = 0;
13 my @wrong;
14 foreach my $s (@_) {
15 push @wrong, $s unless exists $bitmask{$s};
16 $bits |= $bitmask{$s} || 0;
17 }
18 if (@wrong) {
4b2eca7a 19 require Carp;
e279cb0b 20 Carp::croak("Unknown 'strict' tag(s) '@wrong'");
4b2eca7a
NC
21 }
22 $bits;
23}
24
08d31bcd
JH
25my $default_bits = bits(qw(refs subs vars));
26
4b2eca7a
NC
27sub import {
28 shift;
08d31bcd 29 $^H |= @_ ? bits(@_) : $default_bits;
4b2eca7a
NC
30}
31
32sub unimport {
33 shift;
08d31bcd 34 $^H &= ~ (@_ ? bits(@_) : $default_bits);
4b2eca7a
NC
35}
36
371;
38__END__
39
f06db76b
AD
40=head1 NAME
41
42strict - Perl pragma to restrict unsafe constructs
43
44=head1 SYNOPSIS
45
46 use strict;
47
48 use strict "vars";
49 use strict "refs";
50 use strict "subs";
51
52 use strict;
53 no strict "vars";
54
55=head1 DESCRIPTION
56
57If no import list is supplied, all possible restrictions are assumed.
58(This is the safest mode to operate in, but is sometimes too strict for
55497cff 59casual programming.) Currently, there are three possible things to be
60strict about: "subs", "vars", and "refs".
f06db76b
AD
61
62=over 6
63
64=item C<strict refs>
65
66This generates a runtime error if you
67use symbolic references (see L<perlref>).
68
69 use strict 'refs';
70 $ref = \$foo;
71 print $$ref; # ok
72 $ref = "foo";
73 print $$ref; # runtime error; normally ok
d6fd2b02
GS
74 $file = "STDOUT";
75 print $file "Hi!"; # error; note: no comma after $file
f06db76b 76
cec39fc8
RS
77There is one exception to this rule:
78
79 $bar = \&{'foo'};
80 &$bar;
81
82is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
83
84
f06db76b
AD
85=item C<strict vars>
86
87This generates a compile-time error if you access a variable that wasn't
d66e832e 88declared via C<our> or C<use vars>,
17f410f9 89localized via C<my()>, or wasn't fully qualified. Because this is to avoid
f06db76b
AD
90variable suicide problems and subtle dynamic scoping issues, a merely
91local() variable isn't good enough. See L<perlfunc/my> and
92L<perlfunc/local>.
93
94 use strict 'vars';
95 $X::foo = 1; # ok, fully qualified
96 my $foo = 10; # ok, my() var
97 local $foo = 9; # blows up
98
535b5725 99 package Cinna;
17f410f9 100 our $bar; # Declares $bar in current package
535b5725
TP
101 $bar = 'HgS'; # ok, global declared via pragma
102
f06db76b
AD
103The local() generated a compile-time error because you just touched a global
104name without fully qualifying it.
105
3ce0d271
GS
106Because of their special use by sort(), the variables $a and $b are
107exempted from this check.
108
f06db76b
AD
109=item C<strict subs>
110
cb1a09d0
AD
111This disables the poetry optimization, generating a compile-time error if
112you try to use a bareword identifier that's not a subroutine, unless it
d66e832e
RGS
113is a simple identifier (no colons) and that it appears in curly braces or
114on the left hand side of the C<< => >> symbol.
f06db76b
AD
115
116 use strict 'subs';
117 $SIG{PIPE} = Plumber; # blows up
5438961c 118 $SIG{PIPE} = "Plumber"; # just fine: quoted string is always ok
cb1a09d0
AD
119 $SIG{PIPE} = \&Plumber; # preferred form
120
f06db76b
AD
121=back
122
ee580363 123See L<perlmodlib/Pragmatic Modules>.
f06db76b 124
d66e832e
RGS
125=head1 HISTORY
126
cbbb4974 127C<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted
d66e832e
RGS
128compound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or
129inside curlies), but without forcing it always to a literal string.
130
cbbb4974
JH
131Starting with Perl 5.8.1 strict is strict about its restrictions:
132if unknown restrictions are used, the strict pragma will abort with
133
134 Unknown 'strict' tag(s) '...'
135
f06db76b 136=cut