File Coverage

blib/lib/strictures.pm
Criterion Covered Total %
statement 74 74 100.0
branch 42 44 95.4
condition 5 6 83.3
subroutine 12 12 100.0
pod 1 1 100.0
total 134 137 97.8


line stmt bran cond sub pod time code
1             package strictures;
2              
3 3     3   32391 use strict;
  3         5  
  3         86  
4 3     3   9 use warnings FATAL => 'all';
  3         4  
  3         316  
5              
6             BEGIN {
7 3 50   3   23 *_PERL_LT_5_8_4 = ($] < 5.008004) ? sub(){1} : sub(){0};
8             # goto &UNIVERSAL::VERSION usually works on 5.8, but fails on some ARM
9             # machines. Seems to always work on 5.10 though.
10 3 50       274 *_CAN_GOTO_VERSION = ($] >= 5.010000) ? sub(){1} : sub(){0};
11             }
12              
13             our $VERSION = '2.000003';
14             $VERSION = eval $VERSION;
15              
16             our @WARNING_CATEGORIES = grep { exists $warnings::Offsets{$_} } qw(
17             closure
18             chmod
19             deprecated
20             exiting
21             experimental
22             experimental::autoderef
23             experimental::bitwise
24             experimental::const_attr
25             experimental::lexical_subs
26             experimental::lexical_topic
27             experimental::postderef
28             experimental::re_strict
29             experimental::refaliasing
30             experimental::regex_sets
31             experimental::signatures
32             experimental::smartmatch
33             experimental::win32_perlio
34             glob
35             imprecision
36             io
37             closed
38             exec
39             layer
40             newline
41             pipe
42             syscalls
43             unopened
44             locale
45             misc
46             missing
47             numeric
48             once
49             overflow
50             pack
51             portable
52             recursion
53             redefine
54             redundant
55             regexp
56             severe
57             debugging
58             inplace
59             internal
60             malloc
61             signal
62             substr
63             syntax
64             ambiguous
65             bareword
66             digit
67             illegalproto
68             parenthesis
69             precedence
70             printf
71             prototype
72             qw
73             reserved
74             semicolon
75             taint
76             threads
77             uninitialized
78             umask
79             unpack
80             untie
81             utf8
82             non_unicode
83             nonchar
84             surrogate
85             void
86             void_unusual
87             y2k
88             );
89              
90             sub VERSION {
91             {
92 3     3 1 11 no warnings;
  3     33   3  
  3         2697  
  33         17815  
93 33         66 local $@;
94 33 100 100     84 if (defined $_[1] && eval { &UNIVERSAL::VERSION; 1}) {
  32         394  
  31         164  
95 31         54 $^H |= 0x20000
96             unless _PERL_LT_5_8_4;
97 31         168 $^H{strictures_enable} = int $_[1];
98             }
99             }
100 33         280 _CAN_GOTO_VERSION ? goto &UNIVERSAL::VERSION : &UNIVERSAL::VERSION;
101             }
102              
103             our %extra_load_states;
104              
105             our $Smells_Like_VCS;
106              
107             sub import {
108 34     34   825 my $class = shift;
109 34 100       82 my %opts = @_ == 1 ? %{$_[0]} : @_;
  2         5  
110 34 100       83 if (!exists $opts{version}) {
111             $opts{version}
112             = exists $^H{strictures_enable} ? delete $^H{strictures_enable}
113 32 100       93 : int $VERSION;
114             }
115 34         88 $opts{file} = (caller)[1];
116 34         69 $class->_enable(\%opts);
117             }
118              
119             sub _enable {
120 34     34   32 my ($class, $opts) = @_;
121 34         35 my $version = $opts->{version};
122 34 100       52 $version = 'undef'
123             if !defined $version;
124 34         42 my $method = "_enable_$version";
125 34 100       126 if (!$class->can($method)) {
126 2         7 require Carp;
127 2         276 Carp::croak("Major version specified as $version - not supported!");
128             }
129 32         61 $class->$method($opts);
130             }
131              
132             sub _enable_1 {
133 15     15   13 my ($class, $opts) = @_;
134 15         51 strict->import;
135 15         195 warnings->import(FATAL => 'all');
136              
137 15 100       27 if (_want_extra($opts->{file})) {
138 8         12 _load_extras(qw(indirect multidimensional bareword::filehandles));
139             indirect->unimport(':fatal')
140 8 100       26 if $extra_load_states{indirect};
141             multidimensional->unimport
142 8 100       34 if $extra_load_states{multidimensional};
143             bareword::filehandles->unimport
144 8 100       93 if $extra_load_states{'bareword::filehandles'};
145             }
146             }
147              
148             our @V2_NONFATAL = grep { exists $warnings::Offsets{$_} } (
149             'exec', # not safe to catch
150             'recursion', # will be caught by other mechanisms
151             'internal', # not safe to catch
152             'malloc', # not safe to catch
153             'newline', # stat on nonexistent file with a newline in it
154             'experimental', # no reason for these to be fatal
155             'deprecated', # unfortunately can't make these fatal
156             'portable', # everything worked fine here, just may not elsewhere
157             );
158             our @V2_DISABLE = grep { exists $warnings::Offsets{$_} } (
159             'once' # triggers inconsistently, can't be fatalized
160             );
161              
162             sub _enable_2 {
163 17     17   16 my ($class, $opts) = @_;
164 17         49 strict->import;
165 17         112 warnings->import;
166 17         888 warnings->import(FATAL => @WARNING_CATEGORIES);
167 17         202 warnings->unimport(FATAL => @V2_NONFATAL);
168 17         178 warnings->import(@V2_NONFATAL);
169 17         70 warnings->unimport(@V2_DISABLE);
170              
171 17 100       26 if (_want_extra($opts->{file})) {
172 9         13 _load_extras(qw(indirect multidimensional bareword::filehandles));
173             indirect->unimport(':fatal')
174 9 100       23 if $extra_load_states{indirect};
175             multidimensional->unimport
176 9 100       27 if $extra_load_states{multidimensional};
177             bareword::filehandles->unimport
178 9 100       97 if $extra_load_states{'bareword::filehandles'};
179             }
180             }
181              
182             sub _want_extra_env {
183 32 100   32   57 if (exists $ENV{PERL_STRICTURES_EXTRA}) {
184 8         7 if (_PERL_LT_5_8_4 and $ENV{PERL_STRICTURES_EXTRA}) {
185             die 'PERL_STRICTURES_EXTRA checks are not available on perls older'
186             . "than 5.8.4: please unset \$ENV{PERL_STRICTURES_EXTRA}\n";
187             }
188 8 100       18 return $ENV{PERL_STRICTURES_EXTRA} ? 1 : 0;
189             }
190 24         24 return undef;
191             }
192              
193             sub _want_extra {
194 32     32   27 my $file = shift;
195 32         38 my $want_env = _want_extra_env();
196 32 100       156 return $want_env
197             if defined $want_env;
198             return (
199 24   66     513 !_PERL_LT_5_8_4
200             and $file =~ /^(?:t|xt|lib|blib)[\\\/]/
201             and defined $Smells_Like_VCS ? $Smells_Like_VCS
202             : ( $Smells_Like_VCS = !!(
203             -e '.git' || -e '.svn' || -e '.hg' || -e '.bzr'
204             || (-e '../../dist.ini'
205             && (-e '../../.git' || -e '../../.svn' || -e '../../.hg' || -e '../../.bzr' ))
206             ))
207             );
208             }
209              
210             sub _load_extras {
211 17     17   25 my @extras = @_;
212 17         15 my @failed;
213 17         18 foreach my $mod (@extras) {
214             next
215 51 100       79 if exists $extra_load_states{$mod};
216              
217 12 100       537 $extra_load_states{$mod} = eval "require $mod; 1;" or do {
218 9         106 push @failed, $mod;
219              
220             #work around 5.8 require bug
221 9         17 (my $file = $mod) =~ s|::|/|g;
222 9         35 delete $INC{"${file}.pm"};
223             };
224             }
225              
226 17 100       34 if (@failed) {
227 3         8 my $failed = join ' ', @failed;
228 3         5 my $extras = join ' ', @extras;
229 3         15 print STDERR <
230             strictures.pm extra testing active but couldn't load all modules. Missing were:
231              
232             $failed
233              
234             Extra testing is auto-enabled in checkouts only, so if you're the author
235             of a strictures-using module you need to run:
236              
237             cpan $extras
238              
239             but these modules are not required by your users.
240             EOE
241             }
242             }
243              
244             1;
245              
246             __END__