Commit | Line | Data |
394c3a46 |
1 | package strictures; |
2 | |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | |
500f28df |
6 | BEGIN { |
e12af862 |
7 | *_PERL_LT_5_8_4 = ("$]" < 5.008004) ? sub(){1} : sub(){0}; |
161de96d |
8 | # goto &UNIVERSAL::VERSION usually works on 5.8, but fails on some ARM |
9 | # machines. Seems to always work on 5.10 though. |
e12af862 |
10 | *_CAN_GOTO_VERSION = ("$]" >= 5.010000) ? sub(){1} : sub(){0}; |
500f28df |
11 | } |
084caaf3 |
12 | |
72ab6906 |
13 | our $VERSION = '2.000005'; |
03278299 |
14 | $VERSION =~ tr/_//d; |
394c3a46 |
15 | |
23c0b85d |
16 | our @WARNING_CATEGORIES = grep { exists $warnings::Offsets{$_} } qw( |
17 | closure |
95d54bcb |
18 | chmod |
23c0b85d |
19 | deprecated |
20 | exiting |
21 | experimental |
ae11fa66 |
22 | experimental::alpha_assertions |
c5b35ddf |
23 | experimental::autoderef |
dfe53a53 |
24 | experimental::bitwise |
1e139b49 |
25 | experimental::const_attr |
b87194d6 |
26 | experimental::declared_refs |
23c0b85d |
27 | experimental::lexical_subs |
28 | experimental::lexical_topic |
c5b35ddf |
29 | experimental::postderef |
be9a89ec |
30 | experimental::private_use |
1e139b49 |
31 | experimental::re_strict |
32 | experimental::refaliasing |
23c0b85d |
33 | experimental::regex_sets |
ae11fa66 |
34 | experimental::script_run |
c5b35ddf |
35 | experimental::signatures |
23c0b85d |
36 | experimental::smartmatch |
1e139b49 |
37 | experimental::win32_perlio |
23c0b85d |
38 | glob |
39 | imprecision |
40 | io |
41 | closed |
42 | exec |
43 | layer |
44 | newline |
45 | pipe |
c5b35ddf |
46 | syscalls |
23c0b85d |
47 | unopened |
1e139b49 |
48 | locale |
23c0b85d |
49 | misc |
1e139b49 |
50 | missing |
23c0b85d |
51 | numeric |
52 | once |
53 | overflow |
54 | pack |
55 | portable |
56 | recursion |
57 | redefine |
1e139b49 |
58 | redundant |
23c0b85d |
59 | regexp |
60 | severe |
61 | debugging |
62 | inplace |
63 | internal |
64 | malloc |
ae11fa66 |
65 | shadow |
23c0b85d |
66 | signal |
67 | substr |
68 | syntax |
69 | ambiguous |
70 | bareword |
71 | digit |
72 | illegalproto |
73 | parenthesis |
74 | precedence |
75 | printf |
76 | prototype |
77 | qw |
78 | reserved |
79 | semicolon |
80 | taint |
81 | threads |
82 | uninitialized |
95d54bcb |
83 | umask |
23c0b85d |
84 | unpack |
85 | untie |
86 | utf8 |
87 | non_unicode |
88 | nonchar |
89 | surrogate |
90 | void |
1e139b49 |
91 | void_unusual |
23c0b85d |
92 | y2k |
93 | ); |
94 | |
394c3a46 |
95 | sub VERSION { |
24590d98 |
96 | { |
97 | no warnings; |
98 | local $@; |
99 | if (defined $_[1] && eval { &UNIVERSAL::VERSION; 1}) { |
100 | $^H |= 0x20000 |
101 | unless _PERL_LT_5_8_4; |
102 | $^H{strictures_enable} = int $_[1]; |
103 | } |
394c3a46 |
104 | } |
24590d98 |
105 | _CAN_GOTO_VERSION ? goto &UNIVERSAL::VERSION : &UNIVERSAL::VERSION; |
394c3a46 |
106 | } |
107 | |
8f0df510 |
108 | our %extra_load_states; |
ffedb166 |
109 | |
2da53f4a |
110 | our $Smells_Like_VCS; |
12b8f19b |
111 | |
394c3a46 |
112 | sub import { |
92cde693 |
113 | my $class = shift; |
da50db3f |
114 | my %opts = @_ == 1 ? %{$_[0]} : @_; |
92cde693 |
115 | if (!exists $opts{version}) { |
116 | $opts{version} |
117 | = exists $^H{strictures_enable} ? delete $^H{strictures_enable} |
118 | : int $VERSION; |
119 | } |
c5a67be6 |
120 | $opts{file} = (caller)[1]; |
92cde693 |
121 | $class->_enable(\%opts); |
122 | } |
123 | |
124 | sub _enable { |
125 | my ($class, $opts) = @_; |
126 | my $version = $opts->{version}; |
127 | $version = 'undef' |
128 | if !defined $version; |
129 | my $method = "_enable_$version"; |
130 | if (!$class->can($method)) { |
a97f64c7 |
131 | require Carp; |
132 | Carp::croak("Major version specified as $version - not supported!"); |
92cde693 |
133 | } |
134 | $class->$method($opts); |
135 | } |
136 | |
137 | sub _enable_1 { |
c5a67be6 |
138 | my ($class, $opts) = @_; |
394c3a46 |
139 | strict->import; |
140 | warnings->import(FATAL => 'all'); |
084caaf3 |
141 | |
c5a67be6 |
142 | if (_want_extra($opts->{file})) { |
143 | _load_extras(qw(indirect multidimensional bareword::filehandles)); |
144 | indirect->unimport(':fatal') |
145 | if $extra_load_states{indirect}; |
146 | multidimensional->unimport |
147 | if $extra_load_states{multidimensional}; |
148 | bareword::filehandles->unimport |
149 | if $extra_load_states{'bareword::filehandles'}; |
150 | } |
8f0df510 |
151 | } |
152 | |
1931c77d |
153 | our @V2_NONFATAL = grep { exists $warnings::Offsets{$_} } ( |
154 | 'exec', # not safe to catch |
155 | 'recursion', # will be caught by other mechanisms |
156 | 'internal', # not safe to catch |
157 | 'malloc', # not safe to catch |
158 | 'newline', # stat on nonexistent file with a newline in it |
159 | 'experimental', # no reason for these to be fatal |
160 | 'deprecated', # unfortunately can't make these fatal |
161 | 'portable', # everything worked fine here, just may not elsewhere |
23c0b85d |
162 | ); |
1931c77d |
163 | our @V2_DISABLE = grep { exists $warnings::Offsets{$_} } ( |
164 | 'once' # triggers inconsistently, can't be fatalized |
23c0b85d |
165 | ); |
166 | |
167 | sub _enable_2 { |
168 | my ($class, $opts) = @_; |
169 | strict->import; |
170 | warnings->import; |
171 | warnings->import(FATAL => @WARNING_CATEGORIES); |
d50343b9 |
172 | warnings->unimport(FATAL => @V2_NONFATAL); |
173 | warnings->import(@V2_NONFATAL); |
23c0b85d |
174 | warnings->unimport(@V2_DISABLE); |
175 | |
176 | if (_want_extra($opts->{file})) { |
177 | _load_extras(qw(indirect multidimensional bareword::filehandles)); |
178 | indirect->unimport(':fatal') |
179 | if $extra_load_states{indirect}; |
180 | multidimensional->unimport |
181 | if $extra_load_states{multidimensional}; |
182 | bareword::filehandles->unimport |
183 | if $extra_load_states{'bareword::filehandles'}; |
184 | } |
185 | } |
186 | |
9d763997 |
187 | sub _want_extra_env { |
c5a67be6 |
188 | if (exists $ENV{PERL_STRICTURES_EXTRA}) { |
189 | if (_PERL_LT_5_8_4 and $ENV{PERL_STRICTURES_EXTRA}) { |
190 | die 'PERL_STRICTURES_EXTRA checks are not available on perls older' |
191 | . "than 5.8.4: please unset \$ENV{PERL_STRICTURES_EXTRA}\n"; |
394c3a46 |
192 | } |
9d763997 |
193 | return $ENV{PERL_STRICTURES_EXTRA} ? 1 : 0; |
c5a67be6 |
194 | } |
9d763997 |
195 | return undef; |
196 | } |
197 | |
198 | sub _want_extra { |
199 | my $file = shift; |
200 | my $want_env = _want_extra_env(); |
201 | return $want_env |
202 | if defined $want_env; |
c5a67be6 |
203 | return ( |
204 | !_PERL_LT_5_8_4 |
205 | and $file =~ /^(?:t|xt|lib|blib)[\\\/]/ |
206 | and defined $Smells_Like_VCS ? $Smells_Like_VCS |
207 | : ( $Smells_Like_VCS = !!( |
7470ef57 |
208 | -e '.git' || -e '.svn' || -e '.hg' || -e '.bzr' |
c5a67be6 |
209 | || (-e '../../dist.ini' |
7470ef57 |
210 | && (-e '../../.git' || -e '../../.svn' || -e '../../.hg' || -e '../../.bzr' )) |
c5a67be6 |
211 | )) |
212 | ); |
213 | } |
4f219885 |
214 | |
c5a67be6 |
215 | sub _load_extras { |
216 | my @extras = @_; |
8f0df510 |
217 | my @failed; |
218 | foreach my $mod (@extras) { |
219 | next |
220 | if exists $extra_load_states{$mod}; |
4f219885 |
221 | |
8f0df510 |
222 | $extra_load_states{$mod} = eval "require $mod; 1;" or do { |
223 | push @failed, $mod; |
488f2966 |
224 | |
8f0df510 |
225 | #work around 5.8 require bug |
226 | (my $file = $mod) =~ s|::|/|g; |
227 | delete $INC{"${file}.pm"}; |
228 | }; |
229 | } |
4f219885 |
230 | |
8f0df510 |
231 | if (@failed) { |
232 | my $failed = join ' ', @failed; |
233 | my $extras = join ' ', @extras; |
234 | print STDERR <<EOE; |
ffedb166 |
235 | strictures.pm extra testing active but couldn't load all modules. Missing were: |
236 | |
237 | $failed |
238 | |
0925b84b |
239 | Extra testing is auto-enabled in checkouts only, so if you're the author |
624cf8bb |
240 | of a strictures-using module you need to run: |
653f4377 |
241 | |
8f0df510 |
242 | cpan $extras |
653f4377 |
243 | |
244 | but these modules are not required by your users. |
084caaf3 |
245 | EOE |
394c3a46 |
246 | } |
247 | } |
248 | |
249 | 1; |
250 | |
251 | __END__ |
252 | =head1 NAME |
253 | |
4104cf62 |
254 | strictures - Turn on strict and make most warnings fatal |
394c3a46 |
255 | |
256 | =head1 SYNOPSIS |
257 | |
1c35787c |
258 | use strictures 2; |
394c3a46 |
259 | |
260 | is equivalent to |
261 | |
262 | use strict; |
263 | use warnings FATAL => 'all'; |
1c35787c |
264 | use warnings NONFATAL => qw( |
265 | exec |
266 | recursion |
267 | internal |
268 | malloc |
269 | newline |
270 | experimental |
271 | deprecated |
272 | portable |
273 | ); |
274 | no warnings 'once'; |
394c3a46 |
275 | |
5ab06a4d |
276 | except when called from a file which matches: |
394c3a46 |
277 | |
5b2a026b |
278 | (caller)[1] =~ /^(?:t|xt|lib|blib)[\\\/]/ |
394c3a46 |
279 | |
7470ef57 |
280 | and when either C<.git>, C<.svn>, C<.hg>, or C<.bzr> is present in the current |
281 | directory (with the intention of only forcing extra tests on the author side) |
282 | -- or when C<.git>, C<.svn>, C<.hg>, or C<.bzr> is present two directories up |
283 | along with C<dist.ini> (which would indicate we are in a C<dzil test> operation, |
284 | via L<Dist::Zilla>) -- or when the C<PERL_STRICTURES_EXTRA> environment variable |
285 | is set, in which case it also does the equivalent of |
394c3a46 |
286 | |
394c3a46 |
287 | no indirect 'fatal'; |
653f4377 |
288 | no multidimensional; |
289 | no bareword::filehandles; |
394c3a46 |
290 | |
3e14202f |
291 | Note that C<PERL_STRICTURES_EXTRA> may at some point add even more tests, with |
292 | only a minor version increase, but any changes to the effect of C<use |
293 | strictures> in normal mode will involve a major version bump. |
394c3a46 |
294 | |
0eb0d037 |
295 | If any of the extra testing modules are not present, L<strictures> will |
25877bf2 |
296 | complain loudly, once, via C<warn()>, and then shut up. But you really |
ffedb166 |
297 | should consider installing them, they're all great anti-footgun tools. |
17b03f2e |
298 | |
394c3a46 |
299 | =head1 DESCRIPTION |
300 | |
301 | I've been writing the equivalent of this module at the top of my code for |
302 | about a year now. I figured it was time to make it shorter. |
303 | |
25877bf2 |
304 | Things like the importer in C<use Moose> don't help me because they turn |
d8c1c6b2 |
305 | warnings on but don't make them fatal -- which from my point of view is |
2288278f |
306 | useless because I want an exception to tell me my code isn't warnings-clean. |
394c3a46 |
307 | |
308 | Any time I see a warning from my code, that indicates a mistake. |
309 | |
d8c1c6b2 |
310 | Any time my code encounters a mistake, I want a crash -- not spew to STDERR |
394c3a46 |
311 | and then unknown (and probably undesired) subsequent behaviour. |
312 | |
313 | I also want to ensure that obvious coding mistakes, like indirect object |
314 | syntax (and not so obvious mistakes that cause things to accidentally compile |
315 | as such) get caught, but not at the cost of an XS dependency and not at the |
316 | cost of blowing things up on another machine. |
317 | |
0eb0d037 |
318 | Therefore, L<strictures> turns on additional checking, but only when it thinks |
2288278f |
319 | it's running in a test file in a VCS checkout -- although if this causes |
93ae637e |
320 | undesired behaviour this can be overridden by setting the |
25877bf2 |
321 | C<PERL_STRICTURES_EXTRA> environment variable. |
394c3a46 |
322 | |
323 | If additional useful author side checks come to mind, I'll add them to the |
3e14202f |
324 | C<PERL_STRICTURES_EXTRA> code path only -- this will result in a minor version |
325 | increase (e.g. 1.000000 to 1.001000 (1.1.0) or similar). Any fixes only to the |
326 | mechanism of this code will result in a sub-version increase (e.g. 1.000000 to |
327 | 1.000001 (1.0.1)). |
394c3a46 |
328 | |
d48424bd |
329 | =head1 CATEGORY SELECTIONS |
330 | |
331 | strictures does not enable fatal warnings for all categories. |
332 | |
333 | =over 4 |
334 | |
335 | =item exec |
336 | |
337 | Includes a warning that can cause your program to continue running |
338 | unintentionally after an internal fork. Not safe to fatalize. |
339 | |
340 | =item recursion |
341 | |
342 | Infinite recursion will end up overflowing the stack eventually anyway. |
343 | |
344 | =item internal |
345 | |
346 | Triggers deep within perl, in places that are not safe to trap. |
347 | |
348 | =item malloc |
349 | |
350 | Triggers deep within perl, in places that are not safe to trap. |
351 | |
352 | =item newline |
353 | |
354 | Includes a warning for using stat on a valid but suspect filename, ending in a |
355 | newline. |
356 | |
357 | =item experimental |
358 | |
359 | Experimental features are used intentionally. |
360 | |
361 | =item deprecated |
362 | |
363 | Deprecations will inherently be added to in the future in unexpected ways, |
364 | so making them fatal won't be reliable. |
365 | |
366 | =item portable |
367 | |
368 | Doesn't indicate an actual problem with the program, only that it may not |
369 | behave properly if run on a different machine. |
370 | |
371 | =item once |
372 | |
373 | Can't be fatalized. Also triggers very inconsistently, so we just disable it. |
374 | |
375 | =back |
376 | |
1c35787c |
377 | =head1 VERSIONS |
378 | |
379 | Depending on the version of strictures requested, different warnings will be |
380 | enabled. If no specific version is requested, the current version's behavior |
381 | will be used. Versions can be requested using perl's standard mechanism: |
382 | |
383 | use strictures 2; |
384 | |
385 | Or, by passing in a C<version> option: |
386 | |
387 | use strictures version => 2; |
394c3a46 |
388 | |
1c35787c |
389 | =head2 VERSION 2 |
394c3a46 |
390 | |
1c35787c |
391 | Equivalent to: |
392 | |
393 | use strict; |
394 | use warnings FATAL => 'all'; |
d771b3bb |
395 | use warnings NONFATAL => qw( |
396 | exec |
397 | recursion |
398 | internal |
399 | malloc |
400 | newline |
401 | experimental |
402 | deprecated |
403 | portable |
404 | ); |
405 | no warnings 'once'; |
406 | |
1c35787c |
407 | # and if in dev mode: |
408 | no indirect 'fatal'; |
409 | no multidimensional; |
410 | no bareword::filehandles; |
411 | |
d771b3bb |
412 | Additionally, any warnings created by modules using L<warnings::register> or |
413 | C<warnings::register_categories()> will not be fatalized. |
414 | |
1c35787c |
415 | =head2 VERSION 1 |
416 | |
417 | Equivalent to: |
418 | |
419 | use strict; |
420 | use warnings FATAL => 'all'; |
421 | # and if in dev mode: |
422 | no indirect 'fatal'; |
423 | no multidimensional; |
424 | no bareword::filehandles; |
eae006ee |
425 | |
426 | =head1 METHODS |
427 | |
428 | =head2 import |
429 | |
1c35787c |
430 | This method does the setup work described above in L</DESCRIPTION>. Optionally |
431 | accepts a C<version> option to request a specific version's behavior. |
eae006ee |
432 | |
433 | =head2 VERSION |
434 | |
25877bf2 |
435 | This method traps the C<< strictures->VERSION(1) >> call produced by a use line |
eae006ee |
436 | with a version number on it and does the version check. |
437 | |
f9df7e2e |
438 | =head1 EXTRA TESTING RATIONALE |
439 | |
25877bf2 |
440 | Every so often, somebody complains that they're deploying via C<git pull> |
d8c1c6b2 |
441 | and that they don't want L<strictures> to enable itself in this case -- and that |
f9df7e2e |
442 | setting C<PERL_STRICTURES_EXTRA> to 0 isn't acceptable (additional ways to |
443 | disable extra testing would be welcome but the discussion never seems to get |
444 | that far). |
445 | |
446 | In order to allow us to skip a couple of stages and get straight to a |
447 | productive conversation, here's my current rationale for turning the |
448 | extra testing on via a heuristic: |
449 | |
450 | The extra testing is all stuff that only ever blows up at compile time; |
2288278f |
451 | this is intentional. So the oft-raised concern that it's different code being |
d8c1c6b2 |
452 | tested is only sort of the case -- none of the modules involved affect the |
f9df7e2e |
453 | final optree to my knowledge, so the author gets some additional compile |
454 | time crashes which he/she then fixes, and the rest of the testing is |
455 | completely valid for all environments. |
456 | |
d8c1c6b2 |
457 | The point of the extra testing -- especially C<no indirect> -- is to catch |
f9df7e2e |
458 | mistakes that newbie users won't even realise are mistakes without |
459 | help. For example, |
460 | |
461 | foo { ... }; |
462 | |
d8c1c6b2 |
463 | where foo is an & prototyped sub that you forgot to import -- this is |
9a363fed |
464 | pernicious to track down since all I<seems> fine until it gets called |
f9df7e2e |
465 | and you get a crash. Worse still, you can fail to have imported it due |
466 | to a circular require, at which point you have a load order dependent |
9a363fed |
467 | bug which I've seen before now I<only> show up in production due to tiny |
f9df7e2e |
468 | differences between the production and the development environment. I wrote |
469 | L<http://shadow.cat/blog/matt-s-trout/indirect-but-still-fatal/> to explain |
470 | this particular problem before L<strictures> itself existed. |
471 | |
2288278f |
472 | As such, in my experience so far L<strictures>' extra testing has |
9a363fed |
473 | I<avoided> production versus development differences, not caused them. |
f9df7e2e |
474 | |
0eb0d037 |
475 | Additionally, L<strictures>' policy is very much "try and provide as much |
d8c1c6b2 |
476 | protection as possible for newbies -- who won't think about whether there's |
477 | an option to turn on or not" -- so having only the environment variable |
f9df7e2e |
478 | is not sufficient to achieve that (I get to explain that you need to add |
d8c1c6b2 |
479 | C<use strict> at least once a week on freenode #perl -- newbies sometimes |
f9df7e2e |
480 | completely skip steps because they don't understand that that step |
481 | is important). |
482 | |
d8c1c6b2 |
483 | I make no claims that the heuristic is perfect -- it's already been evolved |
f9df7e2e |
484 | significantly over time, especially for 1.004 where we changed things to |
0eb0d037 |
485 | ensure it only fires on files in your checkout (rather than L<strictures>-using |
f9df7e2e |
486 | modules you happened to have installed, which was just silly). However, I |
487 | hope the above clarifies why a heuristic approach is not only necessary but |
3e14202f |
488 | desirable from a point of view of providing new users with as much safety as |
489 | possible, and will allow any future discussion on the subject to focus on "how |
490 | do we minimise annoyance to people deploying from checkouts intentionally". |
f9df7e2e |
491 | |
96c8649b |
492 | =head1 SEE ALSO |
493 | |
494 | =over 4 |
495 | |
496 | =item * |
497 | |
498 | L<indirect> |
499 | |
500 | =item * |
501 | |
502 | L<multidimensional> |
503 | |
504 | =item * |
505 | |
506 | L<bareword::filehandles> |
507 | |
508 | =back |
509 | |
eae006ee |
510 | =head1 COMMUNITY AND SUPPORT |
511 | |
512 | =head2 IRC channel |
513 | |
514 | irc.perl.org #toolchain |
515 | |
516 | (or bug 'mst' in query on there or freenode) |
517 | |
518 | =head2 Git repository |
519 | |
520 | Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is: |
521 | |
522 | git clone git://git.shadowcat.co.uk/p5sagit/strictures.git |
523 | |
91be28bc |
524 | The web interface to the repository is at: |
525 | |
526 | http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/strictures.git |
527 | |
eae006ee |
528 | =head1 AUTHOR |
529 | |
d81f898d |
530 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
eae006ee |
531 | |
532 | =head1 CONTRIBUTORS |
533 | |
8190ff5b |
534 | Karen Etheridge (cpan:ETHER) <ether@cpan.org> |
eae006ee |
535 | |
04b4a35d |
536 | Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@gmail.com> |
537 | |
a79d1096 |
538 | haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org> |
539 | |
eae006ee |
540 | =head1 COPYRIGHT |
541 | |
542 | Copyright (c) 2010 the strictures L</AUTHOR> and L</CONTRIBUTORS> |
543 | as listed above. |
544 | |
545 | =head1 LICENSE |
546 | |
547 | This library is free software and may be distributed under the same terms |
548 | as perl itself. |
549 | |
550 | =cut |