« Policy/Documentation/ProhibitBadAproposMarkup.pm
↑ Summary
Policy/Documentation/ProhibitParagraphTwoDots.pm »
| 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 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 40 | ▶ | use List::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 40 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 40 | ▶ | use Perl::Critic::Utils 0.21; # version 0.21 for shebang_line() | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 40 | ▶ | use Perl::Critic::Pulp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 40 | ▶ | use Perl::Critic::Pulp::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 28 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 40 | our $VERSION = 99; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 40 | 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 | ▶ | }); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 40 | ▶ | use constant default_themes => ('pulp', 'cosmetic'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 40 | ▶ | use constant applies_to => 'PPI::Document'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # only ever gives one violation | ||||||||||||||||||||||||||||
| 43 | 40 | ▶ | use constant default_maximum_violations_per_document => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub violates { | ||||||||||||||||||||||||||||
| 46 | 7 | ▶ | my ($self, $elem, $document) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | ### ProhibitModuleShebang elem: $elem->content | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 7 | my $filename = $document->filename; | |||||||||||||||||||||||||||
| 50 | 7 | ▶ | (defined $filename && $filename =~ /\.pm$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $filename and $filename =~ /\.pm$/
MC/DC50%1/2
defined $filename and $filename =~ /\.pm$/
defined $filename
$filename =~ /\.pm$/
Truth table66%2/3
A and B
A= defined $filenameB= $filename =~ /\.pm$/
| |||||||||||||||||||||||||||||
| 51 | or return; # only .pm files are modules | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | ▶ | my $shebang = Perl::Critic::Utils::shebang_line($document) | ||||||||||||||||||||||||||
|
Condition100%2/2
Perl::Critic::Utils::shebang_line($document) || (return)
MC/DC100%1/1
Perl::Critic::Utils::shebang_line($document) || (return)
Perl::Critic::Utils::shebang_line($document)
Truth table100%2/2
A
A= Perl::Critic::Utils::shebang_line($document)
| |||||||||||||||||||||||||||||
| 54 | || return; # no #! at all | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | if ($self->{'_allow_bin_false'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | if ($shebang =~ m{^#!\s*/bin/false\s*$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | return; # /bin/false allowed | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 3 | return $self->violation ('Don\'t use #! in a module file', | |||||||||||||||||||||||||||
| 62 | '', | ||||||||||||||||||||||||||||
| 63 | $document); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 40 | 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 | ||||||||||||||||||||||||||||