d1479ed865ee1dd4c627f6d08ec90728f628c70c
[p5sagit/p5-mst-13.2.git] / lib / strict.pm
1 package strict;
2
3 $strict::VERSION = "1.02";
4
5 my %bitmask = (
6 refs => 0x00000002,
7 subs => 0x00000200,
8 vars => 0x00000400
9 );
10
11 sub 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) {
19         require Carp;
20         Carp::croak("Unknown 'strict' tag(s) '@wrong'");
21     }
22     $bits;
23 }
24
25 sub import {
26     shift;
27     $^H |= bits(@_ ? @_ : qw(refs subs vars));
28 }
29
30 sub unimport {
31     shift;
32     $^H &= ~ bits(@_ ? @_ : qw(refs subs vars));
33 }
34
35 1;
36 __END__
37
38 =head1 NAME
39
40 strict - Perl pragma to restrict unsafe constructs
41
42 =head1 SYNOPSIS
43
44     use strict;
45
46     use strict "vars";
47     use strict "refs";
48     use strict "subs";
49
50     use strict;
51     no strict "vars";
52
53 =head1 DESCRIPTION
54
55 If no import list is supplied, all possible restrictions are assumed.
56 (This is the safest mode to operate in, but is sometimes too strict for
57 casual programming.)  Currently, there are three possible things to be
58 strict about:  "subs", "vars", and "refs".
59
60 =over 6
61
62 =item C<strict refs>
63
64 This generates a runtime error if you 
65 use symbolic references (see L<perlref>).
66
67     use strict 'refs';
68     $ref = \$foo;
69     print $$ref;        # ok
70     $ref = "foo";
71     print $$ref;        # runtime error; normally ok
72     $file = "STDOUT";
73     print $file "Hi!";  # error; note: no comma after $file
74
75 There is one exception to this rule:
76
77     $bar = \&{'foo'};
78     &$bar;
79
80 is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
81
82
83 =item C<strict vars>
84
85 This generates a compile-time error if you access a variable that wasn't
86 declared via "our" or C<use vars>,
87 localized via C<my()>, or wasn't fully qualified.  Because this is to avoid
88 variable suicide problems and subtle dynamic scoping issues, a merely
89 local() variable isn't good enough.  See L<perlfunc/my> and
90 L<perlfunc/local>.
91
92     use strict 'vars';
93     $X::foo = 1;         # ok, fully qualified
94     my $foo = 10;        # ok, my() var
95     local $foo = 9;      # blows up
96
97     package Cinna;
98     our $bar;                   # Declares $bar in current package
99     $bar = 'HgS';               # ok, global declared via pragma
100
101 The local() generated a compile-time error because you just touched a global
102 name without fully qualifying it.
103
104 Because of their special use by sort(), the variables $a and $b are
105 exempted from this check.
106
107 =item C<strict subs>
108
109 This disables the poetry optimization, generating a compile-time error if
110 you try to use a bareword identifier that's not a subroutine, unless it
111 appears in curly braces or on the left hand side of the "=E<gt>" symbol.
112
113
114     use strict 'subs';
115     $SIG{PIPE} = Plumber;       # blows up
116     $SIG{PIPE} = "Plumber";     # just fine: bareword in curlies always ok
117     $SIG{PIPE} = \&Plumber;     # preferred form
118
119
120
121 =back
122
123 See L<perlmodlib/Pragmatic Modules>.
124
125 =cut