Wildcard.pm

filter: Statementstmt 99.5% 229 / 230 Branchbran 92.7% 89 / 96 Conditioncond 87.0% 47 / 54 MC/DCmcdc 79.3% 23 / 29 Subroutinesub 100% 29 / 29 Podpod 100% 8 / 8 totaltotal 95.2% 425 / 446 CC 75 SCAR 23.9
CC
75
Cov
96%
_state_nextdir
27.0
new
13.4
_state_wildcard
8.2
CRAP 10.9
↑ Summary Wildcard/Find.pm »
1  
2 package File::Wildcard;
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '0.11';
6  
7 =head1 NAME
8  
9 File::Wildcard - Enhanced glob processing
10  
11 =head1 SYNOPSIS
12  
13 use File::Wildcard;
14 my $foo = File::Wildcard->new(path => "/home/me///core");
15 while (my $file = $foo->next) {
16 unlink $file;
17 }
18
19 =head1 DESCRIPTION
20  
21 When looking at how various operating systems do filename wildcard expansion
22 (globbing), VMS has a nice syntax which allows expansion and searching of
23 whole directory trees. It would be nice if other operating systems had
24 something like this built in. The best Unix can manage is through the
25 utility program C<find>.
26  
27 This module provides this facility to Perl. Whereas native VMS syntax uses
28 the ellipsis "...", this will not fit in with POSIX filenames, as ... is a
29 valid (though somewhat strange) filename. Instead, the construct "///" is
30 used as this cannot syntactically be part of a filename, as you do not get
31 three concurrent filename separators with nothing between (three slashes
32 are used to avoid confusion with //node/path/name syntax).
33  
34 You don't have to use this syntax, as you can do the splitting yourself and
35 pass in an arrayref as your path.
36  
37 The module also forms a B<regular expression> for the whole of the wildcard
38 string, and binds a series of back references ($1, $2
39 etc.) which are available to construct new filenames.
40  
41 =head2 new
42  
43 C<File::Wildcard->new( $wildcard, [,option => value,...]);>
44  
45 my $foo = File::Wildcard->new( path => "/home/me///core");
46 my $srcfnd = File::Wildcard->new( path => "src///*.cpp",
47 match => qr(^src/(.*?)\.cpp$),
48 derive => ['src/$1.o','src/$1.hpp']);
49  
50 This is the constructor for File::Wildcard objects. At a simple level,
51 pass a single wildcard string as a path.
52  
53 For more complicated operations, you can supply your own match regexp, or
54 use the derive option to specify regular expression captures to form
55 the basis of other filenames that are constructed for you.
56  
57 The $srcfnd example gives you object files and header files corresponding
58 to C++ source files.
59  
60 Here are the options that are available:
61  
62 =over 4
63  
64 =item C<path>
65  
66 This is the input parameter that specifies the range
67 of files that will be looked at. This is a glob spec which can also contain
68 the ellipsis '///' (it could contain more than one ellipsis, but the benefit
69 of this is questionable, and multiple ellipsi would cause a performance hit).
70  
71 Note that the path can be relative or absolute. B<new> will do the right
72 thing, working out that a path starting with '/' is absolute. In order
73 to recurse from the current directory downwards, specify './//foo'.
74  
75 As an alternative, you can supply an arrayref with the path constituents
76 already split. If you do this, you need to tell B<new> if the path is absolute.
77 Include an empty string for an ellipsis. For example:
78  
79 'foo///bar/*.c' is equivalent to ['foo','','bar','*.c']
80  
81 You can also construct a File::Wildcard without a path. A call to
82 B<next> will return undef, but paths can be added using the append and prepend
83 methods.
84  
85 =item C<absolute>
86  
87 This is ignored unless you are using a pre split path. If you
88 are passing a string as the path, B<new> will work out whether the path is
89 absolute or relative. Pass a true value for absolute paths.
90  
91 If your original filespec started with '/' before you split it, specify
92 absolute => 1. B<absolute> is not required for Windows if the path contains
93 a drive specification, e.g. C:/foo/bar.
94  
95 =item C<case_insensitive>
96  
97 By default, the module will use L<Filesys::Type> to determine whether the
98 file system of your wildcard is defined. This is an optional module (see
99 L<Module::Optional>), and File::Wildcard will guess at case sensitivity
100 based on your operating system. This will not always be correct, as the
101 file system might be VFAT mounted on Linux or ODS-5 on VMS.
102  
103 Specifying the option C<case_insensitive> explicitly forces this behaviour
104 on the wildcard.
105  
106 Note that File::Wildcard will use the file system of the current working
107 directory if the path is not absolute. If the path is absolute, you should
108 specify the case_sensitivity option explicitly.
109  
110 =item C<exclude>
111  
112 You can provide a regexp to apply to any generated paths, which will cause
113 any matching paths not to be processed. If the root of a directory tree
114 matches, no processing is done on the entire tree.
115  
116 This option can be useful for excluding version control repositories, e.g.
117  
118 exclude => qr/.svn/
119  
120 =item C<match>
121  
122 Optional. If you do not specify a regexp, you get all the files
123 that match the glob; in addition, B<new> will set up a regexp for you, to
124 provide a capture for each wildcard used in the path.
125  
126 If you do provide a match parameter, this will be used instead, and will
127 filter the results.
128  
129 =item C<derive>
130  
131 Supply an arrayref with a list of derived filenames, which
132 will be constructed for each matching file. This causes B<next> to return
133 an arrayref instead of a scalar.
134  
135 =item C<follow>
136  
137 If given a true value indicates that symbolic links are to be followed. Otherwise,
138 the symbolic link target itself is presented, but the ellipsis will not traverse
139 the link.
140  
141 This module detects a looping symlink that points to a directory higher up, and
142 will only present the tree once.
143  
144 =item C<ellipsis_order>
145  
146 This can take one of the following values: normal, breadth-first, inside-out.
147 The default option is normal. This controls how File::Wildcard handles
148 the ellipsis. The default is a normal depth first search, presenting the
149 name of each containing directory before the contents.
150  
151 The inside-out order presents the contents of directories first before
152 the directory, which is useful when you want to remove files and directories
153 (all O/S require directories to be empty before rmdir will work). See
154 t/03_absolute.t as this uses inside-out order to tidy up after the test.
155  
156 Breadth-first is rarely needed (but I do have an application for it). Here,
157 the whole directory contents is presented before traversing any subdirectories.
158  
159 Consider the following tree:
160 a/
161 a/bar/
162 a/bar/drink
163 a/foo/
164 a/foo/lish
165  
166 breadth-first will give the following order: qw(a/ a/bar/ a/foo/ a/bar/drink
167 a/foo/lish). normal gives the order in which the files are listed.
168 inside-out gives the following: qw(a/bar/drink a/bar/ a/foo/lish a/foo/ a/).
169  
170 =item C<sort>
171  
172 By default, globbing returns the list of files in the order in which they
173 are returned by the dirhandle (internally). If you specify sort => 1, the
174 files are sorted into ASCII sequence (case insensitively if we are operating
175 that way). If you specify a CODEREF, this will
176 be used as a comparison routine. Note that this takes its operands in @_,
177 not in $a and $b.
178  
179 =item C<debug> and C<debug_output>
180  
181 You can enable a trace of the internal states of File::Wildcard by setting
182 debug to a true value. Set debug_output to an open filehandle to get the
183 trace in a file. If you are submitting bug reports for File::Wildcard, attaching
184 debug trace files would be very useful.
185  
186 debug_output defaults to STDERR.
187  
188 =back
189  
190 =head2 match
191  
192 my $foo_re = $foo->match;
193 $foo->match('bar/core');
194  
195 This is a get and set method that gives access to the match regexp that
196 the File::Wildcard object is using. It is possible to change the regex
197 on the fly in the middle of a search (though I don't know why anyone would
198 want to do this).
199  
200 =head2 append
201  
202 $foo->append(path => '/home/me///*.tmp');
203  
204 appends a path to an object's todo list. This will be globbed
205 after the object has finished processing the existing wildcards.
206  
207 =head2 prepend
208  
209 $srcfnd->prepend(path => $include_file);
210  
211 This is similar to append, but prepends the path to the todo list. In other
212 words, the current wildcard operation is interrupted to serve the new path,
213 then the previous wildcard operation is resumed when this is exhausted.
214  
215 =head2 next
216  
217 while (my $core = $foo->next) {
218 unlink $core;
219 }
220 my ($src,$obj,$hdr) = @{$srcfnd->next};
221  
222 The C<next> method is an iterator, which returns successive files. Returns
223 matching files if there was no derive option passed to new. If there was
224 a derive option, returns an arrayref containing the matching filespec and
225 all derived filespecs. The derived filespecs do not have to exist.
226  
227 Note that C<next> maintains an internal cursor, which retains context and
228 state information. Beware if the contents of directories are changing while
229 you are iterating with next; you may get unpredictable results. If you are
230 intending to change the contents of the directories you are scanning (with unlink
231 or rename), you are better off deferring this operation until you have processed
232 the whole tree. For the pending delete or rename operations, you could always
233 use another File::Wildcard object - see the spike example below:
234  
235 =head2 all
236  
237 my @cores = $foo->all;
238  
239 C<all> returns an array of matching files, in the simple case. Returns an
240 array of arrays if you are constructing new filenames, like the $srcfnd
241 example.
242  
243 Beware of the performance and memory implications of using C<all>. The
244 method will not return until it has read the entire directory tree. Use of
245 the C<all> method is not recommended for traversing large directory trees
246 and whole file systems. Consider coding the traversal using the iterator
247 C<next> instead.
248  
249 =head2 reset
250  
251 C<reset> causes the wildcard context to be set to re-read the first filename
252 again. Note that this will cause directory contents to be re-read.
253  
254 Note also that this will cause the path to revert to the original path
255 specified to B<new>. Any additional paths appended or prepended will be
256 forgotten.
257  
258 =head2 close
259  
260 Release all directory handles associated with the File::Wildcard object.
261 An object that has been closed will be garbage collected once it goes out
262 of scope. Wildcards that have been exhausted are automatically closed,
263 (i.e. C<all> was used, or c<next> returned undef).
264  
265 Subsequent calls to C<next> will return undef. It is possible to call
266 C<reset> after C<close> on the same File::Wildcard object, which will cause
267 it to be reopened.
268  
269 =head1 EXAMPLES
270  
271 =over 4
272  
273 =item *
274 B<The spike>
275  
276 my $todo = File::Wildcard->new;
277  
278 ...
279  
280 $todo->append(path => $file);
281  
282 ...
283  
284 while (my $file = $todo->next) {
285 ...
286 }
287  
288 You can use an empty wildcard to store a list of filenames for later
289 processing. The order in which they will be seen depends on whether append
290 or prepend is used.
291  
292 =item *
293 B<Shell style globbing>
294  
295 my $wc_args = File::Wildcard->new;
296  
297 $wc_args->append(path => $_) for @ARGV;
298  
299 while ($wc_args->next) {
300 ...
301 }
302  
303 On Unix, file wildcards on the command line are globbed by the shell before
304 perl sees them, unless the wildcards are escaped or quoted. This is not true
305 of other operating systems. MS-DOS does no globbing at all for example.
306  
307 File::Wildcard gives you the bonus of elliptic globbing with '///'.
308  
309 =back
310  
311 =head1 CAVEAT
312  
313 This module takes POSIX filenames, which use forward slash '/' as a
314 path separator. All operating systems that run Perl can manage this type
315 of path. The module is not designed to work with B<native> file specs.
316 If you want to write code that is portable, convert native filespecs to
317 the POSIX form. There is of course no difference on Unix platforms.
318
319 =head1 BUGS
320  
321 Please report bugs to http://rt.cpan.org
322  
323 =head1 AUTHOR
324  
325 Ivor Williams
326 ivorw-file-wildcard010 at xemaps.com
327  
328 =head1 COPYRIGHT
329  
330 This program is free software; you can redistribute
331 it and/or modify it under the same terms as Perl itself.
332  
333 The full text of the license can be found in the
334 LICENSE file included with this module.
335  
336  
337 =head1 SEE ALSO
338  
339 glob(3), L<File::Find>, L<File::Find::Rule>.
340  
341 =cut
342  
343 7 ▶use Params::Validate::Dummy qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
344 7 ▶use Module::Optional qw(Params::Validate :all);
Subroutine BEGIN: called CC=1 SCAR=0.0
345  
346 package Filesys::Type::Dummy;
347 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
348  
349 10 ▶sub case {
Subroutine case: called CC=3 SCAR=11.7
350 10 ▶ return 'insensitive' if $^O =~ /win|dos/i;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /win|dos/i 0 10 10
351 10 ▶ return 'lower' if $^O =~ /vms/i;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /vms/i 0 10 10
352 10 return 'sensitive';
353 }
354  
355 package File::Wildcard;
356 7 ▶use Module::Optional qw(Filesys::Type);
Subroutine BEGIN: called CC=1 SCAR=0.0
357 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
358 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
359  
360 28 ▶sub new {
Subroutine new: called CC=13 SCAR=25.9
Pod documented
361 28 my $pkg = shift;
362  
363 28 my %par = validate(
364 @_,
365 { derive => 0,
366 path => { type => SCALAR | ARRAYREF, optional => 1 },
367 follow => 0,
368 absolute => 0,
369 match => { type => SCALARREF, optional => 1 },
370 exclude => { type => SCALARREF, optional => 1 },
371 sort => { type => SCALAR | CODEREF | UNDEF, optional => 1 },
372 ellipsis_order => {
373 type => SCALAR,
374 regex => qr/(normal|breadth-first|inside-out)/,
375 optional => 1,
376 },
377 case_insensitive => { type => SCALAR, optional => 1 },
378 debug => { type => SCALAR, optional => 1 },
379 debug_output => 0,
380 }
381 );
382  
383 28 ▶ $par{ellipsis_order} ||= 'normal';
Condition100%2/2
$par{"ellipsis_order"} ||= "normal"
l!l
226
MC/DC100%1/1
$par{"ellipsis_order"} ||= "normal"
$par{"ellipsis_order"}
Truth table100%2/2
A
A= $par{"ellipsis_order"}
Aresult
11
00
384 28 my $path = $par{path}; # $par{path} is about to be chopped up
385 ( $par{path}, $par{absolute} )
386 28 = $pkg->_split_path( @par{qw/path absolute follow/} );
387 28 ▶ if ( exists( $par{path} ) && !defined $par{case_insensitive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $par{"path"} and not defined $par{"case_insensitive"}) 9 19 28
Condition66%2/3
exists $par{"path"} and not defined $par{"case_insensitive"}
!ll&&!rl&&r
0199
MC/DC50%1/2
exists $par{"path"} and not defined $par{"case_insensitive"}
exists $par{"path"} not defined $par{"case_insensitive"}
Truth table66%2/3
A and not(B)
A= exists $par{"path"}B= not defined $par{"case_insensitive"}
ABresult
0X0
100
111
388 9 ▶ my $fspath = $par{absolute} ? $path : File::Spec->curdir;
Branch: 1 branch
BranchTrueFalseTotal
$par{'absolute'} ? : 0 9 9
389 9 ▶ my $fscase = eval { Filesys::Type::case($fspath) }
Condition33%1/3
eval { do { Filesys::Type::case($fspath) } } || Filesys::Type::Dummy::case()
l!l&&r!l&&!r
900
MC/DC0%0/2
eval { do { Filesys::Type::case($fspath) } } || Filesys::Type::Dummy::case()
eval { do { Filesys::Type::case($fspath) } } Filesys::Type::Dummy::case()
Truth table33%1/3
A || B
A= eval { do { Filesys::Type::case($fspath) } }B= Filesys::Type::Dummy::case()
ABresult
1X1
011
000
390 || Filesys::Type::Dummy::case;
391 9 $par{case_insensitive} = $fscase eq 'sensitive';
392 }
393  
394 28 ▶ $par{debug_output} ||= \*STDERR if $par{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $par{"debug"} 12 16 28
Condition50%1/2
$par{"debug_output"} ||= \*STDERR
l!l
120
MC/DC0%0/1
$par{"debug_output"} ||= \*STDERR
$par{"debug_output"}
Truth table50%1/2
A
A= $par{"debug_output"}
Aresult
11
00
395  
396 28 ▶ unless ( exists $par{match} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $par{"match"}) 28 0 28
397 28 ▶ my $match_re = $par{absolute} ? '^/' : '^';
Branch: 1 branch
BranchTrueFalseTotal
$par{'absolute'} ? : 7 21 28
398 28 for ( @{ $par{path} } ) {
399 76 my $comp = quotemeta $_;
400 76 $comp =~ s!((?:\\\?)+)!'(.{'.(length($1)/2).'})'!eg;
401 76 $comp =~ s!\\\*!([^/]*)!g;
402 76 ▶ $match_re .= ( $comp || '(.*?)' ) . '/';
Condition100%2/2
$comp || '(.*?)'
l!l
6214
MC/DC100%1/1
$comp || '(.*?)'
$comp
Truth table100%2/2
A
A= $comp
Aresult
11
00
403 }
404 28 $match_re =~ s!/$!\$!;
405 28 ▶ $par{match} = $par{case_insensitive} ? qr/$match_re/i : qr/$match_re/;
Branch: 1 branch
BranchTrueFalseTotal
$par{'case_insensitive'} ? : 15 13 28
406 }
407  
408 28 bless \%par, $pkg;
409 }
410  
411 13819 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=6.9
412 13819 my ( $self, $mess ) = @_;
413  
414 13819 ▶ return unless $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} 8273 5546 13819
415 5546 my $dbug = $self->{debug_output};
416  
417 5546 print $dbug $mess;
418 }
419  
420 104 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
421 104 my $self = shift;
422  
423 104 ▶ $self->_set_state( state => 'initial' ) unless exists $self->{state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"state"} 27 77 104
424  
425 104 ▶ while ( !exists $self->{retval} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"retval"}) 5856 104 5960
426 5856 $self->_debug( "In state " . $self->{state} . "\n" );
427 5856 my $method = "_state_" . $self->{state};
428 5856 $self->$method;
429 }
430 104 ▶ $self->_debug( "Returned " . ( $self->{retval} || 'undef' ) . "\n" );
Condition100%2/2
$self->{'retval'} || 'undef'
l!l
7034
MC/DC100%1/1
$self->{'retval'} || 'undef'
$self->{'retval'}
Truth table100%2/2
A
A= $self->{'retval'}
Aresult
11
00
431 104 my $rv = $self->{retval};
432 104 delete $self->{retval};
433  
434 104 $rv;
435 }
436  
437 21 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod documented
438 21 my $self = shift;
439  
440 21 my @out;
441  
442 21 ▶ while ( my $match = $self->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->next) 58 21 79
443 58 push @out, $match;
444 }
445  
446 21 @out;
447  
448 }
449  
450 2 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
451 2 my $self = shift;
452  
453 2 delete $self->{stack};
454 2 delete $self->{dir};
455 2 delete $self->{seen_symlink};
456 2 $self->_set_state( state => 'finished' );
457 }
458  
459 2 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
460 2 my $self = shift;
461  
462 2 $self->close;
463 2 $self->_set_state( state => 'initial' );
464 }
465  
466 70 ▶sub _derived {
Subroutine _derived: called CC=3 SCAR=11.0
467 70 my $self = shift;
468  
469 70 ▶ return $self->{resulting_path} unless exists $self->{derive};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"derive"} 65 5 70
470  
471 5 my @out = ( $self->{resulting_path} );
472 5 my $re = $self->{match};
473 5 $self->{resulting_path} =~ /$re/;
474 5 for ( @{ $self->{derive} } ) {
475 5 push @out, eval(qq("$_"));
476 }
477  
478 5 \@out;
479 }
480  
481 4 ▶sub match {
Subroutine match: called CC=2 SCAR=6.9
Pod documented
482 4 my $self = shift;
483  
484 4 my ($new_re) = validate_pos( @_, { type => SCALARREF, optional => 1 } );
485  
486 4 ▶ $new_re ? ( $self->{match} = $new_re ) : $self->{match};
Branch: 1 branch
BranchTrueFalseTotal
$new_re ? : 2 2 4
487 }
488  
489 5 ▶sub append {
Subroutine append: called CC=4 SCAR=13.9
Pod documented
490 5 my $self = shift;
491  
492 5 my %par = validate(
493 @_,
494 { path => { type => SCALAR | ARRAYREF },
495 follow => 0,
496 absolute => 0,
497 }
498 );
499 5 my %new;
500  
501 @new{qw/ path_remaining absolute follow /}
502 5 = $self->_split_path( @par{qw/ path absolute follow /} );
503 5 $new{state} = 'nextdir';
504 5 ▶ $new{resulting_path} = $new{absolute} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$new{'absolute'} ? : 2 3 5
505  
506 5 unshift @{ $self->{state_stack} }, \%new;
507  
508 5 ▶ $self->_pop_state if !$self->{state} || ( $self->{state} eq 'finished' );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"state"} or $self->{"state"} eq "finished" 4 1 5
Condition100%3/3
not $self->{"state"} or $self->{"state"} eq "finished"
l!l&&r!l&&!r
131
MC/DC100%2/2
not $self->{"state"} or $self->{"state"} eq "finished"
not $self->{"state"} $self->{"state"} eq "finished"
Truth table100%3/3
not(A) or B
A= not $self->{"state"}B= $self->{"state"} eq "finished"
ABresult
1X1
011
000
509 }
510  
511 2 ▶sub prepend {
Subroutine prepend: called CC=2 SCAR=6.9
Pod documented
512 2 my $self = shift;
513  
514 2 my %par = validate(
515 @_,
516 { path => { type => SCALAR | ARRAYREF },
517 follow => 0,
518 absolute => 0,
519 }
520 );
521  
522 2 $self->_push_state;
523  
524 my ( $pr, $abs, $fol )
525 2 = $self->_split_path( @par{qw/ path absolute follow /} );
526 2 $self->{path_remaining} = $pr;
527 2 $self->{absolute} = $abs;
528 2 $self->{follow} = $fol;
529 2 ▶ $self->{resulting_path} = $self->{absolute} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'absolute'} ? : 0 2 2
530 2 $self->_set_state( state => 'nextdir' );
531 }
532  
533 35 ▶sub _split_path {
Subroutine _split_path: called CC=5 SCAR=16.1
534 35 my $self = shift;
535  
536 35 my ( $path, $abs, $follow ) = validate_pos( @_, 0, 0, 0 );
537  
538 35 ▶ return ( $path, $abs, $follow ) if !defined($path) || ref $path;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path or ref $path 6 29 35
Condition100%3/3
not defined $path or ref $path
l!l&&r!l&&!r
2429
MC/DC100%2/2
not defined $path or ref $path
not defined $path ref $path
Truth table100%3/3
not(A) or B
A= not defined $pathB= ref $path
ABresult
1X1
011
000
539  
540 29 $path =~ s!//!/!g;
541 29 $abs = $path =~ s!^/!!;
542 29 $path =~ s!^\./!/!;
543 29 my @out = split m(/), $path, -1; #/ (syntax highlighting)
544 29 ▶ shift @out if $out[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $out[0] eq "" 9 20 29
545 29 ▶ pop @out if $out[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $out[-1] eq "" 6 23 29
546  
547 29 ( \@out, $abs, $follow );
548 }
549  
550 3679 ▶sub _set_state {
Subroutine _set_state: called CC=2 SCAR=6.9
551 3679 my $self = shift;
552  
553 3679 my %par = validate(
554 @_,
555 { state => { type => SCALAR },
556 dir => { type => GLOBREF | CODEREF, optional => 1 },
557 wildcard => 0,
558 }
559 );
560 3679 $self->{$_} = $par{$_} for keys %par;
561 }
562  
563 1600 ▶sub _push_state {
Subroutine _push_state: called CC=3 SCAR=11.0
564 1600 my $self = shift;
565  
566 $self->_debug( "Push state: "
567 . $self->{state}
568 . " resulting_path: "
569 . $self->{resulting_path}
570 . " Wildcard: "
571 . ( $self->{wildcard} || '' )
572 . " path_remaining: "
573 1600 ▶ . join( '/', @{ $self->{path_remaining} } )
Condition100%2/2
$self->{'wildcard'} || ''
l!l
158416
MC/DC100%1/1
$self->{'wildcard'} || ''
$self->{'wildcard'}
Truth table100%2/2
A
A= $self->{'wildcard'}
Aresult
11
00
574 . "\n" );
575 1600 push @{ $self->{state_stack} }, {
576 map {
577 1600 $_, ( ref( $self->{$_} ) eq 'ARRAY' )
578 1600 ? [ @{ $self->{$_} } ]
579 6400 ▶ : $self->{$_}
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{$_} eq 'ARRAY' ? : 1600 4800 6400
580 } qw/ state path_remaining dir resulting_path /
581 };
582 }
583  
584 1638 ▶sub _pop_state {
Subroutine _pop_state: called CC=5 SCAR=16.1
585 1638 my $self = shift;
586  
587 1638 ▶ $self->{state_stack} ||= [];
Condition100%2/2
$self->{"state_stack"} ||= []
l!l
16335
MC/DC100%1/1
$self->{"state_stack"} ||= []
$self->{"state_stack"}
Truth table100%2/2
A
A= $self->{"state_stack"}
Aresult
11
00
588 my $newstate
589 1638 = @{ $self->{state_stack} }
590 1638 ▶ ? pop( @{ $self->{state_stack} } )
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'state_stack'};} ? : 1605 33 1638
591 : { state => 'finished', dir => undef };
592 1638 $self->{$_} = $newstate->{$_} for keys %$newstate;
593 $self->_debug( "Pop state to "
594 . $self->{state}
595 . " resulting_path: "
596 . $self->{resulting_path}
597 . " Wildcard: "
598 . ( $self->{wildcard} || '' )
599 . " path_remaining: "
600 1638 ▶ . join( '/', @{ $self->{path_remaining} } )
Condition100%2/2
$self->{'wildcard'} || ''
l!l
162117
MC/DC100%1/1
$self->{'wildcard'} || ''
$self->{'wildcard'}
Truth table100%2/2
A
A= $self->{'wildcard'}
Aresult
11
00
601 . "\n" );
602 }
603  
604 29 ▶sub _state_initial {
Subroutine _state_initial: called CC=2 SCAR=6.9
605 29 my $self = shift;
606  
607 29 ▶ $self->{resulting_path} = $self->{absolute} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'absolute'} ? : 7 22 29
608 29 $self->{path_remaining} = [ @{ $self->{path} } ];
609  
610 29 $self->_set_state( state => 'nextdir' );
611 }
612  
613 34 ▶sub _state_finished {
Subroutine _state_finished: called CC=1 SCAR=0.0
614 34 my $self = shift;
615  
616 34 $self->{retval} = undef; # Autovivification optimises this away :(
617 }
618  
619 3750 ▶sub _state_nextdir {
Subroutine _state_nextdir: called CC=27 SCAR=33.0
620 3750 my $self = shift;
621  
622 3750 ▶ unless ( @{ $self->{path_remaining} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"path_remaining"};}) 539 3211 3750
623 539 $self->_debug("Exhaused path\n");
624 539 my $re = $self->{match};
625 $self->{retval} = $self->_derived
626 if ( -e $self->{resulting_path} )
627 539 ▶ && ( $self->{resulting_path} =~ /$re/ );
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->{"resulting_path"} and $self->{"resulting_path"} =~ /$re/i 70 469 539
Condition100%3/3
-e $self->{"resulting_path"} and $self->{"resulting_path"} =~ /$re/i
!ll&&!rl&&r
465470
MC/DC100%2/2
-e $self->{"resulting_path"} and $self->{"resulting_path"} =~ /$re/i
-e $self->{"resulting_path"} $self->{"resulting_path"} =~ /$re/i
Truth table100%3/3
A and B
A= -e $self->{"resulting_path"}B= $self->{"resulting_path"} =~ /$re/i
ABresult
0X0
100
111
628 539 $self->_pop_state;
629 539 return;
630 }
631  
632 3211 my $pathcomp = shift @{ $self->{path_remaining} };
633 3211 $self->_debug("Path component '$pathcomp'\n");
634 3211 ▶ if ( $pathcomp eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pathcomp eq '') { } 790 2421 3211
elsif (not $pathcomp =~ /\?|\*/) { } 1338 1083 2421
635 790 my $order = $self->{ellipsis_order};
636 790 ▶ $self->_set_state(
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'inside-out' ? : 12 778 790
637 state => ( $order eq 'inside-out' ) ? 'nextdir' : 'ellipsis' );
638 790 ▶ if ( $order ne 'breadth-first' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order ne "breadth-first") 784 6 790
639 784 $self->_push_state;
640 784 ▶ $self->_set_state(
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'inside-out' ? : 12 772 784
641 state => ( $order eq 'inside-out' )
642 ? 'ellipsis'
643 : 'nextdir'
644 );
645 }
646  
647 }
648 elsif ( $pathcomp !~ /\?|\*/ ) {
649 1338 $self->{resulting_path} .= $pathcomp;
650 1338 my $rp = $self->{resulting_path};
651 1338 ▶ if ( exists( $self->{exclude} ) && $rp =~ /$self->{exclude}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"exclude"} and $rp =~ /$self->{"exclude"}/) 10 1328 1338
Condition100%3/3
exists $self->{"exclude"} and $rp =~ /$self->{"exclude"}/
!ll&&!rl&&r
112820010
MC/DC100%2/2
exists $self->{"exclude"} and $rp =~ /$self->{"exclude"}/
exists $self->{"exclude"} $rp =~ /$self->{"exclude"}/
Truth table100%3/3
A and B
A= exists $self->{"exclude"}B= $rp =~ /$self->{"exclude"}/
ABresult
0X0
100
111
652 10 $self->_pop_state;
653 10 return;
654 }
655 1328 my $sl = readlink $rp;
656 1328 ▶ if ($sl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sl) 5 1323 1328
657 5 my $slpath = File::Spec->rel2abs( $sl, $rp );
658 5 ▶ if ( exists $self->{seen_symlink}{$slpath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen_symlink"}{$slpath}) 2 3 5
659 2 $self->_pop_state;
660 2 return;
661 }
662 3 $self->{seen_symlink}{$slpath}++;
663 3 ▶ $self->{path_remaining} = [] unless $self->{follow};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"follow"} 2 1 3
664 }
665 1326 ▶ $self->{resulting_path} .= '/' if -d $self->{resulting_path};
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"resulting_path"} 347 979 1326
666 }
667 else {
668 1083 my $wcdir;
669 1083 ▶ if ( !opendir $wcdir, $self->{resulting_path} || '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir $wcdir, $self->{"resulting_path"} || ".") 638 445 1083
Condition100%2/2
$self->{'resulting_path'} || '.'
l!l
106815
MC/DC100%1/1
$self->{'resulting_path'} || '.'
$self->{'resulting_path'}
Truth table100%2/2
A
A= $self->{'resulting_path'}
Aresult
11
00
670 638 $self->_pop_state;
671 638 return;
672 }
673 445 my $wc_re = quotemeta $pathcomp;
674 445 $wc_re =~ s!((?:\\\?)+)!'(.{'.(length($1)/2).'})'!eg;
675 445 $wc_re =~ s!\\\*!([^/]*)!g;
676 my %newstate = (
677 state => 'wildcard',
678 dir => $wcdir,
679 wildcard => $self->{case_insensitive}
680 445 ▶ ? qr(^$wc_re$)i
Branch: 1 branch
BranchTrueFalseTotal
$self->{'case_insensitive'} ? : 350 95 445
681 : qr(^$wc_re$)
682 );
683 445 ▶ if ( $self->{sort} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort"}) 293 152 445
684 my @wcmatch = grep {
685 293 ( $_ ne '.' )
686 && ( $_ ne '..' )
687 1298 ▶ && ( $self->{case_insensitive} ? /$wc_re/i : /$wc_re/ )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'case_insensitive'} ? : 518 194 712
Condition100%6/6
$_ ne '.' && $_ ne '..' && ($self->{'case_insensitive'} ? /$wc_re/i : /$wc_re/)
!ll&&!rl&&r
586134578
$_ ne "." and $_ ne ".."
!ll&&!rl&&r
293293712
MC/DC100%3/3
$_ ne '.' && $_ ne '..' && ($self->{'case_insensitive'} ? /$wc_re/i : /$wc_re/)
$_ ne "." $_ ne ".." ($self->{'case_insensitive'} ? /$wc_re/i : /$wc_re/)
Truth table100%4/4
A and B && C
A= $_ ne "."B= $_ ne ".."C= ($self->{'case_insensitive'} ? /$wc_re/i : /$wc_re/)
ABCresult
0XX0
10X0
1100
1111
688 } readdir($wcdir);
689  
690 293 ▶ if ( $^O =~ /vms/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /vms/i) 0 293 293
691 0 s/\.dir$// for @wcmatch;
692 }
693  
694 @wcmatch
695 = ( ref( $self->{sort} ) eq 'CODE' )
696 1 ▶ ? ( sort { &{ $self->{sort} }( $a, $b ) } @wcmatch )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'case_insensitive'} ? : 201 88 289
697 : $self->{case_insensitive}
698 293 ▶ ? ( sort { lc($a) cmp lc($b) } @wcmatch )
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'sort'} eq 'CODE' ? : 4 289 293
699 : ( sort @wcmatch );
700 293 ▶ if ( $self->{exclude} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude"}) 74 219 293
701 @wcmatch = grep {
702 74 ( $self->{path_remaining} . $_ )
703 !~ /$self->{exclude}/
704 } @wcmatch;
705 }
706 293 $newstate{state} = 'wildcard_sorted';
707 871 ▶ $newstate{dir} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
708 871 ▶ my $fil = ( shift @wcmatch ) || '';
Condition100%2/2
shift @wcmatch || ''
l!l
578293
MC/DC100%1/1
shift @wcmatch || ''
shift @wcmatch
Truth table100%2/2
A
A= shift @wcmatch
Aresult
11
00
709 871 my $rem = join ' ', @wcmatch;
710 871 $self->_debug("wildcard_sorted yields $fil remaining $rem\n");
711 871 return $fil;
712 293 };
713 }
714 445 $self->_set_state(%newstate);
715 }
716 }
717  
718 382 ▶sub _state_wildcard {
Subroutine _state_wildcard: called CC=8 SCAR=21.0
719 382 my $self = shift;
720  
721 382 my $fil = '.';
722 382 my $re = $self->{wildcard};
723 382 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($fil eq "." or $fil eq ".." or not $fil =~ /$re/i or exists $self->{"exclude"} and $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/) 825 230 1055
Condition75%9/12
$fil eq "." or $fil eq ".." or not $fil =~ /$re/i or exists $self->{"exclude"} and $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/
l!l&&r!l&&!r
8250230
$fil eq "." or $fil eq ".." or not $fil =~ /$re/i
l!l&&r!l&&!r
686139230
$fil eq "." or $fil eq ".."
l!l&&r!l&&!r
534152369
exists $self->{"exclude"} and $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/
!ll&&!rl&&r
23000
MC/DC60%3/5
$fil eq "." or $fil eq ".." or not $fil =~ /$re/i or exists $self->{"exclude"} and $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/
$fil eq "." $fil eq ".." not $fil =~ /$re/i exists $self->{"exclude"} $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/
Truth table66%4/6
A or B or not(C) or D and E
A= $fil eq "."B= $fil eq ".."C= not $fil =~ /$re/iD= exists $self->{"exclude"}E= $self->{"resulting_path"} . $fil =~ /$self->{"exclude"}/
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
724 ( $fil eq '.' )
725 || ( $fil eq '..' )
726 || ( $fil !~ /$re/ )
727 || ( exists( $self->{exclude} )
728 && ( $self->{resulting_path} . $fil =~ /$self->{exclude}/ ) )
729 )
730 {
731 825 $fil = readdir $self->{dir};
732 825 ▶ return $self->_pop_state unless defined $fil;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fil 152 673 825
733 }
734 230 ▶ $fil =~ s/.dir$// if $^O =~ /vms/i;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /vms/i 0 230 230
735 230 $self->_push_state;
736 230 unshift @{ $self->{path_remaining} }, $fil;
737 230 $self->_set_state( state => 'nextdir' );
738 }
739  
740 871 ▶sub _state_wildcard_sorted {
Subroutine _state_wildcard_sorted: called CC=2 SCAR=6.9
741 871 my $self = shift;
742  
743 871 my $fil = &{ $self->{dir} };
744 871 ▶ return $self->_pop_state unless $fil;
Branch: 1 branch
BranchTrueFalseTotal
unless $fil 293 578 871
745 578 $self->_push_state;
746 578 unshift @{ $self->{path_remaining} }, $fil;
747 578 $self->_set_state( state => 'nextdir' );
748 }
749  
750 790 ▶sub _state_ellipsis {
Subroutine _state_ellipsis: called CC=2 SCAR=6.9
751 790 my $self = shift;
752  
753 790 ▶ if ( $self->{ellipsis_order} eq 'breadth-first' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ellipsis_order'} eq 'breadth-first') { } 6 784 790
754 6 unshift @{ $self->{path_remaining} }, '*', '';
755 6 $self->_set_state( state => 'nextdir' );
756 6 $self->_push_state;
757 6 splice @{ $self->{path_remaining} }, 1, 1;
758 }
759 else {
760 784 unshift @{ $self->{path_remaining} }, '*', '';
761 784 $self->_set_state( state => 'nextdir' );
762 }
763 }
764  
765 7 1; #this line is important and will help the module return a true value
766 __END__
↑ Summary Wildcard/Find.pm »