blib/lib/Script/isAperlScript.pm

filter: Statementstmt 15.2% 11 / 72 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod 100% 3 / 3 totaltotal 14.2% 17 / 119 CC 20 SCAR 43.4
CC
20
Cov
10%
stringIsAperlScript
132.0
isAperlScript
56.0
new
20.0
CRAP 77.1
↑ Summary
1 package Script::isAperlScript;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Script::isAperlScript - This does a basic check if something is a perl script or not.
10  
11 =head1 VERSION
12  
13 Version 1.0.0
14  
15 =cut
16  
17 2 our $VERSION = '1.0.0';
18  
19 =head1 SYNOPSIS
20  
21 This module does a basic check to see if something is a perl script.
22  
23 By default it checks for the paths below.
24  
25 /^\#\!\/usr\/bin\/perl/
26 /^\#\!\/usr\/bin\/suidperl/
27 /^\#\!\/usr\/local\/bin\/perl/
28 /^\#\!\/usr\/local\/bin\/suidperl/
29  
30 This will also match stuff like "#!/usr/local/bin/perl5.8.9".
31  
32 If {env=>1} is given to the new method, the checks below are done.
33  
34 /^\#!\/usr\/bin\/env.*perl/
35  
36 If {any=>1} is given to the new method, the checks below are done.
37  
38 /^\#!\/.*perl/
39  
40 =head1 METHODS
41  
42 =head2 new
43  
44 This initiates the object.
45  
46 =head3 args hash
47  
48 =head4 env
49  
50 Allow env based scripts.
51  
52 By default this is false as it can't be trusted.
53  
54 =head4 any
55  
56 This does very loose matching.
57  
58 By default this is false as it can't be trusted.
59  
60 my $checker=Script::isAperlScript->new( \%args );
61  
62 =cut
63  
64 0 ▶sub new{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
65 0 my %args;
66 0 ▶ if ( defined( $_[1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
67 0 %args=%{$_[1]};
68 }
69  
70 0 my $self={
71 error=>0,
72 errorString=>0,
73 env=>0,
74 any=>0,
75 errorExtra=>{
76 flags=>{
77 2=>'noString',
78 3=>'doesNotExist',
79 4=>'notReadable',
80 5=>'fileNotSpecified',
81 6=>'noFile',
82 7=>'notAfile',
83 },
84 },
85 };
86 0 bless $self;
87  
88 0 ▶ if ( $args{env} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"env"}) 0 0 0
89 0 $self->{env}=1;
90 }
91  
92 0 ▶ if ( $args{any} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"any"}) 0 0 0
93 0 $self->{any}=1;
94 }
95  
96 0 return $self;
97 }
98  
99 =head2 isAperlScript
100  
101 This checks if a file is a Perl script.
102  
103 Only one arguement is taken and it is the string in question.
104  
105 In regards to the returned value, see the section "RETURN" for more information.
106  
107 my $returned=isAperlScript($file);
108 if(!$returned){
109 print "It returned false so there for it is a perl script.\n";
110 }
111  
112 =cut
113  
114 0 ▶sub isAperlScript{
Subroutine isAperlScript: not called CC=7 SCAR=40.3
Pod documented
115 0 my $self=$_[0];
116 0 my $file=$_[1];
117  
118 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
119 0 return undef;
120 }
121  
122 #make sure a file is specified
123 0 ▶ if (!defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
124 0 $self->{error}=5;
125 0 $self->{errorString}='No file defined';
126 0 return undef;
127 }
128 #make sure it exists
129 0 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
130 0 $self->{error}=3;
131 0 $self->{errorString}='The file, "'.$file.'", does not exist';
132 0 return undef;
133 }
134  
135 #it is not a file
136 0 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
137 0 $self->{error}=7;
138 0 $self->{errorString}='"'.$file.'" is not a file';
139 0 return undef;
140 }
141  
142 #make sure it is readable
143 0 ▶ if (! -r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 0 0
144 0 $self->{error}=4;
145 0 $self->{errorString}='"'.$file.'" is not readable';
146 0 return undef;
147 }
148  
149 #try to open it
150 0 ▶ if (open(THEFILE, '<', $file )) {
Branch: 1 branch
BranchTrueFalseTotal
if (open THEFILE, "<", $file) 0 0 0
151 0 my $string=join("", <THEFILE>);
152 0 close(THEFILE);
153 0 return $self->stringIsAperlScript($string);
154 }
155  
156 #it could not be opened
157 0 $self->{error}=6;
158 0 $self->{errorString}='"'.$file.'" could not be opened';
159 0 return undef;
160 }
161  
162 =head2 stringIsAperlScript
163  
164 This checks if a string is a Perl script.
165  
166 Only one arguement is taken and it is the string in question.
167  
168 In regards to the returned value, see the section "RETURN" for more information.
169  
170 my $returned=stringIsAperlScript($string);
171 if(!$returned){
172 print "It returned false so there for it is a perl script.\n";
173 }
174  
175 =cut
176  
177 0 ▶sub stringIsAperlScript{
Subroutine stringIsAperlScript: not called CC=11 SCAR=48.8
Pod documented
178 0 my $self=$_[0];
179 0 my $string=$_[1];
180  
181 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
182 0 return undef;
183 }
184  
185 #make sure a string is specified
186 0 ▶ if (!defined( $string )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 0 0
187 0 $self->{error}=2;
188 0 $self->{errorString}='No string defined';
189 0 return undef;
190 }
191  
192 #check if it should possibly do the any check
193 0 ▶ if ( $self->{any} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"any"}) 0 0 0
194 0 ▶ if ($string =~ /^\#\!\/.*perl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#\!/.*perl]) 0 0 0
195 0 return 1;
196 }
197 }
198
199 #checks if it makes #!/usr/bin/perl
200 0 ▶ if ($string =~ /^\#\!\/usr\/bin\/perl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#\!/usr/bin/perl]) 0 0 0
201 0 return 1;
202 }
203  
204 #checks if it makes #!/usr/bin/suidperl
205 0 ▶ if ($string =~ /^\#\!\/usr\/bin\/suidperl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#\!/usr/bin/suidperl]) 0 0 0
206 0 return 1;
207 }
208  
209 #checks if it makes #!/usr/local/bin/perl
210 0 ▶ if ($string =~ /^\#\!\/usr\/local\/bin\/perl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#\!/usr/local/bin/perl]) 0 0 0
211 0 return 1;
212 }
213  
214 #checks if it makes #!/usr/local/bin/suidperl
215 0 ▶ if ($string =~ /^\#\!\/usr\/local\/bin\/suidperl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#\!/usr/local/bin/suidperl]) 0 0 0
216 0 return 1;
217 }
218  
219 #check if it should possibly do the env check
220 0 ▶ if ( $self->{env} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"env"}) 0 0 0
221 0 ▶ if ( $string =~ /^\#!\/usr\/bin\/env.*perl/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ m[^\#!/usr/bin/env.*perl]) 0 0 0
222 0 return 1;
223 }
224 }
225  
226 #not a perl script
227 0 return undef;
228 }
229  
230 =head1 ERROR CODES/FLAGS/HANDLING
231  
232 The easiest way to check is to verify the returned value is false.
233  
234 Error handling is provided by L<Error::Helper>.
235  
236 =head2 2, noString
237  
238 The string is not defined.
239  
240 =head2 3, doesNotExist
241  
242 The file does not exist.
243  
244 =head2 4, notReadable
245  
246 The file is not readable.
247  
248 =head2 5, fileNotSpecified
249  
250 No file specified.
251  
252 =head2 6, noFile
253  
254 The file could not be opened.
255  
256 =head2 7, notAfile
257  
258 The specified file is not a file.
259  
260 =head1 AUTHOR
261  
262 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
263  
264 =head1 BUGS
265  
266 Please report any bugs or feature requests to C<bug-script-isaperlscript at rt.cpan.org>, or through
267 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Script-isAperlScript>. I will be notified, and then you'll
268 automatically be notified of progress on your bug as I make changes.
269  
270  
271  
272  
273 =head1 SUPPORT
274  
275 You can find documentation for this module with the perldoc command.
276  
277 perldoc Script::isAperlScript
278  
279  
280 You can also look for information at:
281  
282 =over 4
283  
284 =item * RT: CPAN's request tracker
285  
286 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Script-isAperlScript>
287  
288 =item * AnnoCPAN: Annotated CPAN documentation
289  
290 L<http://annocpan.org/dist/Script-isAperlScript>
291  
292 =item * CPAN Ratings
293  
294 L<http://cpanratings.perl.org/d/Script-isAperlScript>
295  
296 =item * Search CPAN
297  
298 L<http://search.cpan.org/dist/Script-isAperlScript/>
299  
300 =back
301  
302  
303 =head1 ACKNOWLEDGEMENTS
304  
305  
306 =head1 COPYRIGHT & LICENSE
307  
308 Copyright 2012 Zane C. Bowers-Hadley, all rights reserved.
309  
310 This program is free software; you can redistribute it and/or modify it
311 under the same terms as Perl itself.
312  
313  
314 =cut
315  
316 2 1; # End of Script::isAperlScript
↑ Summary