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