Fu/Dir.pm

filter: Statementstmt 94.7% 268 / 283 Branchbran 70.0% 77 / 110 Conditioncond 88.8% 16 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 96.8% 62 / 64 Podpod 100% 38 / 38 totaltotal 89.3% 469 / 525 CC 64 SCAR 13.2
CC
64
Cov
88%
map
20.0
__ANON__
10.0
parts
6.5
CRAP 3.7
« Fu.pm ↑ Summary Fu/Dir/Temp.pm »
1 package File::Fu::Dir;
2 13 $VERSION = v0.0.8;
3  
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use File::Path (); # for now
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use File::Fu::Dir::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use File::Fu::File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 File::Fu::Dir - a directoryname object
18  
19 =head1 SYNOPSIS
20  
21 use File::Fu;
22  
23 my $dir = File::Fu->dir("path/to/dir");
24 $dir->e and warn "$dir exists";
25  
26 $dir->l and warn "$dir is a link to ", $dir->readlink;
27  
28 foreach my $entry ($dir->list) {
29 warn $entry . ': ' . $entry->stat->size, "\n"
30 if($entry->f);
31 }
32  
33 =cut
34  
35 13 ▶use base 'File::Fu::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 13 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
38 13 '+' => 'file',
39 '/' => 'subdir',
40 13 );
41  
42 =head1 Constructor
43  
44 =head2 new
45  
46 my $dir = File::Fu::Dir->new($path);
47  
48 my $dir = File::Fu::Dir->new(@path);
49  
50 =cut
51  
52 216 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
53 216 my $package = shift;
54 216 ▶ my $class = ref($package) || $package;
Condition66%2/3
ref $package || $package
l!l&&r!l&&!r
541620
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table66%2/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
55 216 my $self = {$class->_init(@_)};
56 216 bless($self, $class);
57 216 return($self);
58 } # end subroutine new definition
59 ########################################################################
60  
61 =head1 Class Constants/Methods
62  
63 =head2 file_class
64  
65 Return the corresponding file class for this dir object. Default:
66 L<File::Fu::File>.
67  
68 my $fc = $class->file_class;
69  
70 =head2 is_dir
71  
72 Always true for a directory.
73  
74 =head2 is_file
75  
76 Always false for a directory.
77  
78 =cut
79  
80 13 ▶use constant top_class => 'File::Fu';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 13 ▶use constant file_class => 'File::Fu::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 13 ▶use constant token_class => 'File::Fu::Dir::Token';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 13 ▶use constant is_dir => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 13 ▶use constant is_file => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 ########################################################################
87  
88 =head2 temp_dir_class
89  
90 Class for L</temp_dir> objects. Default: L<File::Fu::Dir::Temp>.
91  
92 my $class = File::Fu::Dir->temp_dir_class;
93  
94 =cut
95  
96 3 ▶sub temp_dir_class {
Subroutine temp_dir_class: called CC=2 SCAR=7.0
Pod documented
97 3 my $package = shift;
98 3 my $class = ref($package) . '::Temp';
99 3 ▶ $class = __PACKAGE__ . '::Temp' unless($class->can('new'));
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 3 3
100 3 return($class);
101 } # end subroutine temp_dir_class definition
102 ########################################################################
103  
104 =head2 temp_file_class
105  
106 my $class = File::Fu::Dir->temp_file_class;
107  
108 =cut
109  
110 4 ▶sub temp_file_class {
Subroutine temp_file_class: called CC=2 SCAR=7.0
Pod documented
111 4 my $package = shift;
112 4 my $class = $package->file_class . '::Temp';
113 4 ▶ $class = __PACKAGE__->file_class.'::Temp' unless($class->can('new'));
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 4 4
114 4 return($class);
115 } # end subroutine temp_file_class definition
116 ########################################################################
117  
118 =for internal head2 _init
119 my %fields = $class->_init(@_);
120  
121 =cut
122  
123 390 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
124 390 my $class = shift;
125 390 ▶ @_ or return(dirs => ['.']);
Branch: 1 branch
BranchTrueFalseTotal
@_ or return "dirs", ["."] 289 101 390
126 my $dirs = [map({
127 289 ▶ $_ eq '' ? ('') : split(/\/+/, $_)
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 6 298 304
128 } @_)];
129 289 ▶ @$dirs or $dirs = ['']; # XXX
Branch: 1 branch
BranchTrueFalseTotal
@$dirs or $dirs = [""] 286 3 289
130 289 return(dirs => $dirs);
131 } # end subroutine _init definition
132 ########################################################################
133  
134 =head1 Methods
135  
136 =head2 stringify
137  
138 my $string = $dir->stringify;
139  
140 =cut
141  
142 639 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=7.0
Pod documented
143 639 my $self = shift;
144 #Carp::carp("stringify", overload::StrVal($self));
145 #defined($self->{dirs}) or croak("how did this happen?");
146 639 my @dirs = @{$self->{dirs}};
147 #warn "I'm (", join(',', @{$self->{dirs}}), ")";
148 639 ▶ @dirs or return('/');
Branch: 1 branch
BranchTrueFalseTotal
@dirs or return "/" 639 0 639
149 # TODO volume
150 639 join('/', @dirs, ''); # always a trailing slash
151 } # end subroutine stringify definition
152 ########################################################################
153  
154 =begin shutup_pod_cover
155  
156 =head2 l
157  
158 =end shutup_pod_cover
159  
160 =cut
161  
162 32 ▶*l = sub {-l shift->bare};
Subroutine __ANON__: called CC=1 SCAR=0.0
163  
164 =head2 bare
165  
166 Stringify without the trailing slash/assertion.
167  
168 my $str = $self->bare;
169  
170 The trailing slash causes trouble when trying to address a symlink to a
171 directory via a dir object. Thus, C<-l $dir> doesn't work, but
172 C<$dir-E<gt>l> does the same thing as C<-l $dir-E<gt>bare>.
173  
174 =cut
175  
176 149 ▶sub bare {
Subroutine bare: called CC=2 SCAR=7.0
Pod documented
177 149 my $self = shift;
178 149 my @dirs = @{$self->{dirs}};
179 149 ▶ @dirs or return('/');
Branch: 1 branch
BranchTrueFalseTotal
@dirs or return "/" 149 0 149
180 # TODO volume
181 149 join('/', @dirs); # always a trailing slash
182 } # end subroutine bare definition
183 ########################################################################
184  
185 =head2 file
186  
187 Create a filename object with $dir as its parent.
188  
189 my $file = $dir->file($filename);
190  
191 my $file = $dir + $filename;
192  
193 =cut
194  
195 196 ▶sub file {
Subroutine file: called CC=3 SCAR=11.0
Pod documented
196 196 my $self = shift;
197 196 my ($name, $rev) = @_;
198 196 ▶ $rev and croak("bah");
Branch: 1 branch
BranchTrueFalseTotal
$rev and croak("bah") 0 196 196
199  
200 # filename might have dir parts
201 196 ▶ if($name =~ m#/#) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/]) 1 195 196
202 1 my $bit = $self->file_class->new($name);
203 1 return $self->file_class->new_direct(
204 dir => $self->subdir($bit->dirname),
205 file => $bit->basename
206 );
207 }
208  
209 195 return($self->file_class->new_direct(dir => $self, file => $name));
210 } # end subroutine file definition
211 ########################################################################
212  
213 =head2 append
214  
215 Append a string only to the last directory part.
216  
217 $dir->append('.tmp');
218  
219 $dir %= "something";
220  
221 =cut
222  
223 2 ▶sub append {
Subroutine append: called CC=2 SCAR=7.0
Pod documented
224 2 my $self = shift;
225 2 my ($bit, $rev) = @_;
226  
227 2 ▶ $rev and return($bit . "$self"); # stringify is out-of-order
Branch: 1 branch
BranchTrueFalseTotal
$rev and return $bit . "$self" 0 2 2
228 #carp("appending $bit");
229 #$self = $self->clone;
230 2 $self->{dirs}[-1] .= $bit;
231 2 return($self);
232 } # end subroutine append definition
233 ########################################################################
234  
235 =head2 subdir
236  
237 $newdir = $dir->subdir('foo');
238  
239 $newdir = $dir / 'foo';
240  
241 =cut
242  
243 145 ▶sub subdir {
Subroutine subdir: called CC=3 SCAR=11.0
Pod documented
244 145 my $self = shift;
245 145 my ($name, $rev) = @_;
246 145 ▶ $rev and croak("bah");
Branch: 1 branch
BranchTrueFalseTotal
$rev and croak("bah") 0 145 145
247  
248 # appending to cwd means starting over
249 145 ▶ return($self->new($name)) if($self->is_cwd);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_cwd 4 141 145
250  
251 141 my %newbits = $self->_init($name);
252 141 $self = $self->clone;
253 141 push(@{$self->{dirs}}, @{$newbits{dirs}});
254 141 $self;
255 } # end subroutine subdir definition
256 ########################################################################
257  
258 =head2 part
259  
260 Returns the $i'th part of the directory list.
261  
262 my $part = $dir->part($i);
263  
264 $dir->part(-1) is like $dir->basename, but not an object and not quite
265 like File::Basename::basename() when it comes to the / directory.
266  
267 =cut
268  
269 7 ▶sub part {
Subroutine part: called CC=1 SCAR=0.0
Pod documented
270 7 my $self = shift;
271 7 my ($i) = @_;
272 7 return($self->{dirs}[$i]);
273 } # end subroutine part definition
274 ########################################################################
275  
276 =head2 end
277  
278 Shorthand for part(-1);
279  
280 =cut
281  
282 0 ▶sub end {shift->part(-1)};
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
283  
284 =head2 parts
285  
286 Retrieve the inner list of the directory's parts.
287  
288 my @parts = $dir->parts;
289  
290 my @parts = $dir->parts(0..2);
291  
292 The returned parts will be contiguous, but the request can be a
293 two-element list (and can also start or end at negative indices.)
294  
295 my @parts = $dir->parts(3, 7);
296  
297 my @parts = $dir->parts(3, -1);
298  
299 my @parts = $dir->parts(-5, -1);
300  
301 =cut
302  
303 5 ▶sub parts {
Subroutine parts: called CC=6 SCAR=18.7
Pod documented
304 5 my $self = shift;
305 5 my @want = @_;
306 0 ▶ @want or return(@{$self->{dirs}});
Branch: 1 branch
BranchTrueFalseTotal
@want or return @{$self->{"dirs"};} 5 0 5
307 5 ▶ if(@want == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@want == 2) 5 0 5
308 5 foreach my $end (@want) {
309 10 ▶ $end = $#{$self->{dirs}} + 1 + $end if($end < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 5 5 10
310 }
311 5 ▶ if($want[0] > $want[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want[0] > $want[1]) 0 5 5
312 0 croak("first endpoint '$want[0]' is after last '$want[1]'");
313 }
314 5 @want = $want[0]..$want[1];
315 }
316 # TODO else check contiguity?
317 5 return(@{$self->{dirs}}[@want]);
318 } # end subroutine parts definition
319 ########################################################################
320  
321 =head2 slice
322  
323 Returns a new dir object as the return of parts().
324  
325 my $slice = $dir->slice(0);
326  
327 my $slice = $dir->slice(0,3);
328  
329 =cut
330  
331 5 ▶sub slice {
Subroutine slice: called CC=1 SCAR=0.0
Pod documented
332 5 my $self = shift;
333 5 $self = $self->clone;
334 5 @{$self->{dirs}} = $self->parts(@_);
335 5 return($self);
336 } # end subroutine slice definition
337 ########################################################################
338  
339 =head2 map
340  
341 Execute a callback on each part of $dir. The sub should modify $_ (yes,
342 this is slightly unlike the map() builtin.)
343  
344 If $parts is defined as an integer or array reference of integers, it
345 will be treated as a slice on the directory parts to which the map
346 should be applied.
347  
348 $dir->map(sub {...}, [@parts]);
349  
350 $dir &= sub {s/foo$/bar/};
351  
352 So, to modify only the first directory part:
353  
354 $dir->map(sub {s/foo$/bar/}, 0);
355  
356 =cut
357  
358 0 ▶sub map :method {
Subroutine map: not called CC=4 SCAR=30.0
Pod documented
359 0 my $self = shift;
360 0 my ($sub, $parts) = @_;
361 my @parts = defined($parts) ? (ref($parts) ? @$parts : $parts) :
362 0 ▶ 0..($#{$self->{dirs}});
Branch: 2 branches
BranchTrueFalseTotal
defined $parts ? : 0 0 0
ref $parts ? : 0 0 0
363 # TODO actually use the parts() code for this
364 # warn "@parts";
365 0 foreach my $dir (@{$self->{dirs}}[@parts]) {
366 0 local $_ = $dir;
367 0 $sub->();
368 0 $dir = $_;
369 }
370 0 $self;
371 } # end subroutine map definition
372 ########################################################################
373  
374 =head1 Properties
375  
376 =head2 is_cwd
377  
378 True if the $dir represents a relative (e.g. '.') directory.
379  
380 my $bool = $dir->is_cwd;
381  
382 =cut
383  
384 649 ▶sub is_cwd {
Subroutine is_cwd: called CC=2 SCAR=6.9
Pod documented
385 649 my $self = shift;
386  
387 649 my @dirs = @{$self->{dirs}};
388 649 ▶ return(@dirs == 1 and $dirs[0] eq '.');
Condition100%3/3
@dirs == 1 && $dirs[0] eq '.'
!ll&&!rl&&r
194332123
MC/DC100%2/2
@dirs == 1 && $dirs[0] eq '.'
@dirs == 1 $dirs[0] eq '.'
Truth table100%3/3
A && B
A= @dirs == 1B= $dirs[0] eq '.'
ABresult
0X0
100
111
389 } # end subroutine is_cwd definition
390 ########################################################################
391  
392 =for note
393 dirname('.') and basename('.') are both '.' -- also true for '/'
394  
395 =head2 basename
396  
397 Returns the last part of the path as a Dir object.
398  
399 my $bit = $dir->basename;
400  
401 =cut
402  
403 12 ▶sub basename {
Subroutine basename: called CC=1 SCAR=0.0
Pod documented
404 12 my $self = shift;
405 12 return($self->new($self->{dirs}[-1]));
406 } # end subroutine basename definition
407 ########################################################################
408  
409 =head2 dirname
410  
411 Returns the parent parts of the path as a Dir object.
412  
413 my $parent = $dir->dirname;
414  
415 =cut
416  
417 13 ▶sub dirname {
Subroutine dirname: called CC=4 SCAR=13.9
Pod documented
418 13 my $self = shift;
419 13 $self = $self->clone;
420 13 my $dirs = $self->{dirs};
421 13 ▶ if(@$dirs == 1 and $dirs->[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$dirs == 1 and $dirs->[0] eq "") 1 12 13
Condition100%3/3
@$dirs == 1 and $dirs->[0] eq ""
!ll&&!rl&&r
841
MC/DC100%2/2
@$dirs == 1 and $dirs->[0] eq ""
@$dirs == 1 $dirs->[0] eq ""
Truth table100%3/3
A and B
A= @$dirs == 1B= $dirs->[0] eq ""
ABresult
0X0
100
111
422 1 return($self->new('/'));
423 }
424 12 pop(@$dirs);
425 12 ▶ @$dirs or return($self->new);
Branch: 1 branch
BranchTrueFalseTotal
@$dirs or return $self->new 8 4 12
426 8 return($self);
427 } # end subroutine dirname definition
428 ########################################################################
429  
430 =head2 absolute
431  
432 Get an absolute name (without checking the filesystem.)
433  
434 my $abs = $dir->absolute;
435  
436 =cut
437  
438 2 ▶sub absolute {
Subroutine absolute: called CC=2 SCAR=7.1
Pod documented
439 2 my $self = shift;
440 2 ▶ return $self if $self->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_absolute 0 2 2
441 2 return $self->new(File::Spec->rel2abs($self->stringify));
442 } # end subroutine absolute definition
443 ########################################################################
444  
445 =head2 absolutely
446  
447 Get an absolute path (resolved on filesystem, so it must exist.)
448  
449 my $abs = $dir->absolutely;
450  
451 =cut
452  
453 13 ▶sub absolutely {
Subroutine absolutely: called CC=2 SCAR=7.0
Pod documented
454 13 my $self = shift;
455 13 my $res = Cwd::abs_path($self->stringify);
456 13 ▶ defined($res) or croak("$self absolutely() not found");
Branch: 1 branch
BranchTrueFalseTotal
defined $res or croak("$self absolutely() not found") 13 0 13
457 13 return $self->new($res);
458 } # end subroutine absolutely definition
459 ########################################################################
460  
461 =head1 Doing stuff
462  
463 =head2 open
464  
465 Calls opendir(), but throws an error if it fails.
466  
467 my $dh = $dir->open;
468  
469 Returns a directory handle, for e.g. readdir().
470  
471 my @files = map({$dir + $_} grep({$_ !~ m/^\./} readdir($dh)));
472  
473 =cut
474  
475 15 ▶sub open :method {
Subroutine open: called CC=2 SCAR=7.1
Pod documented
476 15 my $self = shift;
477  
478 15 ▶ opendir(my $dh, "$self") or die "cannot opendir '$self' $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "$self" or die "cannot opendir '${self}' $!" 15 0 15
479 15 return($dh);
480 } # end subroutine open definition
481 ########################################################################
482  
483 =head2 touch
484  
485 Update the timestamp of a directory (croak if it doesn't exist.)
486  
487 $dir->touch;
488  
489 =cut
490  
491 1 ▶sub touch {
Subroutine touch: called CC=1 SCAR=0.0
Pod documented
492 1 my $self = shift;
493 1 $self->utime(time);
494 } # end subroutine touch definition
495 ########################################################################
496  
497 =head2 list
498  
499 my @paths = $dir->list(all => 1);
500  
501 =cut
502  
503 3 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
504 3 my $self = shift;
505  
506 3 ▶ map({my $d = $self/$_; -d $d ? $d : $self+$_} $self->contents(@_));
Branch: 1 branch
BranchTrueFalseTotal
-d $d ? : 2 7 9
507 } # end subroutine list definition
508 ########################################################################
509  
510 =head2 lister
511  
512 my $subref = $dir->lister(all => 1);
513  
514 =cut
515  
516 12 ▶sub lister {
Subroutine lister: called CC=1 SCAR=0.0
Pod documented
517 12 my $self = shift;
518 12 my $csub = $self->iterate_contents(@_);
519 116 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
520 116 ▶ $csub or return();
Branch: 1 branch
BranchTrueFalseTotal
$csub or return () 116 0 116
521 116 ▶ while(defined(my $n = $csub->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = &$csub())) 104 12 116
522 104 my $d = $self/$n;
523 104 ▶ return(-d $d->bare ? $d : $self+$n)
Branch: 1 branch
BranchTrueFalseTotal
-d $d->bare ? : 11 93 104
524 }
525 12 $csub = undef;
526 12 return();
527 12 };
528 12 return($sub);
529 } # end subroutine lister definition
530 ########################################################################
531  
532 =head2 contents
533  
534 Equivelant to readdir. With the 'all' option true, returns hidden names
535 too (but not the '.' and '..' entries.)
536  
537 The return values are strings, not File::Fu objects.
538  
539 my @names = $dir->contents(all => 1);
540  
541 =cut
542  
543 3 ▶sub contents {
Subroutine contents: called CC=3 SCAR=11.4
Pod documented
544 3 my $self = shift;
545 3 ▶ (@_ % 2) and croak('odd number of items in options hash');
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("odd number of items in options hash") 0 3 3
546 3 my %opts = @_;
547 3 my $dh = $self->open;
548 # XXX needs more cross-platformness
549 0 ▶ $opts{all} and return(grep({$_ !~ m/^\.{1,2}$/} readdir($dh)));
Branch: 1 branch
BranchTrueFalseTotal
$opts{"all"} and return grep({not $_ =~ /^\.{1,2}$/;} readdir $dh) 0 3 3
550 3 return(grep({$_ !~ m/^\./} readdir($dh)));
551 } # end subroutine contents definition
552 ########################################################################
553  
554 =head2 iterate_contents
555  
556 Returns a subref which will iterate over the directory's contents.
557  
558 my $subref = $dir->iterate_contents(all => 1);
559  
560 =cut
561  
562 12 ▶sub iterate_contents {
Subroutine iterate_contents: called CC=2 SCAR=6.9
Pod documented
563 12 my $self = shift;
564 12 ▶ (@_ % 2) and croak('odd number of items in options hash');
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("odd number of items in options hash") 0 12 12
565 12 my %opts = @_;
566 12 my $all = $opts{all};
567 12 my $dh = $self->open;
568 # XXX needs more cross-platformness
569 116 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
570 116 ▶ $dh or return();
Branch: 1 branch
BranchTrueFalseTotal
$dh or return () 116 0 116
571 116 ▶ while(defined(my $n = readdir($dh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = readdir $dh)) 128 12 140
572 128 ▶ if($all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all) { } 122 6 128
573 122 ▶ return($n) unless($n =~ m/^\.{1,2}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /^\.{1,2}$/ 100 22 122
574 }
575 else {
576 6 ▶ return($n) unless($n =~ m/^\./);
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /^\./ 4 2 6
577 }
578 }
579 12 $dh = undef;
580 12 return();
581 12 };
582 } # end subroutine iterate_contents definition
583 ########################################################################
584  
585 =head2 find
586  
587 Recursively search a directory's contents for items where the supplied
588 coderef (matcher) returns true. The matcher will be invoked with the
589 topic (C<$_>) set to the current path (which is either a Dir or File
590 object.) The return values will be File::Fu::File or File::Fu::Dir
591 objects.
592  
593 If your matcher returns true, the topic will be added to the return
594 values.
595  
596 my @paths = $dir->find(sub {m/foo/});
597  
598 There is a knob for controlling recursion, which is the first argument
599 to your matcher.
600  
601 my @pm_files = $dir->find(sub {
602 return shift->prune
603 if($_->is_dir and $_->part(-1) =~ m/^\.svn$/);
604 $_->is_file and m/\.pm$/;
605 });
606  
607 =over
608  
609 =item Differences from File::Find::find()
610  
611 The invocant (C<$dir> aka '.') is not examined (because this is an
612 object method, there is always only one starting path.)
613  
614 The topic is always absolute in the same sense as the invocant. That
615 is, if C<$dir> is relative to your current directory, then so are the
616 topics and return values. If C<$dir> is absolute, so are the topics and
617 return values.
618  
619 =back
620  
621 =cut
622  
623 3 ▶sub find {
Subroutine find: called CC=3 SCAR=11.0
Pod documented
624 3 my $self = shift;
625  
626 3 my @return;
627 3 my $finder = $self->finder(@_);
628 3 ▶ while(defined(my $ans = $finder->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ans = &$finder())) 12 3 15
629 12 ▶ $ans or next;
Branch: 1 branch
BranchTrueFalseTotal
$ans or next 12 0 12
630 12 push(@return, $ans);
631 }
632 3 return(@return);
633 } # end subroutine find definition
634 ########################################################################
635  
636 =head2 finder
637  
638 Returns an iterator for finding files. This iterator does everything
639 that find() does, but returns one path at a time. Returns undef when
640 exhausted and zero when it is just taking a break.
641  
642 my $subref = $dir->finder(sub {$_->is_file and $_->file =~ m/foo/});
643  
644 This allows a non-blocking find.
645  
646 while(defined(my $path = $subref->())) {
647 $path or next; # 0 means 'not done yet'
648 # do something with $path (a file or dir object)
649 }
650  
651 The find() method is implemented in terms of finder() by simply using a
652 while() loop and accumulating the return values.
653  
654 =cut
655  
656 4 ▶sub finder {
Subroutine finder: called CC=1 SCAR=0.0
Pod documented
657 4 my $self = shift;
658 4 my ($matcher, @opt) = @_; # TODO support options e.g. loops
659  
660 4 my %opt = (all => 1);
661  
662 4 my $reader;
663 my @stack;
664 25 ▶ my $it = sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
665 25 my $loops = 0;
666 FIND: {
667 25 ▶ $reader ||= $self->lister(all => $opt{all});
Condition66%2/3
$reader ||= $self->lister("all", $opt{"all"})
l!l&&r!l&&!r
100110
MC/DC0%0/2
$reader ||= $self->lister("all", $opt{"all"})
$reader $self->lister("all", $opt{"all"})
Truth table66%2/3
A ||= B
A= $readerB= $self->lister("all", $opt{"all"})
ABresult
1X1
011
000
668 111 $loops++;
669 111 ▶ if(defined(my $path = $reader->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $path = &$reader())) { } 100 11 111
670 100 ▶ if($path->is_dir and not $path->l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->is_dir and not $path->l) 8 92 100
Condition100%3/3
$path->is_dir and not $path->l
!ll&&!rl&&r
9028
MC/DC100%2/2
$path->is_dir and not $path->l
$path->is_dir not $path->l
Truth table100%3/3
A and not(B)
A= $path->is_dirB= not $path->l
ABresult
0X0
100
111
671 8 push(@stack, [$self, $reader]);
672 8 ($self, $reader) = ($path, undef);
673 }
674 100 local $_ = $path;
675 100 my $ok = $matcher->(my $knob = File::Fu::Dir::FindKnob->new);
676 100 ▶ if($knob->pruned and not $path->l) { # XXX nofollow assumption
Branch: 1 branch
BranchTrueFalseTotal
if ($knob->pruned and not $path->l) 1 99 100
Condition100%3/3
$knob->pruned and not $path->l
!ll&&!rl&&r
9811
MC/DC100%2/2
$knob->pruned and not $path->l
$knob->pruned not $path->l
Truth table100%3/3
A and not(B)
A= $knob->prunedB= not $path->l
ABresult
0X0
100
111
677 1 ($self, $reader) = @{pop(@stack)};
678 }
679 100 ▶ if($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 21 79 100
680 21 return($path);
681 }
682 79 ▶ redo FIND if($loops < 50);
Branch: 1 branch
BranchTrueFalseTotal
if $loops < 50 79 0 79
683 0 return(0); # no match, but continue
684 }
685 else {
686 11 ▶ @stack or return();
Branch: 1 branch
BranchTrueFalseTotal
@stack or return () 7 4 11
687 7 ($self, $reader) = @{pop(@stack)};
688 7 redo FIND;
689 }
690 }
691 4 };
692 4 return($it);
693 } # end subroutine finder definition
694 ########################################################################
695  
696 =head2 The FindKnob object
697  
698 The FindKnob object allows you to control the next steps of find().
699 Methods called on it will typically return a value which also makes
700 sense as a return value of your matcher sub. Thus the idiom:
701  
702 $dir->find(sub {return shift->prune if(condition); ...})
703  
704 =over
705  
706 =item prune
707  
708 Do not recurse into the topic directory. Returns false.
709  
710 =back
711  
712 =cut
713  
714 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
715 package File::Fu::Dir::FindKnob;
716 13 ▶use Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
717 13 with 'new';
718 13 ri 'pruned';
719 13 ▶no Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
720 2 ▶sub prune {shift->set_pruned(1); 0}
Subroutine prune: called CC=1 SCAR=0.0
721 } # File::Fu::Dir::FindKnob
722 ########################################################################
723  
724 =head2 mkdir
725  
726 Create the directory or croak with an error.
727  
728 $dir->mkdir;
729  
730 $dir->mkdir(0700);
731  
732 =cut
733  
734 15 ▶sub mkdir :method {
Subroutine mkdir: called CC=4 SCAR=13.9
Pod documented
735 15 my $self = shift;
736 15 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 14 15
737 1 my $mode = shift(@_);
738 1 ▶ mkdir($self, $mode) or croak("cannot mkdir('$self', $mode) $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::mkdir $self, $mode or croak("cannot mkdir('${self}', $mode) $!") 1 0 1
739 }
740 else {
741 14 ▶ mkdir($self) or croak("cannot mkdir('$self') $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::mkdir $self or croak("cannot mkdir('${self}') $!") 13 1 14
742 }
743 14 return($self);
744 } # end subroutine mkdir definition
745 ########################################################################
746  
747 =head2 create
748  
749 Create the directory, with parents if needed.
750  
751 $dir->create;
752  
753 =cut
754  
755 1 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
756 1 my $self = shift;
757 # TODO pass mode, but the verbose parameter is silly (should have been
758 # a callback or something -- so we'll end up reimplementing mkpath?)
759 1 File::Path::mkpath("$self");
760 1 return($self);
761 } # end subroutine create definition
762 ########################################################################
763  
764 =head2 rmdir
765  
766 Remove the directory or croak with an error.
767  
768 $dir->rmdir;
769  
770 =cut
771  
772 9 ▶sub rmdir :method {
Subroutine rmdir: called CC=2 SCAR=6.9
Pod documented
773 9 my $self = shift;
774 9 ▶ rmdir($self) or croak("cannot rmdir('$self') $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::rmdir $self or croak("cannot rmdir('${self}') $!") 8 1 9
775 } # end subroutine rmdir definition
776 ########################################################################
777  
778 =head2 remove
779  
780 Remove the directory and all of its children.
781  
782 $dir->remove;
783  
784 =cut
785  
786 8 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
787 8 my $self = shift;
788 8 my $dir = $self->stringify;
789 8 File::Path::rmtree($dir);
790 8 ▶ -e $dir and croak("rmtree failed"); # XXX rmtree is buggy
Branch: 1 branch
BranchTrueFalseTotal
-e $dir and croak("rmtree failed") 0 8 8
791 } # end subroutine remove definition
792 ########################################################################
793  
794 =head2 unlink
795  
796 $link->unlink;
797  
798 =cut
799  
800 8 ▶sub unlink :method {
Subroutine unlink: called CC=3 SCAR=11.7
Pod documented
801 8 my $self = shift;
802 8 ▶ $self->l or croak("not a link");
Branch: 1 branch
BranchTrueFalseTotal
$self->l or croak("not a link") 8 0 8
803 8 ▶ unlink($self->bare) or croak("unlink '$self' failed $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::unlink $self->bare or croak("unlink '${self}' failed $!") 8 0 8
804 } # end subroutine unlink definition
805 ########################################################################
806  
807 =head2 symlink
808  
809 Create a symlink which points to $dir.
810  
811 my $link = $dir->symlink($linkname);
812  
813 Note that symlinks are relative to where they live, so if $dir is a
814 relative path, it must be relative to $linkname.
815  
816 =cut
817  
818 9 ▶sub symlink :method {
Subroutine symlink: called CC=2 SCAR=7.0
Pod documented
819 9 my $self = shift;
820 9 my ($name) = @_;
821  
822 9 $name =~ s#/$##; # stringify and strip
823 9 ▶ symlink($self, $name) or
Branch: 1 branch
BranchTrueFalseTotal
CORE::symlink $self, $name or croak("symlink '${self}' to '${name}' failed $!") 9 0 9
824 croak("symlink '$self' to '$name' failed $!");
825 9 return($self->new($name));
826 } # end subroutine symlink definition
827 ########################################################################
828  
829 =head2 readlink
830  
831 my $to = $file->readlink;
832  
833 =cut
834  
835 1 ▶sub readlink :method {
Subroutine readlink: called CC=2 SCAR=7.0
Pod documented
836 1 my $self = shift;
837 1 my $name = readlink($self->bare);
838 1 ▶ defined($name) or croak("cannot readlink '$self' $!");
Branch: 1 branch
BranchTrueFalseTotal
defined $name or croak("cannot readlink '${self}' $!") 1 0 1
839 1 return($self->new($name));
840 } # end subroutine readlink definition
841 ########################################################################
842  
843 =head1 Changing Directories
844  
845  
846 =head2 chdir
847  
848 Change to the directory in self, returning a new '.' directory object.
849  
850 $dir = $dir->chdir;
851  
852 =cut
853  
854 4 ▶sub chdir :method {
Subroutine chdir: called CC=2 SCAR=7.1
Pod documented
855 4 my $self = shift;
856 4 ▶ chdir($self) or croak("cannot chdir '$self' $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::chdir $self or croak("cannot chdir '${self}' $!") 4 0 4
857 # should return a new '.' object ?
858 4 return($self->new('.'));
859 } # end subroutine chdir definition
860 ########################################################################
861  
862 =head2 chdir_for
863  
864 Change to $dir and run the given subroutine. The sub will be passed a
865 './' directory object.
866  
867 $dir->chdir_for(sub {...});
868  
869 =cut
870  
871 1 ▶sub chdir_for {
Subroutine chdir_for: called CC=1 SCAR=0.0
Pod documented
872 1 my $self = shift;
873 1 my ($sub) = @_;
874 # we need to guarantee that we return, so we must implement the scoped
875 # version in order to implement the wrapper.
876 1 my $dot = $self->chdir_local;
877 # XXX bah. the $token binds weirdly in 5.6.2
878 1 return $sub->($self->new('.'));
879 } # end subroutine chdir_for definition
880 ########################################################################
881  
882 =head2 chdir_local
883  
884 Change to $dir, but return to the current cwd when $token goes out of
885 scope.
886  
887 my $token = $self->chdir_local;
888  
889 =cut
890  
891 1 ▶sub chdir_local {
Subroutine chdir_local: called CC=1 SCAR=0.0
Pod documented
892 1 my $self = shift;
893 1 my $now = $self->top_class->cwd;
894 1 $self->chdir;
895 1 return $self->token_class->new->return_to($now);
896 } # end subroutine chdir_local definition
897 ########################################################################
898 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
899 package File::Fu::Dir::Token;
900 13 our @ISA = qw('File::Fu::Dir);
901 1 ▶sub return_to {
Subroutine return_to: called CC=2 SCAR=7.1
902 1 my $self = shift(@_);
903 1 ▶ $self->{return_to} = shift(@_) or croak("invalid usage");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"return_to"} = shift @_ or croak("invalid usage") 1 0 1
904 1 return($self);
905 }
906 1 ▶sub DESTROY { my $ret = shift->{return_to} or return; $ret->chdir; }
Subroutine DESTROY: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $ret = +(shift())->{"return_to"} or return 1 0 1
907 }
908 ########################################################################
909  
910 =head1 Temporary Directories and Files
911  
912 These methods use the $dir object as a parent location for the temp
913 path. To use your system's global temp space (e.g. '/tmp/'), just
914 replace $dir with 'File::Fu'.
915  
916 File::Fu->temp_dir; # '/tmp/'
917 File::Fu->dir->temp_dir; # './'
918 File::Fu->dir("foo")->temp_dir; # 'foo/'
919  
920 File::Fu->temp_file; # '/tmp/'
921 File::Fu->dir->temp_file; # './'
922 File::Fu->dir("foo")->temp_file; # 'foo/'
923  
924 =head2 temp_dir
925  
926 Return a temporary directory in $dir.
927  
928 my $dir = $dir->temp_dir;
929  
930 =cut
931  
932 3 ▶sub temp_dir {
Subroutine temp_dir: called CC=1 SCAR=0.0
Pod documented
933 3 my $self = shift;
934 3 $self->temp_dir_class->new($self, @_);
935 } # end subroutine temp_dir definition
936 ########################################################################
937  
938 =head2 temp_file
939  
940 Return a filehandle to a temporary file in $dir.
941  
942 my $handle = $dir->temp_file;
943  
944 =cut
945  
946 4 ▶sub temp_file {
Subroutine temp_file: called CC=1 SCAR=0.0
Pod documented
947 4 my $self = shift;
948 4 $self->temp_file_class->new($self, @_);
949 } # end subroutine temp_file definition
950 ########################################################################
951  
952 =head1 AUTHOR
953  
954 Eric Wilhelm @ <ewilhelm at cpan dot org>
955  
956 http://scratchcomputing.com/
957  
958 =head1 BUGS
959  
960 If you found this module on CPAN, please report any bugs or feature
961 requests through the web interface at L<http://rt.cpan.org>. I will be
962 notified, and then you'll automatically be notified of progress on your
963 bug as I make changes.
964  
965 If you pulled this development version from my /svn/, please contact me
966 directly.
967  
968 =head1 COPYRIGHT
969  
970 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
971  
972 =head1 NO WARRANTY
973  
974 Absolutely, positively NO WARRANTY, neither express or implied, is
975 offered with this software. You use this software at your own risk. In
976 case of loss, no person or entity owes you anything whatsoever. You
977 have been warned.
978  
979 =head1 LICENSE
980  
981 This program is free software; you can redistribute it and/or modify it
982 under the same terms as Perl itself.
983  
984 =cut
985  
986 13 require File::Fu;
987 # vi:ts=2:sw=2:et:sta
988 13 1;
« Fu.pm ↑ Summary Fu/Dir/Temp.pm »