4 use warnings FATAL => 'all';
7 *_PERL_LT_5_8_4 = ($] < 5.008004) ? sub(){1} : sub(){0};
8 *_CAN_GOTO_VERSION = ($] >= 5.008000) ? sub(){1} : sub(){0};
11 our $VERSION = '2.000000';
12 $VERSION = eval $VERSION;
14 our @WARNING_CATEGORIES = grep { exists $warnings::Offsets{$_} } qw(
20 experimental::autoderef
21 experimental::const_attr
22 experimental::lexical_subs
23 experimental::lexical_topic
24 experimental::postderef
25 experimental::re_strict
26 experimental::refaliasing
27 experimental::regex_sets
28 experimental::signatures
29 experimental::smartmatch
30 experimental::win32_perlio
91 if (defined $_[1] && eval { &UNIVERSAL::VERSION; 1}) {
93 unless _PERL_LT_5_8_4;
94 $^H{strictures_enable} = int $_[1];
97 _CAN_GOTO_VERSION ? goto &UNIVERSAL::VERSION : &UNIVERSAL::VERSION;
100 our %extra_load_states;
102 our $Smells_Like_VCS;
106 my %opts = ref $_[0] ? %{$_[0]} : @_;
107 if (!exists $opts{version}) {
109 = exists $^H{strictures_enable} ? delete $^H{strictures_enable}
112 $opts{file} = (caller)[1];
113 $class->_enable(\%opts);
117 my ($class, $opts) = @_;
118 my $version = $opts->{version};
120 if !defined $version;
121 my $method = "_enable_$version";
122 if (!$class->can($method)) {
124 Carp::croak("Major version specified as $version - not supported!");
126 $class->$method($opts);
130 my ($class, $opts) = @_;
132 warnings->import(FATAL => 'all');
134 if (_want_extra($opts->{file})) {
135 _load_extras(qw(indirect multidimensional bareword::filehandles));
136 indirect->unimport(':fatal')
137 if $extra_load_states{indirect};
138 multidimensional->unimport
139 if $extra_load_states{multidimensional};
140 bareword::filehandles->unimport
141 if $extra_load_states{'bareword::filehandles'};
145 our @V2_NONFATAL = grep { exists $warnings::Offsets{$_} } (
146 'exec', # not safe to catch
147 'recursion', # will be caught by other mechanisms
148 'internal', # not safe to catch
149 'malloc', # not safe to catch
150 'newline', # stat on nonexistent file with a newline in it
151 'experimental', # no reason for these to be fatal
152 'deprecated', # unfortunately can't make these fatal
153 'portable', # everything worked fine here, just may not elsewhere
155 our @V2_DISABLE = grep { exists $warnings::Offsets{$_} } (
156 'once' # triggers inconsistently, can't be fatalized
160 my ($class, $opts) = @_;
163 warnings->import(FATAL => @WARNING_CATEGORIES);
164 warnings->unimport(FATAL => @V2_NONFATAL);
165 warnings->import(@V2_NONFATAL);
166 warnings->unimport(@V2_DISABLE);
168 if (_want_extra($opts->{file})) {
169 _load_extras(qw(indirect multidimensional bareword::filehandles));
170 indirect->unimport(':fatal')
171 if $extra_load_states{indirect};
172 multidimensional->unimport
173 if $extra_load_states{multidimensional};
174 bareword::filehandles->unimport
175 if $extra_load_states{'bareword::filehandles'};
179 sub _want_extra_env {
180 if (exists $ENV{PERL_STRICTURES_EXTRA}) {
181 if (_PERL_LT_5_8_4 and $ENV{PERL_STRICTURES_EXTRA}) {
182 die 'PERL_STRICTURES_EXTRA checks are not available on perls older'
183 . "than 5.8.4: please unset \$ENV{PERL_STRICTURES_EXTRA}\n";
185 return $ENV{PERL_STRICTURES_EXTRA} ? 1 : 0;
192 my $want_env = _want_extra_env();
194 if defined $want_env;
197 and $file =~ /^(?:t|xt|lib|blib)[\\\/]/
198 and defined $Smells_Like_VCS ? $Smells_Like_VCS
199 : ( $Smells_Like_VCS = !!(
200 -e '.git' || -e '.svn' || -e '.hg'
201 || (-e '../../dist.ini'
202 && (-e '../../.git' || -e '../../.svn' || -e '../../.hg' ))
210 foreach my $mod (@extras) {
212 if exists $extra_load_states{$mod};
214 $extra_load_states{$mod} = eval "require $mod; 1;" or do {
217 #work around 5.8 require bug
218 (my $file = $mod) =~ s|::|/|g;
219 delete $INC{"${file}.pm"};
224 my $failed = join ' ', @failed;
225 my $extras = join ' ', @extras;
227 strictures.pm extra testing active but couldn't load all modules. Missing were:
231 Extra testing is auto-enabled in checkouts only, so if you're the author
232 of a strictures-using module you need to run:
236 but these modules are not required by your users.
246 strictures - turn on strict and make all warnings fatal
255 use warnings FATAL => 'all';
256 use warnings NONFATAL => qw(
268 except when called from a file which matches:
270 (caller)[1] =~ /^(?:t|xt|lib|blib)[\\\/]/
272 and when either C<.git>, C<.svn>, or C<.hg> is present in the current directory
273 (with the intention of only forcing extra tests on the author side) -- or when
274 C<.git>, C<.svn>, or C<.hg> is present two directories up along with
275 C<dist.ini> (which would indicate we are in a C<dzil test> operation, via
276 L<Dist::Zilla>) -- or when the C<PERL_STRICTURES_EXTRA> environment variable is
277 set, in which case it also does the equivalent of
281 no bareword::filehandles;
283 Note that C<PERL_STRICTURES_EXTRA> may at some point add even more tests, with
284 only a minor version increase, but any changes to the effect of C<use
285 strictures> in normal mode will involve a major version bump.
287 If any of the extra testing modules are not present, L<strictures> will
288 complain loudly, once, via C<warn()>, and then shut up. But you really
289 should consider installing them, they're all great anti-footgun tools.
293 I've been writing the equivalent of this module at the top of my code for
294 about a year now. I figured it was time to make it shorter.
296 Things like the importer in C<use Moose> don't help me because they turn
297 warnings on but don't make them fatal -- which from my point of view is
298 useless because I want an exception to tell me my code isn't warnings-clean.
300 Any time I see a warning from my code, that indicates a mistake.
302 Any time my code encounters a mistake, I want a crash -- not spew to STDERR
303 and then unknown (and probably undesired) subsequent behaviour.
305 I also want to ensure that obvious coding mistakes, like indirect object
306 syntax (and not so obvious mistakes that cause things to accidentally compile
307 as such) get caught, but not at the cost of an XS dependency and not at the
308 cost of blowing things up on another machine.
310 Therefore, L<strictures> turns on additional checking, but only when it thinks
311 it's running in a test file in a VCS checkout -- although if this causes
312 undesired behaviour this can be overridden by setting the
313 C<PERL_STRICTURES_EXTRA> environment variable.
315 If additional useful author side checks come to mind, I'll add them to the
316 C<PERL_STRICTURES_EXTRA> code path only -- this will result in a minor version
317 increase (e.g. 1.000000 to 1.001000 (1.1.0) or similar). Any fixes only to the
318 mechanism of this code will result in a sub-version increase (e.g. 1.000000 to
321 =head1 CATEGORY SELECTIONS
323 strictures does not enable fatal warnings for all categories.
329 Includes a warning that can cause your program to continue running
330 unintentionally after an internal fork. Not safe to fatalize.
334 Infinite recursion will end up overflowing the stack eventually anyway.
338 Triggers deep within perl, in places that are not safe to trap.
342 Triggers deep within perl, in places that are not safe to trap.
346 Includes a warning for using stat on a valid but suspect filename, ending in a
351 Experimental features are used intentionally.
355 Deprecations will inherently be added to in the future in unexpected ways,
356 so making them fatal won't be reliable.
360 Doesn't indicate an actual problem with the program, only that it may not
361 behave properly if run on a different machine.
365 Can't be fatalized. Also triggers very inconsistently, so we just disable it.
371 Depending on the version of strictures requested, different warnings will be
372 enabled. If no specific version is requested, the current version's behavior
373 will be used. Versions can be requested using perl's standard mechanism:
377 Or, by passing in a C<version> option:
379 use strictures version => 2;
386 use warnings FATAL => 'all';
387 use warnings NONFATAL => qw(
399 # and if in dev mode:
402 no bareword::filehandles;
404 Additionally, any warnings created by modules using L<warnings::register> or
405 C<warnings::register_categories()> will not be fatalized.
412 use warnings FATAL => 'all';
413 # and if in dev mode:
416 no bareword::filehandles;
422 This method does the setup work described above in L</DESCRIPTION>. Optionally
423 accepts a C<version> option to request a specific version's behavior.
427 This method traps the C<< strictures->VERSION(1) >> call produced by a use line
428 with a version number on it and does the version check.
430 =head1 EXTRA TESTING RATIONALE
432 Every so often, somebody complains that they're deploying via C<git pull>
433 and that they don't want L<strictures> to enable itself in this case -- and that
434 setting C<PERL_STRICTURES_EXTRA> to 0 isn't acceptable (additional ways to
435 disable extra testing would be welcome but the discussion never seems to get
438 In order to allow us to skip a couple of stages and get straight to a
439 productive conversation, here's my current rationale for turning the
440 extra testing on via a heuristic:
442 The extra testing is all stuff that only ever blows up at compile time;
443 this is intentional. So the oft-raised concern that it's different code being
444 tested is only sort of the case -- none of the modules involved affect the
445 final optree to my knowledge, so the author gets some additional compile
446 time crashes which he/she then fixes, and the rest of the testing is
447 completely valid for all environments.
449 The point of the extra testing -- especially C<no indirect> -- is to catch
450 mistakes that newbie users won't even realise are mistakes without
455 where foo is an & prototyped sub that you forgot to import -- this is
456 pernicious to track down since all I<seems> fine until it gets called
457 and you get a crash. Worse still, you can fail to have imported it due
458 to a circular require, at which point you have a load order dependent
459 bug which I've seen before now I<only> show up in production due to tiny
460 differences between the production and the development environment. I wrote
461 L<http://shadow.cat/blog/matt-s-trout/indirect-but-still-fatal/> to explain
462 this particular problem before L<strictures> itself existed.
464 As such, in my experience so far L<strictures>' extra testing has
465 I<avoided> production versus development differences, not caused them.
467 Additionally, L<strictures>' policy is very much "try and provide as much
468 protection as possible for newbies -- who won't think about whether there's
469 an option to turn on or not" -- so having only the environment variable
470 is not sufficient to achieve that (I get to explain that you need to add
471 C<use strict> at least once a week on freenode #perl -- newbies sometimes
472 completely skip steps because they don't understand that that step
475 I make no claims that the heuristic is perfect -- it's already been evolved
476 significantly over time, especially for 1.004 where we changed things to
477 ensure it only fires on files in your checkout (rather than L<strictures>-using
478 modules you happened to have installed, which was just silly). However, I
479 hope the above clarifies why a heuristic approach is not only necessary but
480 desirable from a point of view of providing new users with as much safety as
481 possible, and will allow any future discussion on the subject to focus on "how
482 do we minimise annoyance to people deploying from checkouts intentionally".
498 L<bareword::filehandles>
502 =head1 COMMUNITY AND SUPPORT
506 irc.perl.org #toolchain
508 (or bug 'mst' in query on there or freenode)
510 =head2 Git repository
512 Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is:
514 git clone git://git.shadowcat.co.uk/p5sagit/strictures.git
516 The web interface to the repository is at:
518 http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/strictures.git
522 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
526 Karen Etheridge (cpan:ETHER) <ether@cpan.org>
528 Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@gmail.com>
530 haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
534 Copyright (c) 2010 the strictures L</AUTHOR> and L</CONTRIBUTORS>
539 This library is free software and may be distributed under the same terms