Compare the wincred helper against its original in compat.
[msysgit.git] / lib / perl5 / 5.8.8 / strict.pm
blobd14391add44341f239af567a7ca415ee53e7c861
1 package strict;
3 $strict::VERSION = "1.03";
5 my %bitmask = (
6 refs => 0x00000002,
7 subs => 0x00000200,
8 vars => 0x00000400
9 );
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;
18 if (@wrong) {
19 require Carp;
20 Carp::croak("Unknown 'strict' tag(s) '@wrong'");
22 $bits;
25 my $default_bits = bits(qw(refs subs vars));
27 sub import {
28 shift;
29 $^H |= @_ ? bits(@_) : $default_bits;
32 sub unimport {
33 shift;
34 $^H &= ~ (@_ ? bits(@_) : $default_bits);
38 __END__
40 =head1 NAME
42 strict - Perl pragma to restrict unsafe constructs
44 =head1 SYNOPSIS
46 use strict;
48 use strict "vars";
49 use strict "refs";
50 use strict "subs";
52 use strict;
53 no strict "vars";
55 =head1 DESCRIPTION
57 If 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
59 casual programming.) Currently, there are three possible things to be
60 strict about: "subs", "vars", and "refs".
62 =over 6
64 =item C<strict refs>
66 This generates a runtime error if you
67 use symbolic references (see L<perlref>).
69 use strict 'refs';
70 $ref = \$foo;
71 print $$ref; # ok
72 $ref = "foo";
73 print $$ref; # runtime error; normally ok
74 $file = "STDOUT";
75 print $file "Hi!"; # error; note: no comma after $file
77 There is one exception to this rule:
79 $bar = \&{'foo'};
80 &$bar;
82 is allowed so that C<goto &$AUTOLOAD> would not break under stricture.
85 =item C<strict vars>
87 This generates a compile-time error if you access a variable that wasn't
88 declared via C<our> or C<use vars>,
89 localized via C<my()>, or wasn't fully qualified. Because this is to avoid
90 variable suicide problems and subtle dynamic scoping issues, a merely
91 local() variable isn't good enough. See L<perlfunc/my> and
92 L<perlfunc/local>.
94 use strict 'vars';
95 $X::foo = 1; # ok, fully qualified
96 my $foo = 10; # ok, my() var
97 local $foo = 9; # blows up
99 package Cinna;
100 our $bar; # Declares $bar in current package
101 $bar = 'HgS'; # ok, global declared via pragma
103 The local() generated a compile-time error because you just touched a global
104 name without fully qualifying it.
106 Because of their special use by sort(), the variables $a and $b are
107 exempted from this check.
109 =item C<strict subs>
111 This disables the poetry optimization, generating a compile-time error if
112 you try to use a bareword identifier that's not a subroutine, unless it
113 is a simple identifier (no colons) and that it appears in curly braces or
114 on the left hand side of the C<< => >> symbol.
116 use strict 'subs';
117 $SIG{PIPE} = Plumber; # blows up
118 $SIG{PIPE} = "Plumber"; # just fine: quoted string is always ok
119 $SIG{PIPE} = \&Plumber; # preferred form
121 =back
123 See L<perlmodlib/Pragmatic Modules>.
125 =head1 HISTORY
127 C<strict 'subs'>, with Perl 5.6.1, erroneously permitted to use an unquoted
128 compound identifier (e.g. C<Foo::Bar>) as a hash key (before C<< => >> or
129 inside curlies), but without forcing it always to a literal string.
131 Starting with Perl 5.8.1 strict is strict about its restrictions:
132 if unknown restrictions are used, the strict pragma will abort with
134 Unknown 'strict' tag(s) '...'
136 =cut