4 use warnings FATAL => 'all';
7 *_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 *_CAN_GOTO_VERSION = ("$]" >= 5.010000) ? sub(){1} : sub(){0};
13 our $VERSION = '2.000005';
16 our @WARNING_CATEGORIES = grep { exists $warnings::Offsets{$_} } qw(
22 experimental::autoderef
24 experimental::const_attr
25 experimental::declared_refs
26 experimental::lexical_subs
27 experimental::lexical_topic
28 experimental::postderef
29 experimental::re_strict
30 experimental::refaliasing
31 experimental::regex_sets
32 experimental::signatures
33 experimental::smartmatch
34 experimental::win32_perlio
95 if (defined $_[1] && eval { &UNIVERSAL::VERSION; 1}) {
97 unless _PERL_LT_5_8_4;
98 $^H{strictures_enable} = int $_[1];
101 _CAN_GOTO_VERSION ? goto &UNIVERSAL::VERSION : &UNIVERSAL::VERSION;
104 our %extra_load_states;
106 our $Smells_Like_VCS;
110 my %opts = @_ == 1 ? %{$_[0]} : @_;
111 if (!exists $opts{version}) {
113 = exists $^H{strictures_enable} ? delete $^H{strictures_enable}
116 $opts{file} = (caller)[1];
117 $class->_enable(\%opts);
121 my ($class, $opts) = @_;
122 my $version = $opts->{version};
124 if !defined $version;
125 my $method = "_enable_$version";
126 if (!$class->can($method)) {
128 Carp::croak("Major version specified as $version - not supported!");
130 $class->$method($opts);
134 my ($class, $opts) = @_;
136 warnings->import(FATAL => 'all');
138 if (_want_extra($opts->{file})) {
139 _load_extras(qw(indirect multidimensional bareword::filehandles));
140 indirect->unimport(':fatal')
141 if $extra_load_states{indirect};
142 multidimensional->unimport
143 if $extra_load_states{multidimensional};
144 bareword::filehandles->unimport
145 if $extra_load_states{'bareword::filehandles'};
149 our @V2_NONFATAL = grep { exists $warnings::Offsets{$_} } (
150 'exec', # not safe to catch
151 'recursion', # will be caught by other mechanisms
152 'internal', # not safe to catch
153 'malloc', # not safe to catch
154 'newline', # stat on nonexistent file with a newline in it
155 'experimental', # no reason for these to be fatal
156 'deprecated', # unfortunately can't make these fatal
157 'portable', # everything worked fine here, just may not elsewhere
159 our @V2_DISABLE = grep { exists $warnings::Offsets{$_} } (
160 'once' # triggers inconsistently, can't be fatalized
164 my ($class, $opts) = @_;
167 warnings->import(FATAL => @WARNING_CATEGORIES);
168 warnings->unimport(FATAL => @V2_NONFATAL);
169 warnings->import(@V2_NONFATAL);
170 warnings->unimport(@V2_DISABLE);
172 if (_want_extra($opts->{file})) {
173 _load_extras(qw(indirect multidimensional bareword::filehandles));
174 indirect->unimport(':fatal')
175 if $extra_load_states{indirect};
176 multidimensional->unimport
177 if $extra_load_states{multidimensional};
178 bareword::filehandles->unimport
179 if $extra_load_states{'bareword::filehandles'};
183 sub _want_extra_env {
184 if (exists $ENV{PERL_STRICTURES_EXTRA}) {
185 if (_PERL_LT_5_8_4 and $ENV{PERL_STRICTURES_EXTRA}) {
186 die 'PERL_STRICTURES_EXTRA checks are not available on perls older'
187 . "than 5.8.4: please unset \$ENV{PERL_STRICTURES_EXTRA}\n";
189 return $ENV{PERL_STRICTURES_EXTRA} ? 1 : 0;
196 my $want_env = _want_extra_env();
198 if defined $want_env;
201 and $file =~ /^(?:t|xt|lib|blib)[\\\/]/
202 and defined $Smells_Like_VCS ? $Smells_Like_VCS
203 : ( $Smells_Like_VCS = !!(
204 -e '.git' || -e '.svn' || -e '.hg' || -e '.bzr'
205 || (-e '../../dist.ini'
206 && (-e '../../.git' || -e '../../.svn' || -e '../../.hg' || -e '../../.bzr' ))
214 foreach my $mod (@extras) {
216 if exists $extra_load_states{$mod};
218 $extra_load_states{$mod} = eval "require $mod; 1;" or do {
221 #work around 5.8 require bug
222 (my $file = $mod) =~ s|::|/|g;
223 delete $INC{"${file}.pm"};
228 my $failed = join ' ', @failed;
229 my $extras = join ' ', @extras;
231 strictures.pm extra testing active but couldn't load all modules. Missing were:
235 Extra testing is auto-enabled in checkouts only, so if you're the author
236 of a strictures-using module you need to run:
240 but these modules are not required by your users.
250 strictures - turn on strict and make most warnings fatal
259 use warnings FATAL => 'all';
260 use warnings NONFATAL => qw(
272 except when called from a file which matches:
274 (caller)[1] =~ /^(?:t|xt|lib|blib)[\\\/]/
276 and when either C<.git>, C<.svn>, C<.hg>, or C<.bzr> is present in the current
277 directory (with the intention of only forcing extra tests on the author side)
278 -- or when C<.git>, C<.svn>, C<.hg>, or C<.bzr> is present two directories up
279 along with C<dist.ini> (which would indicate we are in a C<dzil test> operation,
280 via L<Dist::Zilla>) -- or when the C<PERL_STRICTURES_EXTRA> environment variable
281 is set, in which case it also does the equivalent of
285 no bareword::filehandles;
287 Note that C<PERL_STRICTURES_EXTRA> may at some point add even more tests, with
288 only a minor version increase, but any changes to the effect of C<use
289 strictures> in normal mode will involve a major version bump.
291 If any of the extra testing modules are not present, L<strictures> will
292 complain loudly, once, via C<warn()>, and then shut up. But you really
293 should consider installing them, they're all great anti-footgun tools.
297 I've been writing the equivalent of this module at the top of my code for
298 about a year now. I figured it was time to make it shorter.
300 Things like the importer in C<use Moose> don't help me because they turn
301 warnings on but don't make them fatal -- which from my point of view is
302 useless because I want an exception to tell me my code isn't warnings-clean.
304 Any time I see a warning from my code, that indicates a mistake.
306 Any time my code encounters a mistake, I want a crash -- not spew to STDERR
307 and then unknown (and probably undesired) subsequent behaviour.
309 I also want to ensure that obvious coding mistakes, like indirect object
310 syntax (and not so obvious mistakes that cause things to accidentally compile
311 as such) get caught, but not at the cost of an XS dependency and not at the
312 cost of blowing things up on another machine.
314 Therefore, L<strictures> turns on additional checking, but only when it thinks
315 it's running in a test file in a VCS checkout -- although if this causes
316 undesired behaviour this can be overridden by setting the
317 C<PERL_STRICTURES_EXTRA> environment variable.
319 If additional useful author side checks come to mind, I'll add them to the
320 C<PERL_STRICTURES_EXTRA> code path only -- this will result in a minor version
321 increase (e.g. 1.000000 to 1.001000 (1.1.0) or similar). Any fixes only to the
322 mechanism of this code will result in a sub-version increase (e.g. 1.000000 to
325 =head1 CATEGORY SELECTIONS
327 strictures does not enable fatal warnings for all categories.
333 Includes a warning that can cause your program to continue running
334 unintentionally after an internal fork. Not safe to fatalize.
338 Infinite recursion will end up overflowing the stack eventually anyway.
342 Triggers deep within perl, in places that are not safe to trap.
346 Triggers deep within perl, in places that are not safe to trap.
350 Includes a warning for using stat on a valid but suspect filename, ending in a
355 Experimental features are used intentionally.
359 Deprecations will inherently be added to in the future in unexpected ways,
360 so making them fatal won't be reliable.
364 Doesn't indicate an actual problem with the program, only that it may not
365 behave properly if run on a different machine.
369 Can't be fatalized. Also triggers very inconsistently, so we just disable it.
375 Depending on the version of strictures requested, different warnings will be
376 enabled. If no specific version is requested, the current version's behavior
377 will be used. Versions can be requested using perl's standard mechanism:
381 Or, by passing in a C<version> option:
383 use strictures version => 2;
390 use warnings FATAL => 'all';
391 use warnings NONFATAL => qw(
403 # and if in dev mode:
406 no bareword::filehandles;
408 Additionally, any warnings created by modules using L<warnings::register> or
409 C<warnings::register_categories()> will not be fatalized.
416 use warnings FATAL => 'all';
417 # and if in dev mode:
420 no bareword::filehandles;
426 This method does the setup work described above in L</DESCRIPTION>. Optionally
427 accepts a C<version> option to request a specific version's behavior.
431 This method traps the C<< strictures->VERSION(1) >> call produced by a use line
432 with a version number on it and does the version check.
434 =head1 EXTRA TESTING RATIONALE
436 Every so often, somebody complains that they're deploying via C<git pull>
437 and that they don't want L<strictures> to enable itself in this case -- and that
438 setting C<PERL_STRICTURES_EXTRA> to 0 isn't acceptable (additional ways to
439 disable extra testing would be welcome but the discussion never seems to get
442 In order to allow us to skip a couple of stages and get straight to a
443 productive conversation, here's my current rationale for turning the
444 extra testing on via a heuristic:
446 The extra testing is all stuff that only ever blows up at compile time;
447 this is intentional. So the oft-raised concern that it's different code being
448 tested is only sort of the case -- none of the modules involved affect the
449 final optree to my knowledge, so the author gets some additional compile
450 time crashes which he/she then fixes, and the rest of the testing is
451 completely valid for all environments.
453 The point of the extra testing -- especially C<no indirect> -- is to catch
454 mistakes that newbie users won't even realise are mistakes without
459 where foo is an & prototyped sub that you forgot to import -- this is
460 pernicious to track down since all I<seems> fine until it gets called
461 and you get a crash. Worse still, you can fail to have imported it due
462 to a circular require, at which point you have a load order dependent
463 bug which I've seen before now I<only> show up in production due to tiny
464 differences between the production and the development environment. I wrote
465 L<http://shadow.cat/blog/matt-s-trout/indirect-but-still-fatal/> to explain
466 this particular problem before L<strictures> itself existed.
468 As such, in my experience so far L<strictures>' extra testing has
469 I<avoided> production versus development differences, not caused them.
471 Additionally, L<strictures>' policy is very much "try and provide as much
472 protection as possible for newbies -- who won't think about whether there's
473 an option to turn on or not" -- so having only the environment variable
474 is not sufficient to achieve that (I get to explain that you need to add
475 C<use strict> at least once a week on freenode #perl -- newbies sometimes
476 completely skip steps because they don't understand that that step
479 I make no claims that the heuristic is perfect -- it's already been evolved
480 significantly over time, especially for 1.004 where we changed things to
481 ensure it only fires on files in your checkout (rather than L<strictures>-using
482 modules you happened to have installed, which was just silly). However, I
483 hope the above clarifies why a heuristic approach is not only necessary but
484 desirable from a point of view of providing new users with as much safety as
485 possible, and will allow any future discussion on the subject to focus on "how
486 do we minimise annoyance to people deploying from checkouts intentionally".
502 L<bareword::filehandles>
506 =head1 COMMUNITY AND SUPPORT
510 irc.perl.org #toolchain
512 (or bug 'mst' in query on there or freenode)
514 =head2 Git repository
516 Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is:
518 git clone git://git.shadowcat.co.uk/p5sagit/strictures.git
520 The web interface to the repository is at:
522 http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/strictures.git
526 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
530 Karen Etheridge (cpan:ETHER) <ether@cpan.org>
532 Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@gmail.com>
534 haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
538 Copyright (c) 2010 the strictures L</AUTHOR> and L</CONTRIBUTORS>
543 This library is free software and may be distributed under the same terms