blib/lib/File/Next.pm

filter: Statementstmt 95.5% 149 / 156 Branchbran 89.7% 70 / 78 Conditioncond 61.5% 24 / 39 MC/DCmcdc 40.9% 9 / 22 Subroutinesub 100% 20 / 20 Podpod 100% 7 / 7 totaltotal 86.6% 279 / 322 SCAR 41.2
CC
57
Cov
89%
__ANON__
10.6
_candidate_files
10.1
_setup
10.0
CRAP 61.3
↑ Summary
1 package File::Next;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 File::Next - File-finding iterator
9  
10 =head1 VERSION
11  
12 Version 1.17_01
13  
14 =cut
15  
16 17 our $VERSION = '1.17_01';
17  
18 =head1 SYNOPSIS
19  
20 File::Next is a lightweight, taint-safe file-finding module.
21 It has no non-core prerequisites.
22  
23 use File::Next;
24  
25 my $files = File::Next::files( '/tmp' );
26  
27 while ( defined ( my $file = $files->() ) ) {
28 # do something...
29 }
30  
31 =head1 OPERATIONAL THEORY
32  
33 The two major functions, I<files()> and I<dirs()>, return an iterator
34 that will walk through a directory tree. The simplest use case is:
35  
36 use File::Next;
37  
38 my $iter = File::Next::files( '/tmp' );
39  
40 while ( defined ( my $file = $iter->() ) ) {
41 print $file, "\n";
42 }
43  
44 # Prints...
45 /tmp/foo.txt
46 /tmp/bar.pl
47 /tmp/baz/1
48 /tmp/baz/2.txt
49 /tmp/baz/wango/tango/purple.txt
50  
51 Note that only files are returned by C<files()>'s iterator.
52 Directories are ignored.
53  
54 In list context, the iterator returns a list containing I<$dir>,
55 I<$file> and I<$fullpath>, where I<$fullpath> is what would get
56 returned in scalar context.
57  
58 The first parameter to any of the iterator factory functions may
59 be a hashref of options.
60  
61 =head1 ITERATORS
62  
63 For the three iterators, the \%options are optional.
64  
65 =head2 files( [ \%options, ] @starting_points )
66  
67 Returns an iterator that walks directories starting with the items
68 in I<@starting_points>. Each call to the iterator returns another
69 regular file.
70  
71 =head2 dirs( [ \%options, ] @starting_points )
72  
73 Returns an iterator that walks directories starting with the items
74 in I<@starting_points>. Each call to the iterator returns another
75 directory.
76  
77 =head2 everything( [ \%options, ] @starting_points )
78  
79 Returns an iterator that walks directories starting with the items
80 in I<@starting_points>. Each call to the iterator returns another
81 file, whether it's a regular file, directory, symlink, socket, or
82 whatever.
83  
84 =head2 from_file( [ \%options, ] $filename )
85  
86 Returns an iterator that iterates over each of the files specified
87 in I<$filename>. If I<$filename> is C<->, then the files are read
88 from STDIN.
89  
90 The files are assumed to be in the file one filename per line. If
91 I<$nul_separated> is passed, then the files are assumed to be
92 NUL-separated, as by C<find -print0>.
93  
94 If there are blank lines or empty filenames in the input stream,
95 they are ignored.
96  
97 Each filename is checked to see that it is a regular file or a named
98 pipe. If the file does not exists or is a directory, then a warning
99 is thrown to I<warning_handler>, and the file is skipped.
100  
101 The following options have no effect in C<from_files>: I<descend_filter>,
102 I<sort_files>, I<follow_symlinks>.
103  
104 =head1 SUPPORT FUNCTIONS
105  
106 =head2 sort_standard( $a, $b )
107  
108 A sort function for passing as a C<sort_files> option:
109  
110 my $iter = File::Next::files( {
111 sort_files => \&File::Next::sort_standard,
112 }, 't/swamp' );
113  
114 This function is the default, so the code above is identical to:
115  
116 my $iter = File::Next::files( {
117 sort_files => 1,
118 }, 't/swamp' );
119  
120 =head2 sort_reverse( $a, $b )
121  
122 Same as C<sort_standard>, but in reverse.
123  
124 =head2 reslash( $path )
125  
126 Takes a path with all forward slashes and rebuilds it with whatever
127 is appropriate for the platform. For example 'foo/bar/bat' will
128 become 'foo\bar\bat' on Windows.
129  
130 This is really just a convenience function. I'd make it private,
131 but F<ack> wants it, too.
132  
133 =cut
134  
135 =head1 CONSTRUCTOR PARAMETERS
136  
137 =head2 file_filter -> \&file_filter
138  
139 The file_filter lets you check to see if it's really a file you
140 want to get back. If the file_filter returns a true value, the
141 file will be returned; if false, it will be skipped.
142  
143 The file_filter function takes no arguments but rather does its work through
144 a collection of variables.
145  
146 =over 4
147  
148 =item * C<$_> is the current filename within that directory
149  
150 =item * C<$File::Next::dir> is the current directory name
151  
152 =item * C<$File::Next::name> is the complete pathname to the file
153  
154 =back
155  
156 These are analogous to the same variables in L<File::Find>.
157  
158 my $iter = File::Next::files( { file_filter => sub { /\.txt$/ } }, '/tmp' );
159  
160 By default, the I<file_filter> is C<sub {1}>, or "all files".
161  
162 This filter has no effect if your iterator is only returning directories.
163  
164 =head2 descend_filter => \&descend_filter
165  
166 The descend_filter lets you check to see if the iterator should
167 descend into a given directory. Maybe you want to skip F<CVS> and
168 F<.svn> directories.
169  
170 my $descend_filter = sub { $_ ne "CVS" && $_ ne ".svn" }
171  
172 The descend_filter function takes no arguments but rather does its work through
173 a collection of variables.
174  
175 =over 4
176  
177 =item * C<$_> is the current filename of the directory
178  
179 =item * C<$File::Next::dir> is the complete directory name
180  
181 =back
182  
183 The descend filter is NOT applied to any directory names specified
184 as I<@starting_points> in the constructor. For example,
185  
186 my $iter = File::Next::files( { descend_filter => sub{0} }, '/tmp' );
187  
188 always descends into I</tmp>, as you would expect.
189  
190 By default, the I<descend_filter> is C<sub {1}>, or "always descend".
191  
192 =head2 error_handler => \&error_handler
193  
194 If I<error_handler> is set, then any errors will be sent through
195 it. If the error is OS-related (ex. file not found, not permissions), the
196 native error code is passed as a second argument. By default, this value is
197 C<CORE::die>. This function must NOT return.
198  
199 =head2 warning_handler => \&warning_handler
200  
201 If I<warning_handler> is set, then any errors will be sent through
202 it. By default, this value is C<CORE::warn>. Unlike the
203 I<error_handler>, this function must return.
204  
205 =head2 sort_files => [ 0 | 1 | \&sort_sub]
206  
207 If you want files sorted, pass in some true value, as in
208 C<< sort_files => 1 >>.
209  
210 If you want a special sort order, pass in a sort function like
211 C<< sort_files => sub { $a->[1] cmp $b->[1] } >>.
212 Note that the parms passed in to the sub are arrayrefs, where $a->[0]
213 is the directory name, $a->[1] is the file name and $a->[2] is the
214 full path. Typically you're going to be sorting on $a->[2].
215  
216 =head2 follow_symlinks => [ 0 | 1 ]
217  
218 If set to false, the iterator will ignore any files and directories
219 that are actually symlinks. This has no effect on non-Unixy systems
220 such as Windows. By default, this is true.
221  
222 Note that this filter does not apply to any of the I<@starting_points>
223 passed in to the constructor.
224  
225 You should not set C<< follow_symlinks => 0 >> unless you specifically
226 need that behavior. Setting C<< follow_symlinks => 0 >> can be a
227 speed hit, because File::Next must check to see if the file or
228 directory you're about to follow is actually a symlink.
229  
230 =head2 nul_separated => [ 0 | 1 ]
231  
232 Used by the C<from_file> iterator. Specifies that the files
233 listed in the input file are separated by NUL characters, as from
234 the C<find> command with the C<-print0> argument.
235  
236 =cut
237  
238 17 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 17 our $name; # name of the current file
241 17 our $dir; # dir of the current file
242  
243 17 our %files_defaults;
244 17 our %skip_dirs;
245  
246 BEGIN {
247 %files_defaults = (
248 file_filter => undef,
249 descend_filter => undef,
250 3 ▶ error_handler => sub { CORE::die $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
251 1 ▶ warning_handler => sub { CORE::warn @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
252 17 ▶ sort_files => undef,
Subroutine BEGIN: called CC=1 SCAR=0.0
253 follow_symlinks => 1,
254 nul_separated => 0,
255 );
256 17 %skip_dirs = map {($_,1)} (File::Spec->curdir, File::Spec->updir);
257 }
258  
259  
260 sub files {
261 20 ▶ die _bad_invocation() if @_ && defined($_[0]) && ($_[0] eq __PACKAGE__);
Subroutine files: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 2 18 20
Condition50%3/6
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
0182
@_ and defined $_[0]
!ll&&!rl&&r
0020
MC/DC33%1/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
Truth table50%2/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
262  
263 18 my ($parms,@queue) = _setup( \%files_defaults, @_ );
264  
265 18 my $filter = $parms->{file_filter};
266 return sub {
267 211 ▶ while ( my $entry = shift @queue ) {
Subroutine __ANON__: called CC=8 SCAR=20.8
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @queue) 250 14 264
268 250 my ( $dirname, $file, $fullpath, $is_dir, $is_file, $is_fifo ) = @{$entry};
269 250 ▶ if ( $is_file || $is_fifo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_file or $is_fifo) 206 44 250
Condition100%3/3
$is_file or $is_fifo
l!l&&r!l&&!r
205144
MC/DC100%2/2
$is_file or $is_fifo
$is_file $is_fifo
Truth table100%3/3
A or B
A= $is_fileB= $is_fifo
ABresult
1X1
011
000
270 206 ▶ if ( $filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 20 186 206
271 20 local $_ = $file;
272 20 local $File::Next::dir = $dirname;
273 20 local $File::Next::name = $fullpath;
274 20 ▶ next if not $filter->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter() 9 11 20
275 }
276 197 ▶ return wantarray ? ($dirname,$file,$fullpath) : $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 196 197
277 }
278 44 ▶ if ( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) 44 0 44
279 44 unshift( @queue, _candidate_files( $parms, $fullpath ) );
280 }
281 } # while
282  
283 14 return;
284 18 }; # iterator
285 }
286  
287  
288 sub dirs {
289 5 ▶ die _bad_invocation() if @_ && defined($_[0]) && ($_[0] eq __PACKAGE__);
Subroutine dirs: called CC=4 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 2 3 5
Condition50%3/6
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
032
@_ and defined $_[0]
!ll&&!rl&&r
005
MC/DC33%1/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
Truth table50%2/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
290  
291 3 my ($parms,@queue) = _setup( \%files_defaults, @_ );
292  
293 return sub {
294 6 ▶ while ( my $entry = shift @queue ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @queue) 44 1 45
295 44 my ( undef, undef, $fullpath, $is_dir, undef, undef ) = @{$entry};
296 44 ▶ if ( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) 5 39 44
297 5 unshift( @queue, _candidate_files( $parms, $fullpath ) );
298 5 return $fullpath;
299 }
300 } # while
301  
302 1 return;
303 2 }; # iterator
304 }
305  
306  
307 sub everything {
308 5 ▶ die _bad_invocation() if @_ && defined($_[0]) && ($_[0] eq __PACKAGE__);
Subroutine everything: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 2 3 5
Condition50%3/6
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
032
@_ and defined $_[0]
!ll&&!rl&&r
005
MC/DC33%1/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
Truth table50%2/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
309  
310 3 my ($parms,@queue) = _setup( \%files_defaults, @_ );
311  
312 3 my $filter = $parms->{file_filter};
313 return sub {
314 55 ▶ while ( my $entry = shift @queue ) {
Subroutine __ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @queue) 88 2 90
315 88 my ( $dirname, $file, $fullpath, $is_dir, $is_file, $is_fifo ) = @{$entry};
316 88 ▶ if ( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) 10 78 88
317 10 unshift( @queue, _candidate_files( $parms, $fullpath ) );
318 }
319 88 ▶ if ( $filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 44 44 88
320 44 local $_ = $file;
321 44 local $File::Next::dir = $dirname;
322 44 local $File::Next::name = $fullpath;
323 44 ▶ next if not $filter->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter() 35 9 44
324 }
325 53 ▶ return wantarray ? ($dirname,$file,$fullpath) : $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 53 53
326 } # while
327  
328 2 return;
329 3 }; # iterator
330 }
331  
332 sub from_file {
333 10 ▶ die _bad_invocation() if @_ && defined($_[0]) && ($_[0] eq __PACKAGE__);
Subroutine from_file: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 0 10 10
Condition66%4/6
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
190
@_ and defined $_[0]
!ll&&!rl&&r
109
MC/DC0%0/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
Truth table50%2/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
334  
335 10 my ($parms,@queue) = _setup( \%files_defaults, @_ );
336 10 my $err = $parms->{error_handler};
337 10 my $warn = $parms->{warning_handler};
338  
339 10 my $filename = $queue[0]->[1];
340  
341 10 ▶ if ( !defined($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 1 9 10
342 1 $err->( 'Must pass a filename to from_file()' );
343 0 return undef;
344 }
345  
346 9 my $fh;
347 9 ▶ if ( $filename eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 2 7 9
348 2 $fh = \*STDIN;
349 }
350 else {
351 7 ▶ if ( !open( $fh, '<', $filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $filename) 2 5 7
352 2 $err->( "Unable to open $filename: $!", $! + 0 );
353 1 return undef;
354 }
355 }
356  
357 7 my $filter = $parms->{file_filter};
358 return sub {
359 119 ▶ local $/ = $parms->{nul_separated} ? "\x00" : $/;
Subroutine __ANON__: called CC=9 SCAR=23.6
Branch: 1 branch
BranchTrueFalseTotal
$parms->{'nul_separated'} ? : 34 85 119
360 119 ▶ while ( my $fullpath = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fullpath = readline $fh)) 117 7 124
361 117 chomp $fullpath;
362 117 ▶ next unless $fullpath =~ /./;
Branch: 1 branch
BranchTrueFalseTotal
unless $fullpath =~ /./ 2 115 117
363 115 ▶ if ( not ( -f $fullpath || -p _ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $fullpath or -p _) 3 112 115
Condition66%2/3
-f $fullpath or -p _
l!l&&r!l&&!r
11203
MC/DC50%1/2
-f $fullpath or -p _
-f $fullpath -p _
Truth table66%2/3
A or B
A= -f $fullpathB= -p _
ABresult
1X1
011
000
364 3 $warn->( "$fullpath: No such file" );
365 3 next;
366 }
367  
368 112 my ($volume,$dirname,$file) = File::Spec->splitpath( $fullpath );
369 112 ▶ if ( $filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 112 112
370 0 local $_ = $file;
371 0 local $File::Next::dir = $dirname;
372 0 local $File::Next::name = $fullpath;
373 0 ▶ next if not $filter->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter() 0 0 0
374 }
375 112 ▶ return wantarray ? ($dirname,$file,$fullpath) : $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 112 112
376 } # while
377 7 close $fh;
378  
379 7 return;
380 7 }; # iterator
381 }
382  
383 sub _bad_invocation {
384 6 ▶ my $good = (caller(1))[3];
Subroutine _bad_invocation: called CC=1 SCAR=0.0
385 6 my $bad = $good;
386 6 $bad =~ s/(.+)::/$1->/;
387 6 return "$good must not be invoked as $bad";
388 }
389  
390 84 ▶sub sort_standard($$) { return $_[0]->[1] cmp $_[1]->[1] } ## no critic (ProhibitSubroutinePrototypes)
Subroutine sort_standard: called CC=1 SCAR=0.0
Pod documented
391 83 ▶sub sort_reverse($$) { return $_[1]->[1] cmp $_[0]->[1] } ## no critic (ProhibitSubroutinePrototypes)
Subroutine sort_reverse: called CC=1 SCAR=0.0
Pod documented
392  
393 sub reslash {
394 732 ▶ my $path = shift;
Subroutine reslash: called CC=2 SCAR=6.9
Pod documented
395  
396 732 my @parts = split( /\//, $path );
397  
398 732 ▶ return $path if @parts < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 2 85 647 732
399  
400 647 return File::Spec->catfile( @parts );
401 }
402  
403  
404 =head1 PRIVATE FUNCTIONS
405  
406 =head2 _setup( $default_parms, @whatever_was_passed_to_files() )
407  
408 Handles all the scut-work for setting up the parms passed in.
409  
410 Returns a hashref of operational options, combined between
411 I<$passed_parms> and I<$defaults>, plus the queue.
412  
413 The queue prep stuff takes the strings in I<@starting_points> and
414 puts them in the format that queue needs.
415  
416 The C<@queue> that gets passed around is an array, with each entry an
417 arrayref of $dir, $file and $fullpath.
418  
419 =cut
420  
421 sub _setup {
422 34 ▶ my $defaults = shift;
Subroutine _setup: called CC=10 SCAR=23.0
423 34 ▶ my $passed_parms = ref $_[0] eq 'HASH' ? {%{+shift}} : {}; # copy parm hash
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 18 16 34
424  
425 34 my %passed_parms = %{$passed_parms};
426  
427 34 my $parms = {};
428 34 for my $key ( keys %{$defaults} ) {
429 $parms->{$key} =
430 exists $passed_parms{$key}
431 ? delete $passed_parms{$key}
432 238 ▶ : $defaults->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $passed_parms{$key} ? : 18 220 238
433 }
434  
435 # Any leftover keys are bogus
436 34 for my $badkey ( sort keys %passed_parms ) {
437 2 my $sub = (caller(1))[3];
438 2 $parms->{error_handler}->( "Invalid option passed to $sub(): $badkey" );
439 }
440  
441 # If it's not a code ref, assume standard sort
442 33 ▶ if ( $parms->{sort_files} && ( ref($parms->{sort_files}) ne 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parms->{"sort_files"} and ref $parms->{"sort_files"} ne "CODE") 1 32 33
Condition100%3/3
$parms->{"sort_files"} and ref $parms->{"sort_files"} ne "CODE"
!ll&&!rl&&r
2931
MC/DC100%2/2
$parms->{"sort_files"} and ref $parms->{"sort_files"} ne "CODE"
$parms->{"sort_files"} ref $parms->{"sort_files"} ne "CODE"
Truth table100%3/3
A and B
A= $parms->{"sort_files"}B= ref $parms->{"sort_files"} ne "CODE"
ABresult
0X0
100
111
443 1 $parms->{sort_files} = \&sort_standard;
444 }
445 33 my @queue;
446  
447 33 for ( @_ ) {
448 34 my $start = reslash( $_ );
449 34 my $is_dir = -d $start;
450 34 my $is_file = -f _;
451 34 ▶ my $is_fifo = (-p _) || ($start =~ m{^/dev/fd});
Condition66%2/3
-p _ || $start =~ m[^/dev/fd]
l!l&&r!l&&!r
1033
MC/DC50%1/2
-p _ || $start =~ m[^/dev/fd]
-p _ $start =~ m[^/dev/fd]
Truth table66%2/3
A || B
A= -p _B= $start =~ m[^/dev/fd]
ABresult
1X1
011
000
452 34 ▶ push @queue,
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 22 12 34
453 $is_dir
454 ? [ $start, undef, $start, $is_dir, $is_file, $is_fifo ]
455 : [ undef, $start, $start, $is_dir, $is_file, $is_fifo ];
456 }
457  
458 33 return ($parms,@queue);
459 }
460  
461 =head2 _candidate_files( $parms, $dir )
462  
463 Pulls out the files/dirs that might be worth looking into in I<$dir>.
464 If I<$dir> is the empty string, then search the current directory.
465  
466 I<$parms> is the hashref of parms passed into File::Next constructor.
467  
468 =cut
469  
470 sub _candidate_files {
471 59 ▶ my $parms = shift;
Subroutine _candidate_files: called CC=10 SCAR=23.2
472 59 my $dirname = shift;
473  
474 59 my $dh;
475 59 ▶ if ( !opendir $dh, $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir $dh, $dirname) 0 59 59
476 0 $parms->{error_handler}->( "$dirname: $!", $! + 0 );
477 0 return;
478 }
479  
480 59 my @newfiles;
481 59 my $descend_filter = $parms->{descend_filter};
482 59 my $follow_symlinks = $parms->{follow_symlinks};
483  
484 59 for my $file ( grep { !exists $skip_dirs{$_} } readdir $dh ) {
485 379 my $fullpath = File::Spec->catdir( $dirname, $file );
486 379 ▶ if ( !$follow_symlinks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $follow_symlinks) { } 12 367 379
487 12 ▶ next if -l $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
if -l $fullpath 2 10 12
488 }
489 else {
490 367 stat($fullpath);
491 }
492 377 my $is_dir = -d _;
493 377 my $is_file = -f _;
494 377 ▶ my $is_fifo = (-p _) || ($fullpath =~ m{^/dev/fd});
Condition33%1/3
-p _ || $fullpath =~ m[^/dev/fd]
l!l&&r!l&&!r
00377
MC/DC0%0/2
-p _ || $fullpath =~ m[^/dev/fd]
-p _ $fullpath =~ m[^/dev/fd]
Truth table33%1/3
A || B
A= -p _B= $fullpath =~ m[^/dev/fd]
ABresult
1X1
011
000
495  
496 # Only do directory checking if we have a descend_filter
497 377 ▶ if ( $descend_filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descend_filter) 36 341 377
498 36 ▶ if ( $is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) 6 30 36
499 6 local $File::Next::dir = $fullpath;
500 6 local $_ = $file;
501 6 ▶ next if not $descend_filter->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$descend_filter() 3 3 6
502 }
503 }
504 374 push @newfiles, [ $dirname, $file, $fullpath, $is_dir, $is_file, $is_fifo ];
505 }
506 59 closedir $dh;
507  
508 59 my $sort_sub = $parms->{sort_files};
509 59 ▶ if ( $sort_sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_sub) 13 46 59
510 13 @newfiles = sort $sort_sub @newfiles;
511 }
512  
513 59 return @newfiles;
514 }
515  
516  
517 =head1 DIAGNOSTICS
518  
519 =over
520  
521 =item C<< File::Next::files must not be invoked as File::Next->files >>
522  
523 =item C<< File::Next::dirs must not be invoked as File::Next->dirs >>
524  
525 =item C<< File::Next::everything must not be invoked as File::Next->everything >>
526  
527 =back
528  
529 The interface functions do not allow for the method invocation syntax and
530 throw errors with the messages above. You can work around this limitation
531 with L<UNIVERSAL/can>.
532  
533 for my $file_system_feature (qw(dirs files)) {
534 my $iterator = File::Next->can($file_system_feature)->($options, $target_directory);
535 while (defined(my $name = $iterator->())) {
536 # ...
537 }
538 }
539  
540 =head1 SPEED TWEAKS
541  
542 =over 4
543  
544 =item * Don't set C<< follow_symlinks => 0 >> unless you need it.
545  
546 =back
547  
548 =head1 AUTHOR
549  
550 Andy Lester, C<< <andy at petdance.com> >>
551  
552 =head1 BUGS
553  
554 Please report any bugs or feature requests to
555 L<http://github.com/petdance/file-next/issues>.
556  
557 Note that File::Next does NOT use L<http://rt.cpan.org> for bug tracking.
558  
559 =head1 SUPPORT
560  
561 You can find documentation for this module with the perldoc command.
562  
563 perldoc File::Next
564  
565 You can also look for information at:
566  
567 =over 4
568  
569 =item * File::Next's bug queue
570  
571 L<http://github.com/petdance/file-next/issues>
572  
573 =item * CPAN Ratings
574  
575 L<http://cpanratings.perl.org/d/File-Next>
576  
577 =item * Search CPAN
578  
579 L<http://search.cpan.org/dist/File-Next>
580  
581 =item * Source code repository
582  
583 L<http://github.com/petdance/file-next/tree/master>
584  
585 =back
586  
587 =head1 ACKNOWLEDGEMENTS
588  
589 All file-finding in this module is adapted from Mark Jason Dominus'
590 marvelous I<Higher Order Perl>, page 126.
591  
592 Thanks to these fine contributors:
593 Varadinsky,
594 Paulo Custodio,
595 Gerhard Poul,
596 Brian Fraser,
597 Todd Rinaldo,
598 Bruce Woodward,
599 Christopher J. Madsen,
600 Bernhard Fisseni
601 and Rob Hoelz.
602  
603 =head1 COPYRIGHT & LICENSE
604  
605 Copyright 2005-2017 Andy Lester.
606  
607 This program is free software; you can redistribute it and/or modify
608 it under the terms of the Artistic License version 2.0.
609  
610 =cut
611  
612 17 1; # End of File::Next
↑ Summary