File Coverage

blib/lib/Perl/Critic/Policy/Modules/ProhibitModuleShebang.pm
Criterion Covered Total %
statement 46 46 100.0
branch 6 6 100.0
condition 4 5 80.0
subroutine 14 14 100.0
pod 1 1 100.0
total 71 72 98.6


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2              
3             # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by the
5             # Free Software Foundation; either version 3, or (at your option) any later
6             # version.
7             #
8             # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11             # for more details.
12             #
13             # You should have received a copy of the GNU General Public License along
14             # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15              
16             package Perl::Critic::Policy::Modules::ProhibitModuleShebang;
17 40     40   32477 use 5.006;
  40         163  
18 40     40   223 use strict;
  40         97  
  40         829  
19 40     40   213 use warnings;
  40         98  
  40         1060  
20 40     40   217 use List::Util;
  40         81  
  40         2104  
21              
22 40     40   262 use base 'Perl::Critic::Policy';
  40         111  
  40         5374  
23 40     40   187962 use Perl::Critic::Utils 0.21; # version 0.21 for shebang_line()
  40         782  
  40         677  
24 40     40   36666 use Perl::Critic::Pulp;
  40         106  
  40         1011  
25 40     40   731 use Perl::Critic::Pulp::Utils;
  40         89  
  40         3028  
26              
27             # uncomment this to run the ### lines
28             #use Smart::Comments;
29              
30             our $VERSION = 99;
31              
32 40         2907 use constant supported_parameters =>
33             ({ name => 'allow_bin_false',
34             description => 'Whether to allow #!/bin/false',
35             behavior => 'boolean',
36             default_string => '1',
37 40     40   276 });
  40         131  
38 40     40   275 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         100  
  40         2405  
39 40     40   270 use constant default_themes => ('pulp', 'cosmetic');
  40         106  
  40         2348  
40 40     40   262 use constant applies_to => 'PPI::Document';
  40         90  
  40         2284  
41              
42             # only ever gives one violation
43 40     40   276 use constant default_maximum_violations_per_document => 1;
  40         126  
  40         9845  
44              
45             sub violates {
46 7     7 1 555198 my ($self, $elem, $document) = @_;
47             ### ProhibitModuleShebang elem: $elem->content
48              
49 7         24 my $filename = $document->filename;
50 7 100 66     139 (defined $filename && $filename =~ /\.pm$/)
51             or return; # only .pm files are modules
52              
53 6   100     23 my $shebang = Perl::Critic::Utils::shebang_line($document)
54             || return; # no #! at all
55              
56 4 100       263 if ($self->{'_allow_bin_false'}) {
57 3 100       13 if ($shebang =~ m{^#!\s*/bin/false\s*$}) {
58 1         4 return; # /bin/false allowed
59             }
60             }
61 3         14 return $self->violation ('Don\'t use #! in a module file',
62             '',
63             $document);
64             }
65              
66             1;
67             __END__
68              
69             =for stopwords filename boolean Ryde
70              
71             =head1 NAME
72              
73             Perl::Critic::Policy::Modules::ProhibitModuleShebang - don't put a #! line at the start of a module file
74              
75             =head1 DESCRIPTION
76              
77             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
78             add-on. It asks you not to use a C<#!> interpreter line in a F<.pm> module
79             file.
80              
81             #!/usr/bin/perl -w <-- bad
82             package Foo;
83             ...
84              
85             This C<#!> does nothing but might make a reader think it's supposed to be a
86             program instead of a module. Often the C<#!> is a leftover cut and paste
87             from a script into a module, perhaps when grabbing a copyright notice or
88             similar intro.
89              
90             Of course a module works the same with or without, so this policy is low
91             severity and under the "cosmetic" theme (see L<Perl::Critic/POLICY THEMES>).
92              
93             Only the first line of a file is a prospective C<#!> interpreter.
94             A C<#!> anywhere later is allowed, for example in code which generates other
95             code,
96              
97             sub foo {
98             print <<HERE;
99             #!/usr/bin/make <-- ok
100             # Makefile generated by Foo.pm - DO NOT EDIT
101             ...
102              
103             This policy applies only to F<.pm> files. Anything else, such as C<.pl> or
104             C<.t> scripts can have C<#!>, or not, in the usual way. Modules are
105             identified by the F<.pm> filename because it's hard to distinguish a module
106             from a script just by the content.
107              
108             =head2 Disabling
109              
110             If you don't care about this you can always disable C<ProhibitModuleShebang>
111             from your F<.perlcriticrc> in the usual way (see
112             L<Perl::Critic/CONFIGURATION>),
113              
114             [-Modules::ProhibitModuleShebang]
115              
116             =head1 CONFIGURATION
117              
118             =over 4
119              
120             =item C<allow_bin_false> (boolean, default true)
121              
122             If true then allow C<#!/bin/false> in module files.
123              
124             #! /bin/false <-- ok
125              
126             Such a C<#!> prevents executing the code if accidentally run as a script.
127             Whether you want this is a personal preference. It indicates a module is
128             not a script and so accords with C<ProhibitModuleShebang>, but in general it's
129             probably unnecessary.
130              
131             =back
132              
133             =head1 SEE ALSO
134              
135             L<Perl::Critic::Pulp>, L<Perl::Critic>
136              
137             =head1 HOME PAGE
138              
139             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
140              
141             =head1 COPYRIGHT
142              
143             Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
144              
145             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
146             under the terms of the GNU General Public License as published by the Free
147             Software Foundation; either version 3, or (at your option) any later
148             version.
149              
150             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
151             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
152             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
153             more details.
154              
155             You should have received a copy of the GNU General Public License along with
156             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>.
157              
158             =cut