Finder/Steps.pm

filter: Statementstmt 52.2% 116 / 222 Branchbran 41.3% 38 / 92 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.8% 47 / 76 Podpod 100% 31 / 31 totaltotal 53.6% 234 / 436 CC 53 SCAR 34.3
CC
53
Cov
48%
_ls
240.0
__ANON__
12.0
__ANON__
12.0
CRAP 31.0
↑ Summary Finder.pm »
1 package File::Finder::Steps;
2  
3 4 our $VERSION = '1.01';
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 File::Finder::Steps - steps for File::Finder
12  
13 =head1 SYNOPSIS
14  
15 ## See File::Finder for normal use of steps
16  
17 ## subclassing example:
18 BEGIN {
19 package My::File::Finder;
20 use base File::Finder;
21  
22 sub _steps_class { "My::File::Finder::Steps" }
23 }
24 BEGIN {
25 package My::File::Finder::Steps;
26 use base File::Finder::Steps;
27  
28 sub bigger_than { # true if bigger than N bytes
29 my $self = shift;
30 my $bytes = shift;
31 return sub {
32 -s > $bytes;
33 }
34 }
35 }
36  
37 my $over_1k = My::File::Finder->bigger_than(1024);
38 print "Temp files over 1k:\n";
39 $over_1k->ls->in("/tmp");
40  
41 =head1 DESCRIPTION
42  
43 C<File::Finder::Steps> provide the predicates being tested for
44 C<File::Finder>.
45  
46 =head2 STEPS METHODS
47  
48 These methods are called on a class or instance to add a "step". Each
49 step adds itself to a list of steps, returning the new object. This
50 allows you to chain steps together to form a formula.
51  
52 As in I<find>, the default operator is "and", and short-circuiting is
53 performed.
54  
55 Note: the C<user>, C<nouser>, C<group>, C<nogroup>, and C<ls> methods
56 are not available on Win32 systems.
57  
58 =over
59  
60 =item or
61  
62 Like I<find>'s C<or>.
63  
64 =cut
65  
66 63 ▶sub or { return "or" }
Subroutine or: called CC=1 SCAR=0.0
Pod documented
67  
68 =item left
69  
70 Like a left parenthesis. Used in nesting pairs with C<right>.
71  
72 =cut
73  
74 27 ▶sub left { return "left" }
Subroutine left: called CC=1 SCAR=0.0
Pod documented
75 4 ▶BEGIN { *begin = \&left; }
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 =item right
78  
79 Like a right parenthesis. Used in nesting pairs with C<left>.
80 For example:
81  
82 my $big_or_old = File::Finder
83 ->type('f')
84 ->left
85 ->size("+100")->or->mtime("+90")
86 ->right;
87 find($big_or_old->ls, "/tmp");
88  
89 You need parens because the "or" operator is lower precedence than
90 the implied "and", for the same reason you need them here:
91  
92 find /tmp -type f '(' -size +100 -o -mtime +90 ')' -print
93  
94 Without the parens, the -type would bind to -size, and not to the
95 choice of -size or -mtime.
96  
97 Mismatched parens will not be found until the formula is used, causing
98 a fatal error.
99  
100 =cut
101  
102 27 ▶sub right { return "right" }
Subroutine right: called CC=1 SCAR=0.0
Pod documented
103 4 ▶BEGIN { *end = \&right; }
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 =item begin
106  
107 Alias for C<left>.
108  
109 =item end
110  
111 Alias for C<right>.
112  
113 =item not
114  
115 Like I<find>'s C<!>. Prefix operator, can be placed in front of
116 individual terms or open parens. Can be nested, but what's the point?
117  
118 # list all non-files in /tmp
119 File::Finder->not->type('f')->ls->in("/tmp");
120  
121 =cut
122  
123 13 ▶sub not { return "not" }
Subroutine not: called CC=1 SCAR=0.0
Pod documented
124  
125 =item true
126  
127 Always returns true. Useful when a subexpression might fail, but
128 you don't want the overall code to fail:
129  
130 ... ->left-> ...[might return false]... ->or->true->right-> ...
131  
132 Of course, this is the I<find> command's idiom of:
133  
134 find .... '(' .... -o -true ')' ...
135  
136 =cut
137  
138 5 ▶sub true { return sub { 1 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine true: called CC=1 SCAR=0.0
Pod documented
139  
140 =item false
141  
142 Always returns false.
143  
144 =cut
145  
146 11 ▶sub false { return sub { 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine false: called CC=1 SCAR=0.0
Pod documented
147  
148 =item comma
149  
150 Like GNU I<find>'s ",". The result of the expression (or
151 subexpression if in parens) up to this point is discarded, and
152 execution continues afresh. Useful when a part of the expression is
153 needed for its side effects, but shouldn't affect the rest of the
154 "and"-ed chain.
155  
156 # list all files and dirs, but don't descend into CVS dir contents:
157 File::Finder->type('d')->name('CVS')->prune->comma->ls->in('.');
158  
159 =cut
160  
161 12 ▶sub comma { return "comma" } # gnu extension
Subroutine comma: called CC=1 SCAR=0.0
Pod documented
162  
163 =item follow
164  
165 Enables symlink following, and returns true.
166  
167 =cut
168  
169 1 ▶sub follow {
Subroutine follow: called CC=1 SCAR=0.2
Pod documented
170 1 my $self = shift;
171 1 $self->{options}{follow} = 1;
172 0 ▶ return sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
173 }
174  
175 =item name(NAME)
176  
177 True if basename matches NAME, which can be given as a glob
178 pattern or a regular expression object:
179  
180 my $pm_files = File::Finder->name('*.pm')->in('.');
181 my $pm_files_too = File::Finder->name(qr/pm$/)->in('.');
182  
183 =cut
184  
185 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
186 2 my $self = shift;
187 2 my $name = shift;
188  
189 2 ▶ unless (UNIVERSAL::isa($name, "Regexp")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($name, "Regexp")) 1 1 2
190 1 require Text::Glob;
191 1 $name = Text::Glob::glob_to_regex($name);
192 }
193  
194 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 154 /$name/;
196 2 };
197 }
198  
199 =item perm(PERMISSION)
200  
201 Like I<find>'s C<-perm>. Leading "-" means "all of these bits".
202 Leading "+" means "any of these bits". Value is de-octalized if a
203 leading 0 is present, which is likely only if it's being passed as a
204 string.
205  
206 my $files = File::Finder->type('f');
207 # find files that are exactly mode 644
208 my $files_644 = $files->perm(0644);
209 # find files that are at least world executable:
210 my $files_world_exec = $files->perm("-1");
211 # find files that have some executable bit set:
212 my $files_exec = $files->perm("+0111");
213  
214 =cut
215  
216 7 ▶sub perm {
Subroutine perm: called CC=7 SCAR=19.6
Pod documented
217 7 my $self = shift;
218 7 my $perm = shift;
219 7 ▶ $perm =~ /^(\+|-)?\d+\z/ or croak "bad permissions $perm";
Branch: 1 branch
BranchTrueFalseTotal
$perm =~ /^(\+|-)?\d+\z/ or croak("bad permissions $perm") 7 0 7
220 7 ▶ if ($perm =~ s/^-//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($perm =~ s/^-//) { } 1 6 7
elsif ($perm =~ s/^\+//) { } 3 3 6
221 1 ▶ $perm = oct($perm) if $perm =~ /^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $perm =~ /^0/ 1 0 1
222 77 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 77 ((stat _)[2] & $perm) == $perm;
224 1 };
225 } elsif ($perm =~ s/^\+//) { # gnu extension
226 3 ▶ $perm = oct($perm) if $perm =~ /^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $perm =~ /^0/ 3 0 3
227 231 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 231 ((stat _)[2] & $perm);
229 3 };
230 } else {
231 3 ▶ $perm = oct($perm) if $perm =~ /^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $perm =~ /^0/ 2 1 3
232 231 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 231 ((stat _)[2] & 0777) == $perm;
234 3 };
235 }
236 }
237  
238 =item type(TYPE)
239  
240 Like I<find>'s C<-type>. All native Perl types are supported. Note
241 that C<s> is a socket, mapping to Perl's C<-S>, to be consistent with
242 I<find>. Returns true or false, as appropriate.
243  
244 =cut
245  
246 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
247 4 my %typecast;
248  
249 2 ▶ sub type {
Subroutine type: called CC=3 SCAR=11.2
Pod documented
250 2 my $self = shift;
251 2 my $type = shift;
252  
253 2 ▶ $type =~ /^[a-z]\z/i or croak "bad type $type";
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^[a-z]\z/i or croak("bad type $type") 2 0 2
254 2 $type =~ s/s/S/;
255  
256 2 ▶ $typecast{$type} ||= eval "sub { -$type _ }";
Condition66%2/3
$typecast{$type} ||= eval "sub { -$type _ }"
l!l&&r!l&&!r
110
MC/DC0%0/2
$typecast{$type} ||= eval "sub { -$type _ }"
$typecast{$type} eval "sub { -$type _ }"
Truth table66%2/3
A ||= B
A= $typecast{$type}B= eval "sub { -$type _ }"
ABresult
1X1
011
000
257 }
258 }
259  
260 =item print
261  
262 Prints the fullname to C<STDOUT>, followed by a newline. Returns true.
263  
264 =cut
265  
266 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
267 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 print $File::Find::name, "\n";
269 0 1;
270 0 };
271 }
272  
273 =item print0
274  
275 Prints the fullname to C<STDOUT>, followed by a NUL. Returns true.
276  
277 =cut
278  
279 0 ▶sub print0 {
Subroutine print0: not called CC=1 SCAR=6.9
Pod documented
280 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
281 0 print $File::Find::name, "\0";
282 0 1;
283 0 };
284 }
285  
286 =item fstype
287  
288 Not implemented yet.
289  
290 =item user(USERNAME|UID)
291  
292 True if the owner is USERNAME or UID.
293  
294 =cut
295  
296 2 ▶sub user {
Subroutine user: called CC=4 SCAR=14.3
Pod documented
297 2 my $self = shift;
298 2 my $user = shift;
299  
300 2 ▶ croak 'user not supported on this platform' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 2 2
301  
302 2 ▶ my $uid = ($user =~ /^\d+\z/) ? $user : _user_to_uid($user);
Branch: 1 branch
BranchTrueFalseTotal
$user =~ /^\d+\z/ ? : 2 0 2
303 2 ▶ die "bad user $user" unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 2 2
304  
305 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
306 154 (stat _)[4] == $uid;
307 2 };
308 }
309  
310 =item group(GROUPNAME|GID)
311  
312 True if the group is GROUPNAME or GID.
313  
314 =cut
315  
316 2 ▶sub group {
Subroutine group: called CC=4 SCAR=14.3
Pod documented
317 2 my $self = shift;
318 2 my $group = shift;
319  
320 2 ▶ croak 'group not supported on this platform' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 2 2
321  
322 2 ▶ my $gid = ($group =~ /^\d+\z/) ? $group : _group_to_gid($group);
Branch: 1 branch
BranchTrueFalseTotal
$group =~ /^\d+\z/ ? : 2 0 2
323 2 ▶ die "bad group $gid" unless defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gid 0 2 2
324  
325 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
326 154 (stat _)[5] == $gid;
327 2 };
328 }
329  
330 =item nouser
331  
332 True if the entry doesn't belong to any known user.
333  
334 =cut
335  
336 2 ▶sub nouser {
Subroutine nouser: called CC=2 SCAR=7.1
Pod documented
337 2 ▶ croak 'nouser not supported on this platform' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 2 2
338 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 154 CORE::not defined _uid_to_user((stat _)[4]);
340 }
341 2 }
342  
343 =item nogroup
344  
345 True if the entry doesn't belong to any known group.
346  
347 =cut
348  
349 2 ▶sub nogroup {
Subroutine nogroup: called CC=2 SCAR=7.1
Pod documented
350 2 ▶ croak 'nogroup not supported on this platform' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 2 2
351 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
352 154 CORE::not defined _gid_to_group((stat _)[5]);
353 }
354 2 }
355  
356 =item links( +/- N )
357  
358 Like I<find>'s C<-links N>. Leading plus means "more than", minus
359 means "less than".
360  
361 =cut
362  
363 2 ▶sub links {
Subroutine links: called CC=1 SCAR=0.0
Pod documented
364 2 my $self = shift;
365 2 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
366  
367 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
368 154 _n($prefix, $n, (stat(_))[3]);
369 2 };
370 }
371  
372 =item inum( +/- N )
373  
374 True if the inode number meets the qualification.
375  
376 =cut
377  
378 0 ▶sub inum {
Subroutine inum: not called CC=1 SCAR=6.9
Pod documented
379 0 my $self = shift;
380 0 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
381  
382 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
383 0 _n($prefix, $n, (stat(_))[1]);
384 0 };
385 }
386  
387 =item size( +/- N [c/k])
388  
389 True if the file size meets the qualification. By default, N is
390 in half-K blocks. Append a trailing "k" to the number to indicate
391 1K blocks, or "c" to indicate characters (bytes).
392  
393 =cut
394  
395 2 ▶sub size {
Subroutine size: called CC=3 SCAR=15.4
Pod documented
396 2 my $self = shift;
397 2 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
398  
399 2 ▶ if ($n =~ s/c\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/c\z//) 2 0 2
400 154 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
401 154 _n($prefix, $n, int(-s _));
402 2 };
403 }
404 0 ▶ if ($n =~ s/k\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/k\z//) 0 0 0
405 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
406 0 _n($prefix, $n, int(((-s _)+1023) / 1024));
407 0 };
408 }
409 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
410 0 _n($prefix, $n, int(((-s _)+511) / 512));
411 0 };
412 }
413  
414 =item atime( +/- N )
415  
416 True if access time (in days) meets the qualification.
417  
418 =cut
419  
420 0 ▶sub atime {
Subroutine atime: not called CC=1 SCAR=6.9
Pod documented
421 0 my $self = shift;
422 0 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
423  
424 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
425 0 _n($prefix, $n, int(-A _));
426 0 };
427 }
428  
429 =item mtime( +/- N )
430  
431 True if modification time (in days) meets the qualification.
432  
433 =cut
434  
435 0 ▶sub mtime {
Subroutine mtime: not called CC=1 SCAR=6.9
Pod documented
436 0 my $self = shift;
437 0 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
438  
439 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 0 _n($prefix, $n, int(-M _));
441 0 };
442 }
443  
444 =item ctime( +/- N )
445  
446 True if inode change time (in days) meets the qualification.
447  
448 =cut
449  
450 0 ▶sub ctime {
Subroutine ctime: not called CC=1 SCAR=6.9
Pod documented
451 0 my $self = shift;
452 0 my ($prefix, $n) = shift =~ /^(\+|-|)(.*)/;
453  
454 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
455 0 _n($prefix, $n, int(-C _));
456 0 };
457 }
458  
459 =item exec(@COMMAND)
460  
461 Forks the child process via C<system()>. Any appearance of C<{}> in
462 any argument is replaced by the current filename. Returns true if the
463 child exit status is 0. The list is passed directly to C<system>,
464 so if it's a single arg, it can contain C</bin/sh> syntax. Otherwise,
465 it's a pre-parsed command that must be found on the PATH.
466  
467 Note that I couldn't figure out how to horse around with the current
468 directory very well, so I'm using C<$_> here instead of the more
469 traditional C<File::Find::name>. It still works, because we're still
470 chdir'ed down into the directory, but it looks weird on a trace.
471 Trigger C<no_chdir> in C<find> if you want a traditional I<find> full
472 path.
473  
474 my $f = File::Finder->exec('ls', '-ldg', '{}');
475 find({ no_chdir => 1, wanted => $f }, @starting_dirs);
476  
477 Yeah, it'd be trivial for me to add a no_chdir method. Soon.
478  
479 =cut
480  
481 0 ▶sub exec {
Subroutine exec: not called CC=1 SCAR=6.9
Pod documented
482 0 my $self = shift;
483 0 my @command = @ _;
484  
485 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
486 0 my @mapped = @command;
487 0 for my $one (@mapped) {
488 0 $one =~ s/{}/$_/g;
489 }
490 0 system @mapped;
491 0 return !$?;
492 0 };
493 }
494  
495 =item ok(@COMMAND)
496  
497 Like C<exec>, but displays the command line first, and waits for a
498 response. If the response begins with C<y> or C<Y>, runs the command.
499 If the command fails, or the response wasn't yes, returns false,
500 otherwise true.
501  
502 =cut
503  
504 0 ▶sub ok {
Subroutine ok: not called CC=1 SCAR=6.9
Pod documented
505 0 my $self = shift;
506 0 my @command = @ _;
507  
508 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
509 0 my @mapped = @command;
510 0 for my $one (@mapped) {
511 0 $one =~ s/{}/$_/g;
512 }
513 0 my $old = select(STDOUT);
514 0 $|++;
515 0 print "@mapped? ";
516 0 select $old;
517 0 ▶ return 0 unless <STDIN> =~ /^y/i;
Branch: 1 branch
BranchTrueFalseTotal
unless readline(STDIN) =~ /^y/i 0 0 0
518 0 system @mapped;
519 0 return !$?;
520 0 };
521 }
522  
523 =item prune
524  
525 Sets C<$File::Find::prune>, and returns true.
526  
527 =cut
528  
529 0 ▶sub prune {
Subroutine prune: not called CC=1 SCAR=6.9
Pod documented
530 0 ▶ return sub { $File::Find::prune = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
531 }
532  
533 =item xdev
534  
535 Not yet implemented.
536  
537 =item newer
538  
539 Not yet implemented.
540  
541 =item eval(CODEREF)
542  
543 Ah yes, the master escape, with extra benefits. Give it a coderef,
544 and it evaluates that code at the proper time. The return value is noted
545 for true/false and used accordingly.
546  
547 my $blaster = File::Finder->atime("+30")->eval(sub { unlink });
548  
549 But wait, there's more. If the parameter is an object that responds
550 to C<as_wanted>, that method is automatically called, hoping for a
551 coderef return. This neat feature allows subroutines to be created and
552 nested:
553  
554 my $old = File::Finder->atime("+30");
555 my $big = File::Finder->size("+100");
556 my $old_or_big = File::Finder->eval($old)->or->eval($big);
557 my $killer = File::Finder->eval(sub { unlink });
558 my $kill_old_or_big = File::Finder->eval($old_or_big)->ls->eval($killer);
559 $kill_old_or_big->in('/tmp');
560  
561 Almost too cool for words.
562  
563 =cut
564  
565 117 ▶sub eval {
Subroutine eval: called CC=2 SCAR=7.0
Pod documented
566 117 my $self = shift;
567 117 my $eval = shift;
568  
569 ## if this is another File::Finder object... then cheat:
570 117 ▶ $eval = $eval->as_wanted if UNIVERSAL::can($eval, "as_wanted");
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($eval, "as_wanted") 0 117 117
571  
572 117 return $eval; # just reuse the coderef
573 }
574  
575 =item depth
576  
577 Like I<find>'s C<-depth>. Sets a flag for C<as_options>, and returns true.
578  
579 =cut
580  
581 1 ▶sub depth {
Subroutine depth: called CC=1 SCAR=0.2
Pod documented
582 1 my $self = shift;
583 1 $self->{options}{bydepth} = 1;
584 0 ▶ return sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
585 }
586  
587 =item ls
588  
589 Like I<find>'s C<-ls>. Performs a C<ls -dils> on the entry to
590 C<STDOUT> (without forking), and returns true.
591  
592 =cut
593  
594 0 ▶sub ls {
Subroutine ls: not called CC=2 SCAR=17.9
Pod documented
595 0 ▶ croak 'ls not supported on this platform' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
596 0 return \&_ls;
597 }
598  
599 =item tar
600  
601 Not yet implemented.
602  
603 =item [n]cpio
604  
605 Not yet implemented.
606  
607 =item ffr($ffr_object)
608  
609 Incorporate a C<File::Find::Rule> object as a step. Note that this
610 must be a rule object, and not a result, so don't call or pass C<in>.
611 For example, using C<File::Find::Rule::ImageSize> to define a
612 predicate for image files that are bigger than a megapixel in my
613 friends folder, I get:
614  
615 require File::Finder;
616 require File::Find::Rule;
617 require File::Find::Rule::ImageSize;
618 my $ffr = File::Find::Rule->file->image_x('>1000')->image_y('>1000');
619 my @big_friends = File::Finder->ffr($ffr)
620 ->in("/Users/merlyn/Pictures/Sorted/Friends");
621  
622 =cut
623  
624 0 ▶sub ffr {
Subroutine ffr: not called CC=2 SCAR=14.8
Pod documented
625 0 my $self = shift;
626 0 my $ffr_object = shift;
627  
628 0 my $their_wanted;
629  
630 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
631 0 ▶ local *File::Find::find = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
632 0 my ($options) = @ _;
633 0 for (my ($k, $v) = each %$options) {
634 0 ▶ if ($k eq "wanted") {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq 'wanted') { } 0 0 0
635 0 $their_wanted = $v;
636 } else {
637 0 $self->{options}->{$k} = $v;
638 }
639 }
640 0 };
641 0 $ffr_object->in("/DUMMY"); # boom!
642 0 ▶ croak "no wanted defined" unless defined $their_wanted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $their_wanted 0 0 0
643 0 return $their_wanted;
644 }
645  
646 =item contains(pattern)
647  
648 True if the file contains C<pattern> (either a literal string
649 treated as a regex, or a true regex object).
650  
651 my $plugh_files = File::Finder->type('f')->contains(qr/plugh/);
652  
653 Searching is performed on a line-by-line basis, respecting the
654 current value of C<$/>.
655  
656 =cut
657  
658 1 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
659 1 my $self = shift;
660 1 my $pat = shift;
661 77 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
662 77 ▶ open my $f, "<" . $_ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $f, '<' . $_ or return 0 77 0 77
663 77 ▶ while (<$f>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $f)) 4360 76 4436
664 4360 ▶ return 1 if /$pat/;
Branch: 1 branch
BranchTrueFalseTotal
if /$pat/ 1 4359 4360
665 }
666 76 return 0;
667 1 };
668 }
669  
670  
671 =back
672  
673 =head2 EXTENDING
674  
675 A step consists of a compile-time and a run-time component.
676  
677 During the creation of a C<File::Finder> object, step methods are
678 called as if they were methods against the slowly-growing
679 C<File::Finder> instance, including any additional parameters as in a
680 normal method call. The step is expected to return a coderef
681 (possibly a closure) to be executed at run-time.
682  
683 When a C<File::Finder> object is being evaluated as the C<File::Find>
684 C<wanted> routine, the collected coderefs are evaluated in sequence,
685 again as method calls against the C<File::Finder> object. No
686 additional parameters are passed. However, the normal C<wanted>
687 values are available, such as C<$_>, C<$File::Find::name>, and so on.
688 The C<_> pseudo-handle has been set properly, so you can safely
689 use C<-X> filetests and C<stat> against the pseudo-handle.
690 The routine is expected to return a true/false value, which becomes
691 the value of the step.
692  
693 Although a C<File::Finder> object is passed both to the compile-time
694 invocation and the resulting run-time invocation, only the C<options>
695 self-hash element is properly duplicated through the cloning process.
696 Do not be tempted to add additional self-hash elements without
697 overriding C<File::Finder>'s C<_clone>. Instead, pass values from the
698 compile-time phase to the run-time phase using closure variables, as
699 shown in the synopsis.
700  
701 For simplicity, you can also just mix-in your methods to the existing
702 C<File::Finder::Steps> class, rather than subclassing both classes as
703 shown above. However, this may result in conflicting implementations
704 of a given step name, so beware.
705  
706 =head1 SEE ALSO
707  
708 L<File::Finder>
709  
710 =head1 BUGS
711  
712 None known yet.
713  
714 =head1 AUTHOR
715  
716 Randal L. Schwartz, E<lt>merlyn@stonehenge.comE<gt>
717  
718 =head1 COPYRIGHT AND LICENSE
719  
720 Copyright (C) 2003,2004 by Randal L. Schwartz,
721 Stonehenge Consulting Services, Inc.
722  
723 This library is free software; you can redistribute it and/or modify
724 it under the same terms as Perl itself, either Perl version 5.8.2 or,
725 at your option, any later version of Perl 5 you may have available.
726  
727 =cut
728  
729 ## utility subroutines
730  
731 308 ▶sub _n {
Subroutine _n: called CC=3 SCAR=11.3
732 308 my ($prefix, $arg, $value) = @ _;
733 308 ▶ if ($prefix eq "+") {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix eq '+') { } 154 154 308
elsif ($prefix eq '-') { } 154 0 154
734 154 $value > $arg;
735 } elsif ($prefix eq "-") {
736 154 $value < $arg;
737 } else {
738 0 $value == $arg;
739 }
740 }
741  
742 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
743  
744 4 my %user_to_uid;
745 my %uid_to_user;
746  
747 1 ▶ my $initialize = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
748 1 ▶ while (my ($user, $pw, $uid) = getpwent) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = getpwent) 20 1 21
749 20 $user_to_uid{$user} = $uid;
750 20 $uid_to_user{$uid} = $user;
751 }
752 4 };
753  
754 0 ▶ sub _user_to_uid {
Subroutine _user_to_uid: not called CC=2 SCAR=17.9
755 0 my $user = shift;
756  
757 0 ▶ %user_to_uid or $initialize->();
Branch: 1 branch
BranchTrueFalseTotal
%user_to_uid or &$initialize() 0 0 0
758 0 $user_to_uid{$user};
759 }
760  
761 154 ▶ sub _uid_to_user {
Subroutine _uid_to_user: called CC=2 SCAR=6.9
762 154 my $uid = shift;
763  
764 154 ▶ %uid_to_user or $initialize->();
Branch: 1 branch
BranchTrueFalseTotal
%uid_to_user or &$initialize() 153 1 154
765 154 $uid_to_user{$uid};
766 }
767  
768 }
769  
770 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
771  
772 4 my %group_to_gid;
773 my %gid_to_group;
774  
775 1 ▶ my $initialize = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
776 1 ▶ while (my ($group, $pw, $gid) = getgrent) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = getgrent) 40 1 41
777 40 $group_to_gid{$group} = $gid;
778 40 $gid_to_group{$gid} = $group;
779 }
780 4 };
781  
782 0 ▶ sub _group_to_gid {
Subroutine _group_to_gid: not called CC=2 SCAR=17.9
783 0 my $group = shift;
784  
785 0 ▶ %group_to_gid or $initialize->();
Branch: 1 branch
BranchTrueFalseTotal
%group_to_gid or &$initialize() 0 0 0
786 0 $group_to_gid{$group};
787 }
788  
789 154 ▶ sub _gid_to_group {
Subroutine _gid_to_group: called CC=2 SCAR=6.9
790 154 my $gid = shift;
791  
792 154 ▶ %gid_to_group or $initialize->();
Branch: 1 branch
BranchTrueFalseTotal
%gid_to_group or &$initialize() 153 1 154
793 154 $gid_to_group{$gid};
794 }
795  
796 }
797  
798 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
799 ## from find2perl
800  
801 4 my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx);
802 4 my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
803  
804 0 ▶ sub _sizemm {
Subroutine _sizemm: not called CC=1 SCAR=6.9
805 0 my $rdev = shift;
806 0 sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff);
807 }
808  
809 0 ▶ sub _ls {
Subroutine _ls: not called CC=15 SCAR=54.8
810 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
811 $atime,$mtime,$ctime,$blksize,$blocks) = stat(_);
812 0 my $pname = $File::Find::name;
813  
814 0 ▶ $blocks
Branch: 1 branch
BranchTrueFalseTotal
$blocks or $blocks = int(($size + 1023) / 1024) 0 0 0
815 or $blocks = int(($size + 1023) / 1024);
816  
817 0 my $perms = $rwx[$mode & 7];
818 0 $mode >>= 3;
819 0 $perms = $rwx[$mode & 7] . $perms;
820 0 $mode >>= 3;
821 0 $perms = $rwx[$mode & 7] . $perms;
822 0 ▶ substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _;
Branch: 1 branch
BranchTrueFalseTotal
if -u _ 0 0 0
823 0 ▶ substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _;
Branch: 1 branch
BranchTrueFalseTotal
if -g _ 0 0 0
824 0 ▶ substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _;
Branch: 1 branch
BranchTrueFalseTotal
if -k _ 0 0 0
825 0 ▶ if (-f _) { $perms = '-' . $perms; }
Branch: 7 branches
BranchTrueFalseTotal
if (-f _) { } 0 0 0
elsif (-d _) { } 0 0 0
elsif (-l _) { } 0 0 0
elsif (-c _) { } 0 0 0
elsif (-b _) { } 0 0 0
elsif (-p _) { } 0 0 0
elsif (-S _) { } 0 0 0
826 0 elsif (-d _) { $perms = 'd' . $perms; }
827 0 elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($_); }
828 0 elsif (-c _) { $perms = 'c' . $perms; $size = _sizemm($rdev); }
829 0 elsif (-b _) { $perms = 'b' . $perms; $size = _sizemm($rdev); }
830 0 elsif (-p _) { $perms = 'p' . $perms; }
831 0 elsif (-S _) { $perms = 's' . $perms; }
832 0 else { $perms = '?' . $perms; }
833  
834 0 ▶ my $user = _uid_to_user($uid) || $uid;
Condition0%0/3
_uid_to_user($uid) || $uid
l!l&&r!l&&!r
000
MC/DC0%0/2
_uid_to_user($uid) || $uid
_uid_to_user($uid) $uid
Truth table0%0/3
A || B
A= _uid_to_user($uid)B= $uid
ABresult
1X1
011
000
835 0 ▶ my $group = _gid_to_group($gid) || $gid;
Condition0%0/3
_gid_to_group($gid) || $gid
l!l&&r!l&&!r
000
MC/DC0%0/2
_gid_to_group($gid) || $gid
_gid_to_group($gid) $gid
Truth table0%0/3
A || B
A= _gid_to_group($gid)B= $gid
ABresult
1X1
011
000
836  
837 0 my ($sec,$min,$hour,$mday,$mon,$timeyear) = localtime($mtime);
838 0 ▶ if (-M _ > 365.25 / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (-M _ > 182.625) { } 0 0 0
839 0 $timeyear += 1900;
840 } else {
841 0 $timeyear = sprintf("%02d:%02d", $hour, $min);
842 }
843  
844 0 printf "%5lu %4ld %-10s %3d %-8s %-8s %8s %s %2d %5s %s\n",
845 $ino, $blocks, $perms, $nlink, $user, $group, $size,
846 $moname[$mon], $mday, $timeyear, $pname;
847 0 1;
848 }
849 }
850  
851 4 1;
852 __END__
↑ Summary Finder.pm »