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