File Coverage

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


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