blib/lib/File/Find/Rex.pm

filter: Statementstmt 74.0% 77 / 104 Branchbran 33.8% 21 / 62 Conditioncond 41.6% 10 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 90.0% 18 / 20 Podpod 100% 8 / 8 totaltotal 57.9% 135 / 233 CC 40 SCAR 34.2
CC
40
Cov
57%
_callback
80.7
is_recursive
12.0
query
8.2
CRAP 30.6
↑ Summary
1 # File: File/Find/Rex.pm
2 # Description:
3 # Revisions: 2018.01.21 - Roland Ayala - Created
4 #
5 # License: Artistic License 2.0
6 #
7 package File::Find::Rex;
8  
9 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 our $VERSION = '1.01';
13  
14 # import modules
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use File::Spec 'canonpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use if $^O eq 'MSWin32', 'Win32::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub new {
23 1 ▶ my ( $class, $options, $callback ) = @_;
Subroutine new: called CC=5 SCAR=19.7
Pod documented
24 1 ▶ if ( defined $options && ref $options ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options and ref $options ne "HASH") 0 1 1
Condition33%1/3
defined $options and ref $options ne "HASH"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $options and ref $options ne "HASH"
defined $options ref $options ne "HASH"
Truth table33%1/3
A and B
A= defined $optionsB= ref $options ne "HASH"
ABresult
0X0
100
111
25 0 croak 'options expects hash reference';
26 }
27 1 ▶ if ( defined $callback && ref $callback ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback and ref $callback ne "CODE") 0 1 1
Condition33%1/3
defined $callback and ref $callback ne "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $callback and ref $callback ne "CODE"
defined $callback ref $callback ne "CODE"
Truth table33%1/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
ABresult
0X0
100
111
28 0 croak 'callback expects code reference';
29 }
30  
31 1 my $self = {
32 _options => $options,
33 _callback => $callback
34 };
35 1 bless $self, $class;
36 1 return $self;
37 }
38  
39 sub set_option {
40 0 ▶ my ( $self, $key, $value ) = @_;
Subroutine set_option: not called CC=2 SCAR=17.9
Pod documented
41 0 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
42 0 $self->{_options}->{$key} = $value;
43 }
44 0 return;
45 }
46  
47 sub is_ignore_dirs {
48 1 ▶ my $self = shift;
Subroutine is_ignore_dirs: called CC=3 SCAR=12.0
Pod documented
49 return
50 defined $self->{_options}->{ignore_dirs}
51 1 ▶ ? $self->{_options}->{ignore_dirs} > 0
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'_options'}{'ignore_dirs'} ? : 1 0 1
$self->{'_options'}{'ignore_dirs'} > 0 ? : 1 0 1
52 ? 1
53 : 0
54 : 0;
55 }
56  
57 sub is_ignore_hidden {
58 5 ▶ my $self = shift;
Subroutine is_ignore_hidden: called CC=3 SCAR=14.2
Pod documented
59 return
60 defined $self->{_options}->{ignore_hidden}
61 5 ▶ ? $self->{_options}->{ignore_hidden} > 0
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'_options'}{'ignore_hidden'} ? : 0 5 5
$self->{'_options'}{'ignore_hidden'} > 0 ? : 0 0 0
62 ? 1
63 : 0
64 : 0;
65 }
66  
67 sub is_recursive {
68 0 ▶ my $self = shift;
Subroutine is_recursive: not called CC=3 SCAR=24.8
Pod documented
69 return
70 defined $self->{_options}->{recursive}
71 0 ▶ ? $self->{_options}->{recursive} > 0
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'_options'}{'recursive'} ? : 0 0 0
$self->{'_options'}{'recursive'} > 0 ? : 0 0 0
72 ? 1
73 : 0
74 : 0;
75 }
76  
77 sub get_last_modified_earliest {
78 2 ▶ my $self = shift;
Subroutine get_last_modified_earliest: called CC=3 SCAR=13.3
Pod documented
79 2 my $val = $self->{_options}->{last_modified_earliest};
80 2 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 2 2
81  
82 # ensure that value set is an integer value, because caller should
83 # be setting option using epoch timevalue
84 0 ▶ $val =~ m/^[\d]*$/gxs or $val = undef;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^[\d]*$/gsx or $val = undef 0 0 0
85 }
86 2 return $val;
87 }
88  
89 sub get_last_modified_latest {
90 2 ▶ my $self = shift;
Subroutine get_last_modified_latest: called CC=3 SCAR=13.3
Pod documented
91 2 my $val = $self->{_options}->{last_modified_latest};
92 2 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 2 2
93  
94 # ensure that value set is an integer value, because caller should
95 # be setting option using epoch timevalue
96 0 ▶ $val =~ m/^[\d]*$/gxs or $val = undef;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^[\d]*$/gsx or $val = undef 0 0 0
97 }
98 2 return $val;
99 }
100  
101 sub query {
102 1 ▶ my ( $self, $source, $regexp, $context ) = @_;
Subroutine query: called CC=6 SCAR=21.0
Pod documented
103 1 ▶ defined $source or croak 'source path expected';
Branch: 1 branch
BranchTrueFalseTotal
defined $source or croak("source path expected") 0 1 1
104 1 ▶ if ( defined $regexp && ref $regexp ne 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp and ref $regexp ne "Regexp") 0 1 1
Condition33%1/3
defined $regexp and ref $regexp ne "Regexp"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $regexp and ref $regexp ne "Regexp"
defined $regexp ref $regexp ne "Regexp"
Truth table33%1/3
A and B
A= defined $regexpB= ref $regexp ne "Regexp"
ABresult
0X0
100
111
105 0 croak 'regular expression expected';
106 }
107  
108 # Initialize an empty array. If caller sets a callback then this empty
109 # array is returned, else wanted callback will push any files found onto
110 # the array.
111 1 my @files = ();
112  
113 1 ▶ if ( -e $source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $source) { } 1 0 1
114  
115 # Get the absolute path in case caller specifies relative path to source
116 # directory so recursive behavior in find_files_wanted works correctly,
117 # and for logging purposes.
118 1 $source = abs_path($source);
119  
120 1 ▶ if ( -d $source ) # source is a directory
Branch: 1 branch
BranchTrueFalseTotal
if (-d $source) { } 1 0 1
121 {
122 1 File::Find::find(
123 _make_wanted(
124 \&_callback, $self, \@files, $source, $regexp, $context
125 ),
126 $source
127 );
128 }
129 else # source is a file
130 {
131 0 _callback( $self, \@files, $source, $regexp, $context, 1 );
132 }
133 }
134 else {
135 0 warn 'No such file or directory' . "\n";
136 }
137  
138 1 return @files;
139 }
140  
141 sub _make_wanted {
142 1 ▶ my @args = @_; # freeze the args
Subroutine _make_wanted: called CC=1 SCAR=0.0
143 1 my $wanted = shift @args;
144 1 ▶ return sub { $wanted->(@args); };
Subroutine __ANON__: called CC=1 SCAR=0.0
145 }
146  
147 sub _callback {
148 6 ▶ my ( $self, $files, $source, $regexp, $context, $dummy ) = @_;
Subroutine _callback: called CC=20 SCAR=43.9
149 6 ▶ my $file = defined $dummy ? $source : $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
defined $dummy ? : 0 6 6
150  
151 # if the file is a directory and caller has specified to ignore dirs in
152 # results set then jump to end.
153 6 ▶ unless ( -d $file && $self->is_ignore_dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $file and $self->is_ignore_dirs) 5 1 6
Condition66%2/3
-d $file and $self->is_ignore_dirs
!ll&&!rl&&r
501
MC/DC50%1/2
-d $file and $self->is_ignore_dirs
-d $file $self->is_ignore_dirs
Truth table66%2/3
A and B
A= -d $fileB= $self->is_ignore_dirs
ABresult
0X0
100
111
154 NEXT: {
155 5 my ( $fbase, $fdir, $ftype ) = fileparse( $file, '\.[^\.]*' );
156 5 my $filename = $fbase . $ftype;
157  
158 # handle ignore_hidden option
159 5 ▶ if ( $self->is_ignore_hidden ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ignore_hidden) 0 5 5
160  
161 # method for determining if file is hidden depends on if windows
162 # or not.
163 0 my $is_visible;
164 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
165 0 my $attr;
166 0 Win32::File::GetAttributes( $file, $attr );
167 0 $is_visible = !( $attr & Win32::File::HIDDEN() );
168 }
169 else {
170 0 $is_visible = ( $filename !~ /^[.]/gxs );
171 }
172 0 ▶ $is_visible or last NEXT;
Branch: 1 branch
BranchTrueFalseTotal
$is_visible or last NEXT 0 0 0
173 }
174  
175 # handle regex pattern rule if set
176 5 ▶ if ( defined $regexp && !-d $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp and not -d $file) 5 0 5
Condition33%1/3
defined $regexp and not -d $file
!ll&&!rl&&r
005
MC/DC0%0/2
defined $regexp and not -d $file
defined $regexp not -d $file
Truth table33%1/3
A and not(B)
A= defined $regexpB= not -d $file
ABresult
0X0
100
111
177 5 ▶ $filename =~ $regexp or last NEXT;
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ /$regexp/ or last NEXT 3 2 5
178 }
179  
180 # handle last modified window rules if set
181 2 my $oldest = $self->get_last_modified_earliest;
182 2 my $newest = $self->get_last_modified_latest;
183 2 ▶ if ( defined $oldest || defined $newest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldest or defined $newest) 0 2 2
Condition33%1/3
defined $oldest or defined $newest
l!l&&r!l&&!r
002
MC/DC0%0/2
defined $oldest or defined $newest
defined $oldest defined $newest
Truth table33%1/3
A or B
A= defined $oldestB= defined $newest
ABresult
1X1
011
000
184  
185 # capture last modified timestamp from file
186 0 my $timestamp = ( stat $file )[9];
187 0 ▶ if ( defined $oldest ) { $timestamp >= $oldest or last NEXT; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $oldest) 0 0 0
$timestamp >= $oldest or last NEXT 0 0 0
188 0 ▶ if ( defined $newest ) { $timestamp <= $newest or last NEXT; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $newest) 0 0 0
$timestamp <= $newest or last NEXT 0 0 0
189 }
190  
191 2 my $cfile = File::Spec->canonpath($file);
192 2 ▶ if ( defined $self->{_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_callback'}) { } 0 2 2
193 0 $self->{_callback}->( $cfile, $context );
194 }
195 else {
196 2 push @{$files}, $cfile;
197 }
198 }
199 }
200 6 ▶ if ( -d $file && ( uc $file ) ne ( uc $source ) && !$self->is_recursive ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file and uc $file ne uc $source and not $self->is_recursive) 0 6 6
Condition50%3/6
-d $file and uc $file ne uc $source and not $self->is_recursive
!ll&&!rl&&r
600
-d $file and uc $file ne uc $source
!ll&&!rl&&r
510
MC/DC0%0/3
-d $file and uc $file ne uc $source and not $self->is_recursive
-d $file uc $file ne uc $source not $self->is_recursive
Truth table50%2/4
A and B and not(C)
A= -d $fileB= uc $file ne uc $sourceC= not $self->is_recursive
ABCresult
0XX0
10X0
1100
1111
201 0 $File::Find::prune = 1;
202 }
203  
204 6 return;
205 }
206  
207 2 1; # End of File::Find::Rex
208  
209 __END__
↑ Summary