From: Matt S Trout Date: Fri, 4 Jun 2010 18:04:05 +0000 (+0100) Subject: first version of strictures.pm - code and brief docs X-Git-Tag: release_1.0.0~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2Fstrictures.git;a=commitdiff_plain;h=394c3a465392be170c22d19d05bb76ad4618007f first version of strictures.pm - code and brief docs --- 394c3a465392be170c22d19d05bb76ad4618007f diff --git a/AUTHOR b/AUTHOR new file mode 100644 index 0000000..6e8b9ba --- /dev/null +++ b/AUTHOR @@ -0,0 +1 @@ +Matt S Trout diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..3bebaae --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,9 @@ +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'strictures', + VERSION_FROM => 'lib/strictures.pm', + ABSTRACT_FROM => 'lib/strictures.pm', + AUTHOR => do { local (@ARGV) = 'AUTHOR'; <> }, + LICENSE => 'perl', +); diff --git a/lib/strictures.pm b/lib/strictures.pm new file mode 100644 index 0000000..f149054 --- /dev/null +++ b/lib/strictures.pm @@ -0,0 +1,107 @@ +package strictures; + +use strict; +use warnings FATAL => 'all'; + +our $VERSION = '1.000000'; # 1.0.0 + +sub VERSION { + for ($_[1]) { + last unless defined && !ref && int != 1; + die "Major version specified as $_ - this is strictures version 1"; + } + shift->SUPER::VERSION(@_); +} + +sub import { + strict->import; + warnings->import(FATAL => 'all'); + my $do_indirect = do { + if (exists $ENV{PERL_STRICTURES_EXTRA}) { + $ENV{PERL_STRICTURES_EXTRA} + } else { + !!($0 =~ /^x?t\/.*(?:load|compile|coverage).*\.t$/) + } + }; + if ($do_indirect) { + if (eval { require indirect; 1 }) { + indirect->unimport('FATAL'); + } else { + die "strictures.pm extra testing active but couldn't load indirect.pm: $@"; + } + } +} + +1; + +__END__ +=head1 NAME + +strictures - turn on strict and make all warnings fatal + +=head1 SYNOPSIS + + use strictures 1; + +is equivalent to + + use strict; + use warnings FATAL => 'all'; + +except when called from a file where $0 matches: + + /^x?t\/.*(?:load|compile|coverage).*\.t$/ + +or when the PERL_STRICTURES_EXTRA environment variable is set, in which +case + + use strictures 1; + +is equivalent to + + use strict; + use warnings FATAL => 'all'; + no indirect 'fatal'; + +Note that _EXTRA may at some point add even more tests, with only a minor +version increase, but any changes to the effect of 'use strictures' in +normal mode will involve a major version bump. + +=head1 DESCRIPTION + +I've been writing the equivalent of this module at the top of my code for +about a year now. I figured it was time to make it shorter. + +Things like the importer in 'use Moose' don't help me because they turn +warnings on but don't make them fatal - which from my point of view is +useless because I want an exception to tell me my code isn't warnings clean. + +Any time I see a warning from my code, that indicates a mistake. + +Any time my code encounters a mistake, I want a crash - not spew to STDERR +and then unknown (and probably undesired) subsequent behaviour. + +I also want to ensure that obvious coding mistakes, like indirect object +syntax (and not so obvious mistakes that cause things to accidentally compile +as such) get caught, but not at the cost of an XS dependency and not at the +cost of blowing things up on another machine. + +Therefore, strictures turns on indirect checking only when it thinks it's +running in a compilation (or pod coverage) test - though if this causes +undesired behaviour this can be overriden by setting the +PERL_STRICTURES_EXTRA environment variable. + +If additional useful author side checks come to mind, I'll add them to the +_EXTRA code path only - this will result in a minor version increase (i.e. +1.000000 to 1.001000 (1.1.0) or similar). Any fixes only to the mechanism of +this code will result in a subversion increas (i.e. 1.000000 to 1.000001 +(1.0.1)). + +If the behaviour of 'use strictures' in normal mode changes in any way, that +will constitute a major version increase - and the code already checks +when its version is tested to ensure that + + use strictures 1; + +will continue to only introduce the current set of strictures even if 2.0 is +installed.