File Coverage

blib/lib/Perl/Critic/Policy/logicLAB/ProhibitShellDispatch.pm
Criterion Covered Total %
statement 38 38 100.0
branch 10 10 100.0
condition 5 6 83.3
subroutine 10 10 100.0
pod 2 2 100.0
total 65 66 98.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::logicLAB::ProhibitShellDispatch;
2              
3 3     3   2172 use strict;
  3         3  
  3         94  
4 3     3   17 use warnings;
  3         4  
  3         95  
5 3     3   13 use base 'Perl::Critic::Policy';
  3         5  
  3         2143  
6 3     3   467607 use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM };
  3         4  
  3         288  
7 3     3   65 use 5.008;
  3         8  
8              
9             our $VERSION = '0.05';
10              
11             Readonly::Scalar my $EXPL => q{Use Perl builtin instead};
12              
13 3     3   15 use constant supported_parameters => ();
  3         5  
  3         211  
14 3     3   14 use constant default_severity => $SEVERITY_MEDIUM;
  3         4  
  3         154  
15 3     3   11 use constant default_themes => qw(logiclab);
  3         4  
  3         686  
16              
17             sub applies_to {
18              
19             return (
20 6     6 1 14205267 qw(
21             PPI::Statement
22             PPI::Token::QuoteLike::Command
23             PPI::Token::QuoteLike::Backtick
24             )
25             );
26             }
27              
28             sub violates {
29 10     10 1 199 my ( $self, $elem ) = @_;
30              
31             #first element PPI::Token::Word (system or exec)
32 10 100       43 if ( ref $elem eq 'PPI::Statement' ) {
33              
34 7         31 my $word = $elem->find_first('PPI::Token::Word');
35              
36 7 100 100     1773 if ( $word
37             and $word =~ m{
38             \A #beginning of string
39             (system|exec)
40             \Z #end of string
41             }xsm
42             )
43             {
44              
45             #previous significant sibling
46 3         55 my $sibling = $word->sprevious_sibling;
47              
48 3 100 66     80 if ( $sibling and $sibling eq '->' ) {
49 1         18 return;
50             } else {
51 2         16 return $self->violation(
52             q{Do not use 'system' or 'exec' statements},
53             $EXPL, $elem );
54             }
55             }
56 4         33 return;
57             }
58              
59 3 100       11 if ( ref $elem eq 'PPI::Token::QuoteLike::Command' ) {
60 1         8 return $self->violation( q{Do not use 'qx' statements}, $EXPL,
61             $elem );
62             }
63              
64 2 100       9 if ( ref $elem eq 'PPI::Token::QuoteLike::Backtick' ) {
65 1         4 return $self->violation( q{Do not use 'backticks' statements},
66             $EXPL, $elem );
67             }
68              
69 1         2 return;
70             }
71              
72             1;
73              
74             __END__
75              
76             =pod
77              
78             =begin markdown
79              
80             [![CPAN version](https://badge.fury.io/pl/Perl-Critic-Policy-logicLAB-ProhibitShellDispatch.svg)](http://badge.fury.io/pl/Perl-Critic-Policy-logicLAB-ProhibitShellDispatch)
81             [![Build Status](https://travis-ci.org/jonasbn/pcplpsd.svg?branch=master)](https://travis-ci.org/jonasbn/pcplpsd)
82             [![Coverage Status](https://coveralls.io/repos/jonasbn/pcplpsd/badge.png)](https://coveralls.io/r/jonasbn/pcplpsd)
83              
84             =end markdown
85              
86             =head1 NAME
87              
88             Perl::Critic::Policy::logicLAB::ProhibitShellDispatch - simple policy prohibiting shell dispatching
89              
90             =head1 AFFILIATION
91              
92             This policy is a policy in the L<Perl::Critic::logicLAB> distribution.
93              
94             =head1 VERSION
95              
96             This documentation describes version 0.05
97              
98             =head1 DESCRIPTION
99              
100             Using Perl builtins to dispatch to external shell commands are not particularly
101             portable. This policy aims to assist the user in identifying these critical
102             spots in the code and exchange these for pure-perl solutions and CPAN
103             distributions.
104              
105             The policy scans for: system, exec, qx and the use of backticks, some basic examples.
106              
107             system "touch $0.lock";
108            
109             exec "touch $0.lock";
110            
111             my $hostname = qx/hostname/;
112            
113             my $hostname = `hostname`;
114              
115             Instead use the Perl builtins or CPAN distributions. This will make you distribution
116             easier to control and easier to distribute across platforms.
117              
118             #hostname
119             use Net::Domain qw(hostname);
120              
121             Using CPAN distributions and Perl builtins makes it easier to distribute your
122             code and defined you requirements to platforms in your build system.
123              
124             Additional examples and remedies are most welcome, since I would love to write
125             a 101 demonstrating violations and their remedies.
126              
127             =head1 CONFIGURATION AND ENVIRONMENT
128              
129             This Policy is not configurable except for the standard options.
130            
131             =head1 DEPENDENCIES AND REQUIREMENTS
132              
133             =over
134              
135             =item * L<Perl> version 5.8.0
136              
137             =item * L<Perl::Critic>
138              
139             =item * L<Perl::Critic::Utils>
140              
141             =item * L<Readonly>
142              
143             =item * L<Test::More>
144              
145             =item * L<Test::Perl::Critic>
146              
147             =back
148              
149             =head1 INCOMPATIBILITIES
150              
151             This distribution has no known incompatibilities.
152              
153             =head1 BUGS AND LIMITATIONS
154              
155             This distribution has no known bugs or limitations.
156              
157             As pointed out in bug report RT:91542, some modules and components might
158             implement methods/routines holding names similar to the builtins C<system>,
159             C<exec>, C<qx> and similar. I had not anticipated this when first implementing
160             the policy and I expect there will be more cases where the current implementation
161             does not handle this well, please file a bugreport if you run into one of these
162             issues and I will investigate and address accordingly.
163              
164             =head1 BUG REPORTING
165              
166             Please use Requets Tracker for bug reporting:
167              
168             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic-Policy-logicLAB-ProhibitShellDispatch
169              
170             =head1 TEST AND QUALITY
171              
172             The following policies have been disabled for this distribution
173              
174             =over
175              
176             =item * L<Perl::Crititc::Policy::ValuesAndExpressions::ProhibitConstantPragma>
177              
178             =item * L<Perl::Crititc::Policy::NamingConventions::Capitalization>
179              
180             =item * L<Documentation::RequirePodLinksIncludeText>
181              
182             =back
183              
184             See also F<t/perlcriticrc>
185              
186             =head2 TEST COVERAGE
187            
188             ---------------------------- ------ ------ ------ ------ ------ ------ ------
189             File stmt bran cond sub pod time total
190             ---------------------------- ------ ------ ------ ------ ------ ------ ------
191             .../ProhibitShellDispatch.pm 100.0 100.0 83.3 100.0 100.0 100.0 98.5
192             Total 100.0 100.0 83.3 100.0 100.0 100.0 98.5
193             ---------------------------- ------ ------ ------ ------ ------ ------ ------
194              
195             =head1 SEE ALSO
196              
197             =over
198              
199             =item * L<http://logiclab.jira.com/wiki/display/PCPLPSD/Home>, project Wiki
200              
201             =back
202              
203             =head1 AUTHOR
204              
205             =over
206              
207             =item * Jonas B. Nielsen, jonasbn C<< <jonasbn@cpan.org> >>
208              
209             =back
210              
211             =head1 ACKNOWLEDGEMENTS
212              
213             =over
214              
215             =item * Johan the Olive, bug reporting on Net::OpenSSH's system (RT:91542)
216              
217             =item * Adam Kennedy, author of PPI
218              
219             =item * Jeffrey Ryan Thalhammer, author of Perl::Critic
220              
221             =back
222              
223             =head1 COPYRIGHT
224              
225             Perl::Critic::Policy::logicLAB::ProhibitShellDispatch is (C) by Jonas B. Nielsen, (jonasbn) 2013-2015
226              
227             Perl::Critic::Policy::logicLAB::ProhibitShellDispatch is released under the artistic license 2.0
228              
229             =cut