Easy/Dir.pm

filter: Statementstmt 74.7% 77 / 103 Branchbran 62.5% 30 / 48 Conditioncond 60.0% 15 / 25 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 70.5% 12 / 17 Podpod 100% 10 / 10 totaltotal 68.6% 149 / 217 CC 39 SCAR 22.2
CC
39
Cov
69%
__ANON__
20.0
scan_tree
14.0
home
12.0
CRAP 9.2
« Easy/File.pm ↑ Summary Easy.pm »
1 package IO::Easy::Dir;
2  
3 7 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use IO::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use base qw(IO::Easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 my $FS = 'File::Spec';
10  
11 7 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶sub current {
Subroutine current: called CC=1 SCAR=0.0
Pod documented
14 2 my $pack = shift;
15 2 return $pack->new (Cwd::cwd());
16 }
17  
18 0 ▶sub home {
Subroutine home: not called CC=3 SCAR=24.8
Pod documented
19 0 my $pack = shift;
20 return $pack->new (
21 0 ▶ $ENV{USERPROFILE} || $ENV{HOME} || (getpwuid($<)) [7]
Condition0%0/6
$ENV{'USERPROFILE'} || $ENV{'HOME'} || (getpwuid $<)[7]
l!l&&r!l&&!r
000
$ENV{"USERPROFILE"} or $ENV{"HOME"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'USERPROFILE'} || $ENV{'HOME'} || (getpwuid $<)[7]
$ENV{"USERPROFILE"} $ENV{"HOME"} (getpwuid $<)[7]
Truth table0%0/4
A or B || C
A= $ENV{"USERPROFILE"}B= $ENV{"HOME"}C= (getpwuid $<)[7]
ABCresult
1XX1
01X1
0011
0000
22 );
23 }
24  
25 6 ▶sub create {
Subroutine create: called CC=4 SCAR=14.0
Pod documented
26 6 my $self = shift;
27 6 my @path = @_;
28
29 6 my $path = $self->{path};
30 6 ▶ if (scalar @path) { # create @path into received directory
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path) 0 6 6
31 0 $path = File::Spec->catdir ($path, @path);
32 }
33
34 6 my @dirs = File::Spec->splitdir ($path);
35
36 6 foreach my $depth (0 .. scalar @dirs - 1) {
37 18 my $dir = File::Spec->join(map {$dirs[$_]} 0..$depth);
38 18 ▶ mkdir $dir
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 7 11 18
39 unless -d $dir;
40 }
41  
42 }
43  
44 6 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
45 6 return 'dir';
46 }
47  
48 8 ▶sub items {
Subroutine items: called CC=10 SCAR=24.4
Pod documented
49 8 my $self = shift;
50 8 ▶ my $filter = shift || '';
Condition50%1/2
shift() || ''
l!l
08
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
51 8 ▶ my $is_regexp = shift || 0;
Condition50%1/2
shift() || 0
l!l
08
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
52
53 8 my $path = $self->{path};
54
55 8 ▶ unless ($is_regexp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_regexp) 8 0 8
56 8 $filter = join '', '\.', $filter, '$';
57 }
58
59 8 ▶ opendir (DH, $path) || die "can't open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DH, $path or die "can't open ${path}: $!" 8 0 8
60 8 my @files = ();
61 8 foreach my $file_name (readdir (DH)) {
62 24 ▶ next if $file_name =~ /^\.+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /^\.+$/ 16 8 24
63
64 8 ▶ next if $filter ne '\.$' and $file_name !~ /$filter/i;
Branch: 1 branch
BranchTrueFalseTotal
if $filter ne "\\.\$" and not $file_name =~ /$filter/i 0 8 8
Condition33%1/3
$filter ne "\\.\$" and not $file_name =~ /$filter/i
!ll&&!rl&&r
800
MC/DC0%0/2
$filter ne "\\.\$" and not $file_name =~ /$filter/i
$filter ne "\\.\$" not $file_name =~ /$filter/i
Truth table33%1/3
A and not(B)
A= $filter ne "\\.\$"B= not $file_name =~ /$filter/i
ABresult
0X0
100
111
65
66 8 my $file = $self->append ($file_name);
67
68 8 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 8 8
69
70 8 push @files, $file->attach_interface;
71 }
72 8 closedir (DH);
73
74 8 return @files;
75 }
76  
77 7 ▶sub rm_tree {
Subroutine rm_tree: called CC=4 SCAR=13.9
Pod documented
78 7 my $self = shift;
79
80 7 my @files = $self->items;
81 7 foreach my $file (@files) {
82 5 my $path = $file->{path};
83 5 ▶ unlink $path
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 3 2 5
84 if -f $path;
85 5 ▶ $file->rm_tree
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 2 3 5
86 if -d $path;
87 }
88
89 7 rmdir $self->{path};
90 }
91  
92 10 ▶sub scan_tree {
Subroutine scan_tree: called CC=14 SCAR=26.4
Pod documented
93 10 my $self = shift;
94 10 my $handler = shift;
95
96 10 my $flag = '';
97
98 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 8 10
99 2 ($handler, $flag) = (shift, $handler);
100 }
101
102 10 my $path = $self->{path};
103
104 10 ▶ opendir (DH, $path) || die "can't open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DH, $path or die "can't open ${path}: $!" 10 0 10
105
106 10 my @files;
107
108 10 foreach my $file_name (readdir (DH)) {
109 35 ▶ next if $file_name eq $FS->curdir or $file_name eq $FS->updir; # omit . ..
Branch: 1 branch
BranchTrueFalseTotal
if $file_name eq $FS->curdir or $file_name eq $FS->updir 20 15 35
Condition100%3/3
$file_name eq $FS->curdir or $file_name eq $FS->updir
l!l&&r!l&&!r
101015
MC/DC100%2/2
$file_name eq $FS->curdir or $file_name eq $FS->updir
$file_name eq $FS->curdir $file_name eq $FS->updir
Truth table100%3/3
A or B
A= $file_name eq $FS->curdirB= $file_name eq $FS->updir
ABresult
1X1
011
000
110
111 15 my $file = $self->append ($file_name)->attach_interface;
112
113 15 my $return = 1;
114 15 ▶ $return = &$handler ($file)
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "for_files_only" and -f $file or $flag ne "for_files_only" 13 2 15
Condition100%6/6
$flag eq "for_files_only" and -f $file or $flag ne "for_files_only"
l!l&&r!l&&!r
1122
$flag eq "for_files_only" and -f $file
!ll&&!rl&&r
1221
MC/DC33%1/3
$flag eq "for_files_only" and -f $file or $flag ne "for_files_only"
$flag eq "for_files_only" -f $file $flag ne "for_files_only"
Truth table60%3/5
A and B or C
A= $flag eq "for_files_only"B= -f $fileC= $flag ne "for_files_only"
ABCresult
11X1
0X11
1011
0X00
1000
115 if ($flag eq 'for_files_only' && -f $file) || $flag ne 'for_files_only';
116
117 15 ▶ push @files, $file
Branch: 1 branch
BranchTrueFalseTotal
if $return or $flag eq "ignoring_return" 11 4 15
Condition100%3/3
$return or $flag eq "ignoring_return"
l!l&&r!l&&!r
834
MC/DC100%2/2
$return or $flag eq "ignoring_return"
$return $flag eq "ignoring_return"
Truth table100%3/3
A or B
A= $returnB= $flag eq "ignoring_return"
ABresult
1X1
011
000
118 if $return || $flag eq 'ignoring_return';
119
120 }
121 10 closedir (DH);
122
123 10 foreach my $file (@files) {
124 11 ▶ if ($file->type eq 'dir') {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->type eq 'dir') { } 6 5 11
elsif ($file->type eq 'file') { } 5 0 5
125 6 $file->scan_tree ($handler);
126 } elsif ($file->type eq 'file') {
127
128 }
129 }
130 }
131  
132 0 ▶sub copy_children {
Subroutine copy_children: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self = shift;
134 0 my $target = shift;
135 0 my $handler = shift;
136
137 0 ▶ $self->scan_tree (sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
138 0 my $file = shift;
139
140 0 my $path = $file->rel_path ($self->{path});
141
142 0 ▶ if (ref $handler eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 0 0 0
143 0 ▶ next unless &$handler ($file);
Branch: 1 branch
BranchTrueFalseTotal
unless &$handler($file) 0 0 0
144 }
145
146 0 ▶ if ($file->type eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->type eq "dir") 0 0 0
147 0 $target->create ($path);
148 0 return 1;
149 }
150
151 0 $target->append ($path)->as_file->store (
152 $file->contents
153 );
154 0 });
155 }
156  
157 0 ▶sub copy_node {
Subroutine copy_node: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159 0 my $target = shift;
160
161 0 $target->create ($self->name);
162
163 0 ▶ $self->scan_tree (sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
164 0 my $file = shift;
165
166 0 my $path = $file->rel_path ($self->up);
167
168 0 ▶ if ($file->type eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->type eq "dir") 0 0 0
169 0 $target->create ($path);
170 0 return 1;
171 }
172
173 0 $target->append ($path)->as_file->store (
174 $file->contents
175 );
176 0 });
177 }
178  
179 3 ▶sub touch {
Subroutine touch: called CC=5 SCAR=16.8
Pod documented
180 3 my $self = shift;
181
182 3 ▶ if(-e $self->{path})
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{'path'}) { } 2 1 3
183 {
184 2 ▶ if(-d _)
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) { } 2 0 2
185 {
186 2 my $t = time;
187
188 die "can't utime $self->{path}: $!"
189 2 ▶ unless utime $t, $t, $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless utime $t, $t, $self->{"path"} 0 2 2
190 }
191 else
192 {
193 0 warn "not a dir: $self->{path}\n";
194 }
195 }
196 else
197 {
198 die "can't create $self->{path}: $!"
199 1 ▶ unless mkdir $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless mkdir $self->{"path"} 0 1 1
200 }
201  
202 3 return 1;
203 }
204  
205  
206 7 1;
207  
208 =head1 NAME
209  
210 IO::Easy::Dir - IO::Easy child class for operations with directories.
211  
212 =head1 SYNOPSIS
213  
214 use IO::Easy;
215  
216 my $dir = IO::Easy->new ('.')->as_dir;
217  
218 $dir->scan_tree (sub {
219 my $file = shift;
220  
221 return 0 if $file->type eq 'dir' and $file->name eq 'CVS';
222 });
223  
224 $dir->create (qw(t IO-Easy)); # creates ./t/IO-Easy
225  
226 my $source = $dir->append('data')->as_dir;
227 my $destination = $dir->append('backup')->as_dir;
228 $source->copy_children($destination, $handler);
229  
230  
231 =head1 METHODS
232  
233 =head2 scan_tree
234  
235 Scans directory tree.
236  
237 There's a standard module File::Find exists. But it's monstrous and is used
238 because of historical reasons. For the same functionality IO::Easy has a
239 method scan_tree and this method can replace File::Find in the most cases.
240  
241 my $io = IO::Easy->new ('.');
242 my $dir = $io->as_dir;
243 $dir->scan_tree ($handler);
244  
245 $handler is a code ref which is called during scan for each found object
246 and retrieves the found object as a parameter.
247  
248 Symlinks processing during directory scanning must be handled by user of this
249 module himself at the moment.
250  
251 As an example with help of $handler you can recursively scan directory and get
252 the number of files with defined extension, in this case function will look like
253 the following:
254  
255 my $counter = 0;
256 my $handler = sub {
257 my $file = shift;
258 $counter++ if $file->extension eq 'pl';
259 }
260  
261 $dir->scan_tree ($handler);
262  
263 print "The number of files/directories with 'pl' extension:", $counter;
264  
265 BEWARE: If $handler returns 0 for the directory, then scan_tree doesn't scan its contents,
266 this can be useful in e.g. ignoring CVS or any other unwanted directories.
267  
268 This method can be called with any of two optional flags:
269 'for_files_only' and 'ignoring_return'
270  
271 For example:
272  
273 my $counter = 0;
274 my $handler = sub {
275 my $file = shift;
276 $counter++ if $file->extension eq 'pl';
277 }
278  
279 $dir->scan_tree (for_files_only => $handler);
280  
281 print "The number of files with 'pl' extension:", $counter;
282  
283 Flag 'for_files_only' tell method to call handler only with objects
284 with file (-f) check
285  
286 Flag 'ignoring_return' tell method to ignore return value from handler
287 and process any found directory
288  
289 =cut
290  
291 =head2 copy_children, copy_node
292  
293 recursive copying of directory contents
294  
295 my $io = IO::Easy->new ('.');
296 my $source = $io->append('data')->as_dir;
297 my $destination = $io->append('backup')->as_dir;
298 $source->copy_children($destination, $handler);
299  
300 In this example $handler code ref, which is performed for every file during copying.
301 With help of the $handler you can easily control the spice which files will be copied.
302  
303 my $handler = sub {
304 my $file = shift;
305 return 1 if $file->extension eq 'txt';
306 return 0;
307 };
308  
309 In this case $handler function copies only files with 'txt' extension to the new
310 directory.
311  
312 =cut
313  
314 =head2 create
315  
316 creates new directory
317  
318 my $io = IO::Easy->new ('.');
319 my $dir = $io->append('data')->as_dir; # appends 'data' to $io and returns
320 #the new object; blesses into directory object.
321 $dir->create; # creates directory './data/'
322  
323 or
324  
325 $io->as_dir->create ('data');
326  
327 =cut
328  
329 =head2 items
330  
331 directory contents in array. you can provide filter for file extension, plain or regexp
332  
333 $dir->items ('txt'); # plain
334 $dir->items ('txt|doc', 1); # regexp
335  
336 =cut
337  
338 =head2 rm_tree
339  
340 recursive deletion directory contents
341  
342 =cut
343  
344 =head2 current
345  
346 current directory constructor, using Cwd
347  
348 =cut
349  
350 =head2 home
351  
352 user home directory
353  
354 =cut
355  
356 =head2 type
357  
358 always 'dir'
359  
360 =head2 touch
361  
362 similar to unix touch command - updates file timestamp
363  
364 =cut
365  
366  
367 =head1 AUTHOR
368  
369 Ivan Baktsheev, C<< <apla at the-singlers.us> >>
370  
371 =head1 BUGS
372  
373 Please report any bugs or feature requests to my email address,
374 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Easy>.
375 I will be notified, and then you'll automatically be notified
376 of progress on your bug as I make changes.
377  
378 =head1 SUPPORT
379  
380  
381  
382 =head1 ACKNOWLEDGEMENTS
383  
384  
385  
386 =head1 COPYRIGHT & LICENSE
387  
388 Copyright 2007-2009 Ivan Baktsheev
389  
390 This program is free software; you can redistribute it and/or modify it
391 under the same terms as Perl itself.
392  
393  
394 =cut
« Easy/File.pm ↑ Summary Easy.pm »