[system] / trunk / pg / lib / ww_strict.pm Repository:
ViewVC logotype

Annotation of /trunk/pg/lib/ww_strict.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2164 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9