blib/lib/warnings/everywhere.pm

filter: Statementstmt 88.0% 66 / 75 Branchbran 70.0% 14 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.4% 17 / 18 Podpod 100% 4 / 4 totaltotal 86.3% 101 / 117 CC 17 SCAR 11.3
CC
17
Cov
84%
_dump_mask
12.0
_check_warning_category
3.6
categories_enabled
3.0
CRAP 3.1
↑ Summary blib/lib/goddamn/warnings/anywhere.pm »
1 package warnings::everywhere;
2  
3 19 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '0.031';
9 7 $VERSION = eval $VERSION;
10  
11 19 ▶sub import {
Subroutine import: called CC=2 SCAR=7.6
12 19 my $package = shift;
13 19 for my $category (@_) {
14 0 enable_warning_category($category);
15 }
16 }
17  
18 261 ▶sub unimport {
Subroutine unimport: called CC=2 SCAR=6.9
19 261 my $package = shift;
20 261 for my $category (@_) {
21 264 disable_warning_category($category);
22 }
23 }
24  
25 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 NAME
28  
29 warnings::everywhere - a way of ensuring consistent global warning settings
30  
31 =head1 VERSION
32  
33 This is version 0.030.
34  
35 =head1 SYNOPSIS
36  
37 use strict;
38 use warnings;
39 no warnings::anywhere qw(uninitialized);
40
41 use Module::That::Spits::Out::Warnings;
42 use Other::Unnecessarily::Chatty::Module;
43  
44 use warnings::everywhere qw(uninitialized);
45 # Write your own bondage-and-discipline code that really, really
46 # cares about the difference between undef and the empty string
47  
48 =head1 DESCRIPTION
49  
50 Warnings are great - in your own code. Tools like prove, and libraries
51 like Moose and Modern::Perl, turn them on for you so you can spot things
52 like ambiguous syntax, variables you only used once, deprecated syntax
53 and other useful things.
54  
55 By default C<use warnings> turns on all warnings, including some that
56 you might not care about, like uninitialised variables. You could explicitly
57 say
58  
59 use warnings;
60 no warnings qw(uninitialized);
61  
62 or you could use a module like C<common::sense> which disables some warnings
63 and makes others fatal, or you could roll your own system. Either way,
64 for your own code, there are plenty of ways around unwanted warnings.
65  
66 Not so for other code, though.
67  
68 The test suite at $WORK produces a large number of 'use of uninitialized
69 variable' warnings from (at the last count) four separate modules. Some of
70 them are because warnings got switched on for that module,
71 even though the module itself didn't say anything about warnings
72 (probably because the test suite was run with prove).
73 Others are there because the module explicitly said C<use warnings>, and
74 then proceeded to blithely throw around variables without checking whether
75 they were defined first.
76  
77 Either way, this isn't my code, and it's not something I'm going to fix.
78 These warnings are just spam.
79  
80 This is where warnings::everywhere comes in.
81  
82 =head2 Usage
83  
84 At its simplest, say
85  
86 use warnings::everywhere qw(all);
87  
88 and all modules imported from there onwards will have all warnings switched
89 on. Modules imported previously will be unaffected. You can turn specific
90 warnings off by saying e.g.
91  
92 no warnings::everywhere qw(uninitialized);
93  
94 or, depending on how frustrated and/or grammatically-sensitive you happen
95 to be feeling,
96  
97 no warnings::anywhere qw(uninitialized);
98  
99 or
100  
101 no goddamn::warnings::anywhere qw(uninitialized);
102  
103 Parameters are the same as C<use warnings>: a list of categories
104 as per L<perllexwarn>, where C<all> means all warnings.
105  
106 =head2 Limitations
107  
108 warnings::everywhere works by fiddling with the contents of the global hashes
109 %warnings::Bits and %warnings::DeadBits. As such, there are limitations on
110 what it can and cannot do:
111  
112 =over
113  
114 =item It cannot affect modules that are already loaded.
115  
116 If you say
117  
118 use Chatty::Module;
119 no warnings::anywhere qw(uninitialized);
120  
121 that's no good - Chatty::Module has already called C<use warnings> and
122 uninitialized variables was in the list of enabled warnings at that point,
123 so it will still spam you.
124  
125 Similarly, this is no help:
126  
127 use Module::That::Uses::Chatty::Module;
128 no warnings::anywhere qw(uninitialized);
129 use Chatty::Module;
130  
131 Chatty::Module was pulled in by that other module already by the time
132 perl gets to your use statement, so it's ignored.
133  
134 =item It's vulnerable to anything that sets $^W
135  
136 Any code that sets the global variable $^W, rather than saying C<use warnings>
137 or C<warnings->import>, will turn on all warnings everywhere, bypassing the
138 changes warnings::everywhere makes. This also includes any code that sets -w
139 via the shebang.
140  
141 Any change to warnings by any of the warnings::anywhere code will turn off $^W
142 again, whether it's a use statement or an explicit call to
143 L<disable_warning_category> or similar.
144  
145 Any module that claims to enable warnings for you is potentially suspect
146 - Moose is fine, but Dancer sets $^W to 1 as soon as it loads, even if your
147 configuration subsequently disables import_warnings.
148  
149 =item It cannot make all modules use warnings
150  
151 All it does is fiddle with the exact behaviour of C<use warnings>,
152 so a module that doesn't say C<use warnings>, or import a module that
153 injects warnings like Moose, will be unaffected.
154  
155 =item It's not lexical
156  
157 While it I<looks> like a pragma, it's not - it fiddles with global settings,
158 after all. So you can't say
159  
160 {
161 no warnings::anywhere qw(uninitialized);
162 Chatty::Module->do_things;
163 }
164 Unchatty::Module->do_stuff(undef);
165  
166 and expect to get a warning from the last line. That warning's been
167 turned off for good.
168  
169 =item It won't work for compile-time warnings
170  
171 It works by fiddling with the (global) %warnings::Bits variable, and that's
172 fine for run-time warnings. But if you say e.g.
173  
174 use warnings;
175 use experimental 'signatures';
176 no warnings::anywhere 'experimental::signatures';
177 use Moose; # or Moo, or Dancer2, or... or...
178  
179 that won't work, as when Moose (or Moo, or Dancer2 etc.) injects all warnings
180 into your package, it turns everything back on, and warnings::everywhere
181 can't thwart that.
182  
183 (Previous versions of warnings::everywhere I<tried> to thwart this by
184 basically a source filter, but that proved untenable.)
185  
186 The solution is to remember that Moose, Moo, Dancer2 etc. only turn on
187 these compile-time warnings I<for this particular package>, so just say e.g.
188  
189 use warnings;
190 use Moose; # or Moo etc.
191 use experimental 'signatures';
192  
193 =back
194  
195 =head1 SUBROUTINES
196  
197 warnings::anywhere provides the following functions, mostly for diagnostic
198 use. They are not exported or exportable.
199  
200 =over
201  
202 =item categories_enabled
203  
204 Out: @categories
205  
206 Returns a sorted list of warning categories enabled globally. Before you've
207 fiddled with anything, this will be the list of warning categories from
208 L<perllexwarn>, minus C<all> which isn't a category itself.
209  
210 Fatal warnings are ignored for the purpose of this function.
211  
212 =cut
213  
214 1262 ▶sub categories_enabled {
Subroutine categories_enabled: called CC=3 SCAR=11.0
Pod documented
215 1262 my @categories;
216 1262 for my $category (_warning_categories()) {
217 push @categories, $category
218 if _is_bit_set($warnings::Bits{$category},
219 107270 ▶ $warnings::Offsets{$category});
Branch: 1 branch
BranchTrueFalseTotal
if _is_bit_set($warnings::Bits{$category}, $warnings::Offsets{$category}) 106629 641 107270
220 }
221 1262 return @categories;
222 }
223  
224 =item categories_disabled
225  
226 Out: @categories
227  
228 Returns a sorted list of warning categories disabled globally. Before
229 you've fiddled with anything, this will be the empty list.
230  
231 Fatal warnings are ignored for the purpose of this function.
232  
233 =cut
234  
235 1393 ▶sub categories_disabled {
Subroutine categories_disabled: called CC=3 SCAR=11.0
Pod documented
236 1393 my @categories;
237 1393 for my $category (_warning_categories()) {
238 push @categories, $category
239 if !_is_bit_set($warnings::Bits{$category},
240 118405 ▶ $warnings::Offsets{$category});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_bit_set($warnings::Bits{$category}, $warnings::Offsets{$category}) 259 118146 118405
241 }
242 1393 return @categories;
243 }
244  
245 2668 ▶sub _warning_categories {
Subroutine _warning_categories: called CC=1 SCAR=0.0
246 2668 my @categories = sort grep { $_ ne 'all' } keys %warnings::Offsets;
247 2668 return @categories;
248 }
249  
250 =item enable_warning_category
251  
252 In: $category
253  
254 Supplied with a valid warning category, enables it for all future
255 uses of C<use warnings>.
256  
257 =cut
258  
259 624 ▶sub enable_warning_category {
Subroutine enable_warning_category: called CC=2 SCAR=7.0
Pod documented
260 624 my ($category) = @_;
261  
262 624 ▶ _check_warning_category($category) or return;
Branch: 1 branch
BranchTrueFalseTotal
_check_warning_category($category) or return 624 0 624
263 624 _set_category_mask($category, 1);
264 624 return 1;
265 }
266  
267 1258 ▶sub _set_category_mask {
Subroutine _set_category_mask: called CC=3 SCAR=11.0
268 1258 my ($category, $bit_value) = @_;
269  
270 # Set or unset the specific category bit value (e.g. if
271 # someone says use warnings qw(uninitialized) or
272 # no warnings qw(uninitialized)).
273 _set_bit_mask(\($warnings::Bits{$category}),
274 1258 $warnings::Offsets{$category}, $bit_value);
275  
276 # Compute what the bitmask for all should be.
277 1258 $warnings::Bits{all} = _bitmask_categories_enabled();
278  
279 # If we've enabled all categories, we should probably set
280 # the all bit as well, just for tidiness.
281 1258 ▶ if ($bit_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit_value) 624 634 1258
282 624 ▶ if (!categories_disabled()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (categories_disabled()) 622 2 624
283 622 _set_bit_mask(\$warnings::Bits{all}, $warnings::Offsets{all}, 1);
284 }
285 }
286 ### TODO: fatal warnings
287  
288 # Finally, if someone specified the -w flag (which turns on all
289 # warnings, globally), turn it off.
290 1258 $^W = 0;
291 }
292  
293 =item disable_warning_category
294  
295 In: $category
296  
297 Supplied with a valid warning category, disables it for future
298 uses of C<use warnings> - even calls to explicitly enable it.
299  
300 =cut
301  
302 634 ▶sub disable_warning_category {
Subroutine disable_warning_category: called CC=2 SCAR=7.0
Pod documented
303 634 my ($category) = @_;
304  
305 634 ▶ _check_warning_category($category) or return;
Branch: 1 branch
BranchTrueFalseTotal
_check_warning_category($category) or return 634 0 634
306 634 _set_category_mask($category, 0);
307 634 return 1;
308 }
309  
310 1258 ▶sub _bitmask_categories_enabled {
Subroutine _bitmask_categories_enabled: called CC=2 SCAR=6.9
311 1258 my $mask;
312 1258 for my $category_enabled (categories_enabled()) {
313 106291 _set_bit_mask(\$mask, $warnings::Offsets{$category_enabled}, 1)
314 }
315 1258 return $mask;
316 }
317  
318 108171 ▶sub _set_bit_mask {
Subroutine _set_bit_mask: called CC=2 SCAR=6.9
319 108171 my ($mask_ref, $bit_num, $bit_value) = @_;
320  
321 # First get the correct byte from the mask, then set that byte's
322 # bit accordingly.
323 # We have to do it this way as warning masks are hundreds of bits wide,
324 # which neither a 32- nor a 64-bit Perl can deal with natively.
325 # The mask might not be long enough, so pad it with null bytes if
326 # we need to first.
327 108171 my $byte_num = int($bit_num / 8);
328 108171 ▶ while (length($$mask_ref) < $byte_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$mask_ref < $byte_num) 25160 108171 133331
329 25160 $$mask_ref .= "\x0";
330 }
331 108171 my $byte_value = substr($$mask_ref, $byte_num, 1);
332 108171 vec($byte_value, $bit_num % 8, 1) = $bit_value;
333 108171 substr($$mask_ref, $byte_num, 1) = $byte_value;
334 108171 return $$mask_ref;
335 }
336  
337 225678 ▶sub _is_bit_set {
Subroutine _is_bit_set: called CC=1 SCAR=0.0
338 225678 my ($mask, $bit_num) = @_;
339  
340 225678 return vec($mask, int($bit_num / 8), 8) & (1 << ($bit_num % 8));
341 }
342  
343 0 ▶sub _dump_mask {
Subroutine _dump_mask: not called CC=3 SCAR=24.8
344 0 my ($mask) = @_;
345  
346 0 my $output;
347 0 for my $byte_num (reverse 0..15) {
348 0 $output .= sprintf('%08b', vec($mask, $byte_num, 8));
349 0 ▶ $output .= ($byte_num % 4 == 0 ? "\n" : '|');
Branch: 1 branch
BranchTrueFalseTotal
$byte_num % 4 == 0 ? : 0 0 0
350 }
351 0 return $output;
352 }
353  
354 1258 ▶sub _check_warning_category {
Subroutine _check_warning_category: called CC=3 SCAR=12.7
355 1258 my ($category) = @_;
356  
357 1258 ▶ return if $category eq 'all';
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "all" 0 1258 1258
358 1258 ▶ if (!exists $warnings::Offsets{$category}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $warnings::Offsets{$category}) 0 1258 1258
359 0 carp "Unrecognised warning category $category";
360 0 return;
361 }
362 1258 return 1;
363 }
364  
365 =back
366  
367 =head1 TO DO
368  
369 Support for fatal warnings, possibly.
370 It's possible it doesn't behave correctly when passed 'all'.
371  
372 =head1 DIAGNOSTICS
373  
374 =over
375  
376 =item Unrecognised warning category $category
377  
378 Your version of Perl doesn't recognise the warning category $category.
379 Either you're using a different version of Perl than you thought, or a
380 third-party module that defined that warning isn't loaded yet.
381  
382 =back
383  
384 =head1 SEE ALSO
385  
386 L<common::sense>
387  
388 =head1 AUTHOR
389  
390 Sam Kington <skington@cpan.org>
391  
392 The source code for this module is hosted on GitHub
393 L<https://github.com/skington/warnings_everywhere> - this is probably the
394 best place to look for suggestions and feedback.
395  
396 =head1 COPYRIGHT
397  
398 Copyright (c) 2013 Sam Kington.
399  
400 =head1 LICENSE
401  
402 This library is free software and may be distributed under the same terms as
403 perl itself.
404  
405 =cut
406  
407 7 1;
↑ Summary blib/lib/goddamn/warnings/anywhere.pm »