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