fix warnings, improve author side error
[p5sagit/strictures.git] / lib / strictures.pm
CommitLineData
394c3a46 1package strictures;
2
3use strict;
4use warnings FATAL => 'all';
5
0925b84b 6our $VERSION = '1.001001'; # 1.1.1
394c3a46 7
8sub VERSION {
9 for ($_[1]) {
10 last unless defined && !ref && int != 1;
11 die "Major version specified as $_ - this is strictures version 1";
12 }
0925b84b 13 # disable this since Foo->VERSION(undef) correctly returns the version
14 # and that can happen either if our caller passes undef explicitly or
15 # because the for above autovivified $_[1] - I could make it stop but
16 # it's pointless since we don't want to blow up if the caller does
17 # something valid either.
18 no warnings 'uninitialized';
394c3a46 19 shift->SUPER::VERSION(@_);
20}
21
22sub import {
23 strict->import;
24 warnings->import(FATAL => 'all');
25 my $do_indirect = do {
26 if (exists $ENV{PERL_STRICTURES_EXTRA}) {
27 $ENV{PERL_STRICTURES_EXTRA}
28 } else {
dca7f184 29 !!($0 =~ /^x?t\/.*(?:load|compile|coverage|use_ok).*\.t$/
30 and (-e '.git' or -e '.svn'))
394c3a46 31 }
32 };
33 if ($do_indirect) {
34 if (eval { require indirect; 1 }) {
eae006ee 35 indirect->unimport(':fatal');
394c3a46 36 } else {
0925b84b 37 die "strictures.pm extra testing active but couldn't load indirect.pm
38Extra testing is auto-enabled in checkouts only, so if you're the author
39of a strictures using module you should 'cpan indirect' but the module
40is not required by your users.
41Error loading indirect.pm was: $@";
394c3a46 42 }
43 }
44}
45
461;
47
48__END__
49=head1 NAME
50
51strictures - turn on strict and make all warnings fatal
52
53=head1 SYNOPSIS
54
55 use strictures 1;
56
57is equivalent to
58
59 use strict;
60 use warnings FATAL => 'all';
61
62except when called from a file where $0 matches:
63
dca7f184 64 /^x?t\/.*(?:load|compile|coverage|use_ok).*\.t$/
394c3a46 65
dca7f184 66and when either '.git' or '.svn' is present in the current directory (with
67the intention of only forcing extra tests on the author side) - or when the
68PERL_STRICTURES_EXTRA environment variable is set, in which case
394c3a46 69
70 use strictures 1;
71
72is equivalent to
73
74 use strict;
75 use warnings FATAL => 'all';
76 no indirect 'fatal';
77
78Note that _EXTRA may at some point add even more tests, with only a minor
79version increase, but any changes to the effect of 'use strictures' in
80normal mode will involve a major version bump.
81
17b03f2e 82Be aware: THIS MEANS INDIRECT IS REQUIRED FOR AUTHORS OF STRICTURES USING
83CODE - but not by end users thereof.
84
394c3a46 85=head1 DESCRIPTION
86
87I've been writing the equivalent of this module at the top of my code for
88about a year now. I figured it was time to make it shorter.
89
90Things like the importer in 'use Moose' don't help me because they turn
91warnings on but don't make them fatal - which from my point of view is
92useless because I want an exception to tell me my code isn't warnings clean.
93
94Any time I see a warning from my code, that indicates a mistake.
95
96Any time my code encounters a mistake, I want a crash - not spew to STDERR
97and then unknown (and probably undesired) subsequent behaviour.
98
99I also want to ensure that obvious coding mistakes, like indirect object
100syntax (and not so obvious mistakes that cause things to accidentally compile
101as such) get caught, but not at the cost of an XS dependency and not at the
102cost of blowing things up on another machine.
103
104Therefore, strictures turns on indirect checking only when it thinks it's
105running in a compilation (or pod coverage) test - though if this causes
106undesired behaviour this can be overriden by setting the
107PERL_STRICTURES_EXTRA environment variable.
108
109If additional useful author side checks come to mind, I'll add them to the
110_EXTRA code path only - this will result in a minor version increase (i.e.
1111.000000 to 1.001000 (1.1.0) or similar). Any fixes only to the mechanism of
112this code will result in a subversion increas (i.e. 1.000000 to 1.000001
113(1.0.1)).
114
115If the behaviour of 'use strictures' in normal mode changes in any way, that
116will constitute a major version increase - and the code already checks
117when its version is tested to ensure that
118
119 use strictures 1;
120
121will continue to only introduce the current set of strictures even if 2.0 is
122installed.
eae006ee 123
124=head1 METHODS
125
126=head2 import
127
128This method does the setup work described above in L</DESCRIPTION>
129
130=head2 VERSION
131
132This method traps the strictures->VERSION(1) call produced by a use line
133with a version number on it and does the version check.
134
135=head1 COMMUNITY AND SUPPORT
136
137=head2 IRC channel
138
139irc.perl.org #toolchain
140
141(or bug 'mst' in query on there or freenode)
142
143=head2 Git repository
144
145Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is:
146
147 git clone git://git.shadowcat.co.uk/p5sagit/strictures.git
148
149=head1 AUTHOR
150
151Matt S. Trout <mst@shadowcat.co.uk>
152
153=head1 CONTRIBUTORS
154
155None required yet. Maybe this module is perfect (hahahahaha ...).
156
157=head1 COPYRIGHT
158
159Copyright (c) 2010 the strictures L</AUTHOR> and L</CONTRIBUTORS>
160as listed above.
161
162=head1 LICENSE
163
164This library is free software and may be distributed under the same terms
165as perl itself.
166
167=cut