blib/lib/File/Find/Object/Rule.pm

filter: Statementstmt 78.3% 242 / 309 Branchbran 83.3% 50 / 60 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.5% 43 / 45 Podpod 100% 15 / 15 totaltotal 80.8% 359 / 444 SCAR 39.0
CC
37
Cov
79%
match
8.0
find
7.0
__ANON__
6.6
CRAP 49.5
↑ Summary Build »
1 # $Id: /mirror/lab/perl/File-Find-Rule/lib/File/Find/Rule.pm 2102 2006-06-01T15:39:03.942922Z richardc $
2  
3 package File::Find::Object::Rule;
4 7 $File::Find::Object::Rule::VERSION = '0.0312';
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Text::Glob 'glob_to_regex';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Number::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use File::Find::Object; # we're only wrapping for now
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Cwd; # 5.00503s File::Find goes screwy with max_depth == 0
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 use Class::XSAccessor accessors => {
20 "extras" => "extras",
21 "finder" => "finder",
22 "_match_cb" => "_match_cb",
23 "rules" => "rules",
24 "_relative" => "_relative",
25 "_subs" => "_subs",
26 "_maxdepth" => "_maxdepth",
27 "_mindepth" => "_mindepth",
28 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # we'd just inherit from Exporter, but I want the colon
31 sub import
32 {
33 9 ▶ my $pkg = shift;
Subroutine import: called CC=4 SCAR=13.9
34 9 my $to = caller;
35 9 for my $sym (qw( find rule ))
36 {
37 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 18 *{"$to\::$sym"} = \&{$sym};
39 }
40 9 for ( grep /^:/, @_ )
41 {
42 2 my ($extension) = /^:(.*)/;
43 2 eval "require File::Find::Object::Rule::$extension";
44 2 ▶ croak "couldn't bootstrap File::Find::Object::Rule::$extension: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
45 if $@;
46 }
47 }
48  
49  
50 # the procedural shim
51  
52 7 *rule = \&find;
53  
54 sub find
55 {
56 28 ▶ my $object = __PACKAGE__->new();
Subroutine find: called CC=7 SCAR=19.5
Pod documented
57 28 my $not = 0;
58  
59 28 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 74 13 87
60 {
61 74 my $method = shift;
62 74 my @args;
63  
64 74 ▶ if ( $method =~ s/^\!// )
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ s/^\!//) 1 73 74
65 {
66 # jinkies, we're really negating this
67 1 unshift @_, $method;
68 1 $not = 1;
69 1 next;
70 }
71 73 ▶ unless ( defined prototype $method )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined prototype $method) 53 20 73
72 {
73 53 my $args = shift;
74 53 ▶ @args = ref $args eq 'ARRAY' ? @$args : $args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 6 47 53
75 }
76 73 ▶ if ($not)
Branch: 1 branch
BranchTrueFalseTotal
if ($not) 1 72 73
77 {
78 1 $not = 0;
79 1 @args = ref($object)->new->$method(@args);
80 1 $method = "not";
81 }
82  
83 73 my @return = $object->$method(@args);
84 73 ▶ return @return if $method eq 'in';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "in" 15 58 73
85 }
86 13 $object;
87 }
88  
89  
90 sub new
91 {
92 # We need this to maintain compatibility with File-Find-Object.
93 # However, Randal Schwartz recommends against this practice in general:
94 # http://www.stonehenge.com/merlyn/UnixReview/col52.html
95 59 ▶ my $referent = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
96 59 ▶ my $class = ref $referent || $referent;
Condition66%2/3
ref $referent || $referent
l!l&&r!l&&!r
1580
MC/DC0%0/2
ref $referent || $referent
ref $referent $referent
Truth table66%2/3
A || B
A= ref $referentB= $referent
ABresult
1X1
011
000
97  
98 59 return bless {
99 rules => [], # [0]
100 _subs => [], # [1]
101 iterator => [],
102 extras => {},
103 _maxdepth => undef,
104 _mindepth => undef,
105 _relative => 0,
106 }, $class;
107 }
108  
109 sub _force_object
110 {
111 340 ▶ my $object = shift;
Subroutine _force_object: called CC=2 SCAR=6.9
112 340 ▶ if ( !ref($object) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $object) 22 318 340
113 {
114 22 $object = $object->new();
115 }
116 340 return $object;
117 }
118  
119  
120 sub _flatten
121 {
122 24 ▶ my @flat;
Subroutine _flatten: called CC=3 SCAR=11.0
123 24 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 28 24 52
124 {
125 28 my $item = shift;
126 28 ▶ ref $item eq 'ARRAY' ? push @_, @{$item} : push @flat, $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 1 27 28
127 }
128 24 return @flat;
129 }
130  
131 sub _add_rule
132 {
133 91 ▶ my $self = shift;
Subroutine _add_rule: called CC=1 SCAR=0.0
134 91 my $new_rule = shift;
135  
136 91 push @{ $self->rules() }, $new_rule;
137  
138 91 return;
139 }
140  
141 sub name
142 {
143 24 ▶ my $self = _force_object shift;
Subroutine name: called CC=2 SCAR=6.9
Pod documented
144 24 ▶ my @names = map { ref $_ eq "Regexp" ? $_ : glob_to_regex $_ } _flatten(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'Regexp' ? : 15 12 27
145  
146 $self->_add_rule(
147 {
148 rule => 'name',
149 24 code => join( ' || ', map { "m($_)" } @names ),
150 args => \@_,
151 }
152 );
153  
154 24 $self;
155 }
156  
157  
158 7 ▶use vars qw( %X_tests );
Subroutine BEGIN: called CC=1 SCAR=0.0
159 7 %X_tests = (
160 -r => readable => -R => r_readable => -w => writeable => -W =>
161 r_writeable => -w => writable => -W => r_writable => -x =>
162 executable => -X => r_executable => -o => owned => -O => r_owned =>
163  
164 -e => exists => -f => file => -z => empty => -d => directory => -s =>
165 nonempty => -l => symlink => => -p => fifo => -u => setuid => -S =>
166 socket => -g => setgid => -b => block => -k => sticky => -c =>
167 character => => -t => tty => -M => modified => -A => accessed => -T =>
168 ascii => -C => changed => -B => binary =>
169 );
170  
171 7 for my $test ( keys %X_tests )
172 {
173 my $sub = eval 'sub () {
174 my $self = _force_object shift;
175 $self->_add_rule({
176 code => "' . $test . ' \$path",
177 0 ▶ rule => "' . $X_tests{$test} . '",
Subroutine __ANON__: not called CC=1 SCAR=4.2
178 });
179 $self;
180 } ';
181 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
182 189 *{ $X_tests{$test} } = $sub;
183 }
184  
185  
186 7 ▶use vars qw( @stat_tests );
Subroutine BEGIN: called CC=1 SCAR=0.0
187 7 @stat_tests = qw( dev ino mode nlink uid gid rdev
188 size atime mtime ctime blksize blocks );
189 {
190 7 my $i = 0;
191 7 for my $test (@stat_tests)
192 {
193 91 my $index = $i++; # to close over
194 my $sub = sub {
195 7 ▶ my $self = _force_object shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
196  
197 7 my @tests = map { Number::Compare->parse_to_perl($_) } @_;
198  
199 $self->_add_rule(
200 {
201 rule => $test,
202 args => \@_,
203 code => 'do { my $val = (stat $path)['
204 . $index
205 . '] || 0;'
206 7 . join( '||', map { "(\$val $_)" } @tests ) . ' }',
207 }
208 );
209 7 $self;
210 91 };
211 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 91 *$test = $sub;
213 }
214 }
215  
216  
217 sub any
218 {
219 8 ▶ my $self = _force_object shift;
Subroutine any: called CC=1 SCAR=0.0
Pod documented
220 8 my @rulesets = @_;
221  
222 $self->_add_rule(
223 {
224 rule => 'any',
225 code => '('
226 . join( ' || ',
227 8 map { "( " . $_->_compile( $self->_subs() ) . " )" } @rulesets )
228 . ")",
229 args => \@rulesets,
230 }
231 );
232 8 $self;
233 }
234  
235 7 *or = \&any;
236  
237  
238 sub not
239 {
240 3 ▶ my $self = _force_object shift;
Subroutine not: called CC=1 SCAR=0.0
Pod documented
241 3 my @rulesets = @_;
242  
243 $self->_add_rule(
244 {
245 rule => 'not',
246 args => \@rulesets,
247 code => '('
248 . join( ' && ',
249 3 map { "!(" . $_->_compile( $self->_subs() ) . ")" } @_ )
250 . ")",
251 }
252 );
253 3 $self;
254 }
255  
256 7 *none = \¬
257  
258  
259 sub prune ()
260 {
261 4 ▶ my $self = _force_object shift;
Subroutine prune: called CC=1 SCAR=0.0
Pod documented
262  
263 4 $self->_add_rule(
264 {
265 rule => 'prune',
266 code => 'do { $self->finder->prune(); 1 }'
267 },
268 );
269  
270 4 return $self;
271 }
272  
273  
274 sub discard ()
275 {
276 6 ▶ my $self = _force_object shift;
Subroutine discard: called CC=1 SCAR=0.0
Pod documented
277  
278 6 $self->_add_rule(
279 {
280 rule => 'discard',
281 code => '$discarded = 1',
282 }
283 );
284  
285 6 return $self;
286 }
287  
288  
289 sub exec
290 {
291 19 ▶ my $self = _force_object shift;
Subroutine exec: called CC=1 SCAR=0.0
Pod documented
292 19 my $code = shift;
293  
294 19 $self->_add_rule(
295 {
296 rule => 'exec',
297 code => $code,
298 }
299 );
300  
301 19 return $self;
302 }
303  
304  
305 sub grep
306 {
307 1 ▶ my $self = _force_object shift;
Subroutine grep: called CC=4 SCAR=14.3
Pod documented
308 my @pattern = map {
309 1 ref $_
310 ? ref $_ eq 'ARRAY'
311 2 ▶ ? map { [ ( ref $_ ? $_ : qr/$_/ ) => 0 ] } @$_
Branch: 3 branches
BranchTrueFalseTotal
ref $_ ? : 2 0 2
ref $_ eq 'ARRAY' ? : 1 1 2
ref $_ ? : 1 0 1
312 : [ $_ => 1 ]
313 : [ qr/$_/ => 1 ]
314 } @_;
315  
316 $self->exec(
317 sub {
318 3 ▶ local *FILE;
Subroutine __ANON__: called CC=6 SCAR=18.8
319 3 ▶ open FILE, $self->finder->item() or return;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $self->finder->item or return 0 3 3
320 3 local ( $_, $. );
321 3 ▶ while (<FILE>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 3 0 3
322 {
323 3 for my $p (@pattern)
324 {
325 5 my ( $rule, $ret ) = @$p;
326 5 ▶ return $ret
Branch: 2 branches
BranchTrueFalseTotal
if ref $rule eq "Regexp" ? /$rule/ : &$rule(@_) 3 2 5
ref $rule eq 'Regexp' ? : 5 0 5
327 if ref $rule eq 'Regexp'
328 ? /$rule/
329 : $rule->(@_);
330 }
331 }
332 0 return;
333 }
334 1 );
335 }
336  
337  
338 sub maxdepth
339 {
340 22 ▶ my $self = _force_object shift;
Subroutine maxdepth: called CC=1 SCAR=0.0
Pod documented
341 22 $self->_maxdepth(shift);
342 22 return $self;
343 }
344  
345 sub mindepth
346 {
347 2 ▶ my $self = _force_object shift;
Subroutine mindepth: called CC=1 SCAR=0.0
Pod documented
348 2 $self->_mindepth(shift);
349 2 return $self;
350 }
351  
352  
353 sub relative ()
354 {
355 1 ▶ my $self = _force_object shift;
Subroutine relative: called CC=1 SCAR=0.0
Pod documented
356 1 $self->_relative(1);
357  
358 1 return $self;
359 }
360  
361  
362 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
363  
364 sub AUTOLOAD
365 {
366 1 ▶ $AUTOLOAD =~ /::not_([^:]*)$/
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$File::Find::Object::Rule::AUTOLOAD =~ /::not_([^:]*)$/ or croak("Can't locate method $File::Find::Object::Rule::AUTOLOAD") 0 1 1
367 or croak "Can't locate method $AUTOLOAD";
368 1 my $method = $1;
369  
370 my $sub = sub {
371 1 ▶ my $self = _force_object shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
372 1 $self->not( $self->new->$method(@_) );
373 1 };
374 {
375 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
376 1 *$AUTOLOAD = $sub;
377 }
378 1 &$sub;
379 }
380  
381  
382 sub _call_find
383 {
384 42 ▶ my $self = shift;
Subroutine _call_find: called CC=1 SCAR=0.0
385 42 my $paths = shift;
386  
387 42 my $finder = File::Find::Object->new( $self->extras(), @$paths );
388  
389 42 $self->finder($finder);
390  
391 42 return;
392 }
393  
394 sub _compile
395 {
396 61 ▶ my $self = shift;
Subroutine _compile: called CC=3 SCAR=11.0
397 61 my $subs = shift;
398  
399 61 ▶ return '1' unless @{ $self->rules() };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->rules;} 6 55 61
400  
401 my $code = join " && ", map {
402 94 ▶ if ( ref $_->{code} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{'code'}) { } 19 75 94
403 {
404 19 push @$subs, $_->{code};
405 19 "\$subs->[$#{$subs}]->(\@args) # $_->{rule}\n";
406 }
407 else
408 {
409 75 "( $_->{code} ) # $_->{rule}\n";
410 }
411 55 } @{ $self->rules() };
412  
413 55 return $code;
414 }
415  
416 sub in
417 {
418 40 ▶ my $self = _force_object shift;
Subroutine in: called CC=2 SCAR=6.9
Pod documented
419 40 my @paths = @_;
420  
421 40 $self->start(@paths);
422  
423 40 my @results;
424  
425 40 ▶ while ( defined( my $match = $self->match() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $match = $self->match)) 85 40 125
426 {
427 85 push @results, $match;
428 }
429  
430 40 return @results;
431 }
432  
433  
434 sub start
435 {
436 42 ▶ my $self = _force_object shift;
Subroutine start: called CC=4 SCAR=14.1
Pod documented
437 42 my @paths = @_;
438  
439 42 my $fragment = $self->_compile( $self->_subs() );
440  
441 42 my $subs = $self->_subs();
442  
443 42 ▶ warn "relative mode handed multiple paths - that's a bit silly\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_relative and @paths > 1 0 42 42
Condition66%2/3
$self->_relative and @paths > 1
!ll&&!rl&&r
4110
MC/DC0%0/2
$self->_relative and @paths > 1
$self->_relative @paths > 1
Truth table66%2/3
A and B
A= $self->_relativeB= @paths > 1
ABresult
0X0
100
111
444 if $self->_relative() && @paths > 1;
445  
446 42 my $code = 'sub {
447 my $path_obj = shift;
448 my $path = shift;
449  
450 if (!defined($path_obj))
451 {
452 return;
453 }
454  
455 $path =~ s#^(?:\./+)+##;
456 my $path_dir = dirname($path);
457 my $path_base = fileparse($path);
458 my @args = ($path_base, $path_dir, $path);
459 local $_ = $path_base;
460 my $maxdepth = $self->_maxdepth;
461 my $mindepth = $self->_mindepth;
462  
463 my $comps = $path_obj->full_components();
464  
465 my $depth = scalar(@$comps);
466  
467 defined $maxdepth && $depth >= $maxdepth
468 and $self->finder->prune();
469  
470 defined $mindepth && $depth < $mindepth
471 and return;
472  
473 #print "Testing \'$_\'\n";
474  
475 my $discarded;
476 return unless ' . $fragment . ';
477 return if $discarded;
478 return $path;
479 }';
480  
481 #use Data::Dumper;
482 #print Dumper \@subs;
483 #warn "Compiled sub: '$code'\n";
484  
485 42 ▶ my $callback = eval "$code" or die "compile error '$code' $@";
Branch: 1 branch
BranchTrueFalseTotal
my $callback = eval "$code" or die "compile error '${code}' $@" 0 42 42
486  
487 42 $self->_match_cb($callback);
488 42 $self->_call_find( \@paths );
489  
490 42 return $self;
491 }
492  
493  
494 sub match
495 {
496 140 ▶ my $self = _force_object shift;
Subroutine match: called CC=8 SCAR=20.8
Pod documented
497  
498 140 my $finder = $self->finder();
499  
500 140 my $match_cb = $self->_match_cb();
501 140 my $preproc_cb = $self->extras()->{'preprocess'};
502  
503 140 ▶ while ( defined( my $next_obj = $finder->next_obj() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next_obj = $finder->next_obj)) 300 42 342
504 {
505 300 ▶ if ( defined($preproc_cb) && $next_obj->is_dir() )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preproc_cb and $next_obj->is_dir) 7 293 300
Condition100%3/3
defined $preproc_cb and $next_obj->is_dir
!ll&&!rl&&r
28947
MC/DC100%2/2
defined $preproc_cb and $next_obj->is_dir
defined $preproc_cb $next_obj->is_dir
Truth table100%3/3
A and B
A= defined $preproc_cbB= $next_obj->is_dir
ABresult
0X0
100
111
506 {
507 $finder->set_traverse_to(
508 $preproc_cb->(
509 7 $self, [ @{ $finder->get_current_node_files_list() } ]
510 )
511 );
512 }
513  
514 300 ▶ if ( defined( my $path = $match_cb->( $next_obj, $next_obj->path() ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $path = &$match_cb($next_obj, $next_obj->path))) 98 202 300
515 {
516 98 ▶ if ( $self->_relative )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_relative) { } 1 97 98
517 {
518 1 my $comps = $next_obj->full_components();
519 1 ▶ if (@$comps)
Branch: 1 branch
BranchTrueFalseTotal
if (@$comps) 1 0 1
520 {
521 return (
522 1 ▶ $next_obj->is_dir()
Branch: 1 branch
BranchTrueFalseTotal
$next_obj->is_dir ? : 0 1 1
523 ? File::Spec->catdir(@$comps)
524 : File::Spec->catfile(@$comps)
525 );
526 }
527 }
528 else
529 {
530 97 return $path;
531 }
532 }
533  
534 }
535  
536 42 return;
537 }
538  
539 7 1;
540  
541 __END__
↑ Summary Build »