File Coverage

blib/lib/File/Find/Mason.pm
Criterion Covered Total %
statement 50 62 80.6
branch 24 36 66.6
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 83 107 77.5


line stmt bran cond sub pod time code
1             package File::Find::Mason;
2              
3 1     1   7718 use strict;
  1         2  
  1         35  
4 1     1   5 use warnings;
  1         7  
  1         71  
5 1     1   6 use File::Find qw//;
  1         2  
  1         1077  
6              
7             our $VERSION='0.0.7';
8              
9             my %default=(
10             verbose =>0,
11             shebang =>1,
12             args =>1,
13             once =>1,
14             perl =>1,
15             call =>1,
16             modeline =>1,
17             );
18              
19             my %pattern=(
20             shebang =>qr/^#!.*\w/,
21             args =>qr/^<%args>/,
22             once =>qr/^<%once>/,
23             perl =>qr/^<%perl>/,
24             call =>qr/^<&.*&>/,
25             modeline =>qr/^% *#.*mason/,
26             );
27              
28             my $optpattern=join('|',keys %default);
29              
30             sub find {
31 20     20 1 372313 my ($options,@dirs)=@_;
32 20         46 my %findopt;
33 20         85 foreach my $k (grep {!/^(?:$optpattern)$/} keys(%$options)) { $findopt{$k}=$$options{$k} }
  130         935  
  10         37  
34 20 100       85 if($$options{wanted}) {
35 10         25 my $cb=$$options{wanted};
36             File::Find::find({%findopt, no_chdir=>1, wanted=>sub {
37 80 100   80   1156 if(-d $File::Find::name) { return }
  10         1452  
38 70 100       183 if(wanted($options,$File::Find::name)) { &$cb() }
  10         554  
39 10         1299 } },@dirs);
40             }
41             else {
42 10         19 my @res;
43             File::Find::find({%findopt, no_chdir=>1, wanted=>sub {
44 80 100   80   848 if(-d $File::Find::name) { return }
  10         1522  
45 70 100       191 if(wanted($options,$File::Find::name)) { push @res,$File::Find::name }
  10         373  
46 10         1191 } },@dirs);
47 10         109 return @res;
48             }
49             }
50              
51             sub wanted {
52 168     168 1 27355 my ($options,$fn)=@_;
53 168         1465 my %opt=(%default,%$options);
54 168 0       2889 if(!-e $fn) { if($opt{verbose}) { print STDERR "File not found: $fn\n" }; return }
  0 50       0  
  0         0  
  0         0  
55 168 0       1810 if(!-r $fn) { if($opt{verbose}) { print STDERR "Unable to read: $fn\n" }; return }
  0 50       0  
  0         0  
  0         0  
56 168         286 my ($fh,$txt);
57 168 0       7123 if(!open($fh,'<',$fn)) { if($opt{verbose}) { print STDERR "Unable to read: $fn\n" }; return }
  0 50       0  
  0         0  
  0         0  
58 168         370 {local($/); $txt=<$fh>}
  168         818  
  168         3474  
59 168         1782 close($fh);
60 168 50       428 if(!$txt) {
61 0 0       0 if($opt{verbose}) { print STDERR "No content for $fn\n" }
  0         0  
62 0         0 return 0;
63             }
64 168 100       717 foreach my $k (grep {$opt{$_}&&$pattern{$_}} keys(%default)) {
  1176         3468  
65 306 100       967 if($k eq 'shebang') { if($txt=~$pattern{$k}) { return 0 } }
  90 100       622  
  24         759  
66 282 100       1464 if($txt=~$pattern{$k}) {
67 30 100       91 if($opt{wanted}) { return &{$opt{wanted}}($fn) }
  15         29  
  15         83  
68 15         153 else { return 1 }
69             }
70             }
71 114         3079 return 0;
72             }
73              
74             1;
75              
76             __END__