File Coverage

blib/lib/Perl/Critic/Policy/Community/WarningsSwitch.pm
Criterion Covered Total %
statement 26 27 96.3
branch 4 4 100.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 4 5 80.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Community::WarningsSwitch;
2              
3 1     1   439 use strict;
  1         2  
  1         71  
4 1     1   7 use warnings;
  1         3  
  1         28  
5              
6 1     1   6 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         51  
7 1     1   390 use parent 'Perl::Critic::Policy';
  1         3  
  1         5  
8              
9             our $VERSION = 'v1.0.2';
10              
11 1     1   93 use constant DESC => 'Using -w switch';
  1         2  
  1         55  
12 1     1   6 use constant EXPL => 'Don\'t use -w (or -W), it\'s too eager. use warnings; instead.';
  1         2  
  1         235  
13              
14 8     8 0 19951 sub supported_parameters { () }
15 6     6 1 85 sub default_severity { $SEVERITY_LOW }
16 0     0 1 0 sub default_themes { 'community' }
17 8     8 1 27904 sub applies_to { 'PPI::Document' }
18              
19             sub violates {
20 8     8 1 63 my ($self, $elem) = @_;
21 8         40 my $shebang = $elem->first_token;
22 8 100 66     207 return () unless $shebang->isa('PPI::Token::Comment') and $shebang->content =~ m/^#!/;
23            
24 7 100       49 return $self->violation(DESC, EXPL, $elem) if $shebang->content =~ m/\h-[a-zA-Z]*[wW]/;
25            
26 1         9 return ();
27             }
28              
29             1;
30              
31             =head1 NAME
32              
33             Perl::Critic::Policy::Community::WarningsSwitch - Scripts should not use the -w
34             switch on the shebang line
35              
36             =head1 DESCRIPTION
37              
38             The C<-w> switch enables warnings globally in a perl program, including for any
39             modules that did not explicitly enable or disable any warnings. The C<-W>
40             switch enables warnings even for modules that explicitly disabled them. The
41             primary issue with this is enabling warnings for code that you did not write.
42             Some of these modules may not be designed to run with warnings enabled, but
43             still work fine. Instead, use L<warnings> within your own code only.
44              
45             #!/usr/bin/perl -w # not ok
46             #!/usr/bin/perl -W # not ok
47             use warnings; # ok
48              
49             =head1 AFFILIATION
50              
51             This policy is part of L<Perl::Critic::Community>.
52              
53             =head1 CONFIGURATION
54              
55             This policy is not configurable except for the standard options.
56              
57             =head1 AUTHOR
58              
59             Dan Book, C<dbook@cpan.org>
60              
61             =head1 COPYRIGHT AND LICENSE
62              
63             Copyright 2015, Dan Book.
64              
65             This library is free software; you may redistribute it and/or modify it under
66             the terms of the Artistic License version 2.0.
67              
68             =head1 SEE ALSO
69              
70             L<Perl::Critic>, L<warnings>