Fu/Base.pm

filter: Statementstmt 67.3% 93 / 138 Branchbran 50.0% 25 / 50 Conditioncond 16.6% 2 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 51.7% 29 / 56 Podpod 100% 14 / 14 totaltotal 58.9% 164 / 278 CC 30 SCAR 24.7
CC
30
Cov
60%
is_same
90.0
clone
6.3
relative_to
6.0
CRAP 11.8
« Fu/File.pm ↑ Summary Fu/File/Temp.pm »
1 package File::Fu::Base;
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 File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 File::Fu::Base - nothing to see here
13  
14 =head1 SYNOPSIS
15  
16 =cut
17  
18 13 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ '=' => sub {shift->clone(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
20 '""' => 'stringify',
21 '%=' => 'append',
22 2 ▶ '%' => sub {shift->clonedo('append', @_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
23 # can't overload s/// or accomplish anything with prototypes
24 1 ▶ '&' => sub {shift->clonedo('map', @_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
25 '&=' => 'map',
26 184 ▶ cmp => sub {"$_[0]" cmp "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 # invalid methods
29 1 ▶ '-' => sub {shift->error('-')},
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 ▶ '*' => sub {shift->error('*')},
Subroutine __ANON__: called CC=1 SCAR=0.0
31 0 ▶ '~' => sub {~ shift->stringify},
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 1 ▶ nomethod => sub {shift->error($_[2])},
Subroutine __ANON__: called CC=1 SCAR=0.0
33 13 );
34  
35 =head2 clone
36  
37 my $obj = $obj->clone;
38  
39 =cut
40  
41 171 ▶sub clone {
Subroutine clone: called CC=6 SCAR=18.4
Pod documented
42 171 my $self = shift;
43 171 my $clone = {%$self};
44 171 bless($clone, ref($self));
45 #carp("clone the ", overload::StrVal($self));
46 171 foreach my $item (values(%$clone)) {
47 182 ▶ my $ref = ref($item) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = ref $item or next 171 11 182
48 171 ▶ if($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 166 5 171
elsif ($ref eq 'HASH') { } 0 5 5
elsif (eval { do { $item->can('clone') } }) { } 5 0 5
49 #warn "clone [@$item]\n";
50 166 $item = [@$item];
51 }
52 elsif($ref eq 'HASH') {
53 0 $item = {%$item};
54 }
55 5 elsif(eval {$item->can('clone')}) {
56 5 $item = $item->clone
57 }
58 else {
59 0 croak("cannot deref $item");
60 }
61 }
62 #carp("now ", overload::StrVal($clone));
63 171 return($clone);
64 } # end subroutine clone definition
65 ########################################################################
66  
67 =head2 clonedo
68  
69 $clone = $self->clonedo($action, @args);
70  
71 =cut
72  
73 3 ▶sub clonedo {
Subroutine clonedo: called CC=3 SCAR=13.0
Pod documented
74 3 my $self = shift;
75 3 my ($action, $arg, $rev) = @_;
76 #carp("clonedo $action", $rev ? ' backwards' : '');
77 3 ▶ if($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) 0 3 3
78 0 ▶ return($arg . $self->stringify) if($action eq 'append');
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "append" 0 0 0
79 0 croak("$action is invalid in that order");
80 }
81  
82 # perl doesn't know how to stringify
83 # TODO how can I tell when this is just a quoted string?
84 #if($action eq 'append' and $arg =~ m/\n/) { return($self->stringify . $arg); }
85  
86 3 $self = $self->clone;
87 3 $self->$action($arg);
88 #carp("now ", overload::StrVal($self));
89 3 return($self);
90 } # end subroutine clonedo definition
91 ########################################################################
92  
93 =head2 error
94  
95 $package->error($op);
96  
97 =cut
98  
99 3 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
100 3 my $self = shift;
101 3 my ($op) = @_;
102 3 croak("$op is not a valid op for a ", ref($self), " object");
103 } # end subroutine error definition
104 ########################################################################
105  
106 =head1 Filetests
107  
108 =head2 r w x o R W X O e z s f d l p S b c t u g k T B M A C
109  
110 See perldoc -f -x
111  
112 =cut
113  
114 13 foreach my $test (split(//, 'rwxoRWXOezsfdlpSbctugkTBMAC')) {
115 0 ▶ my $subref = eval("sub {-$test shift}");
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: called CC=1 SCAR=4.9
Subroutine __ANON__: called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
116 351 ▶ $@ and croak("I broke this -- $@");
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak("I broke this -- $@") 0 351 351
117 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 351 *{"$test"} = $subref;
119 }
120  
121 =head1 File::Spec stuff
122  
123 This needs to be redone.
124  
125 =cut
126  
127 13 ▶use File::Spec; # GRR
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 =head2 is_absolute
130  
131 =cut
132  
133 6 ▶sub is_absolute {
Subroutine is_absolute: called CC=1 SCAR=0.0
Pod documented
134 # XXX this is immutable, no?
135 6 File::Spec->file_name_is_absolute($_[0]->stringify);
136 }
137  
138 =head2 relative
139  
140 Get a relative name.
141  
142 my $rel = $abs->relative;
143  
144 Also, with optional relative-to directory:
145  
146 my $rel = $abs->relative($to);
147  
148 =cut
149  
150 5 ▶sub relative {
Subroutine relative: called CC=2 SCAR=6.9
Pod documented
151 5 my $self = shift;
152 5 my $base = shift;
153 5 ▶ return $self->new(File::Spec->abs2rel($self->stringify,
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 3 2 5
154 defined($base) ? "$base" : ()
155 ));
156 }
157  
158 =head2 relative_to
159  
160 Same as relative(), but requires the $dir argument.
161  
162 my $rel = $abs->relative_to($dir);
163  
164 =cut
165  
166 0 ▶sub relative_to {
Subroutine relative_to: not called CC=2 SCAR=17.9
Pod documented
167 0 my $self = shift;
168 0 ▶ my $base = shift or croak('relative_to() requires a $dir');
Branch: 1 branch
BranchTrueFalseTotal
my $base = shift() or croak("relative_to() requires a \$dir") 0 0 0
169 0 return $self->relative($base);
170 }
171  
172 =head2 resolve
173  
174 Fully resolve any symlinks;
175  
176 my $path = $path->resolve;
177  
178 =cut
179  
180 2 ▶sub resolve {
Subroutine resolve: called CC=3 SCAR=11.0
Pod documented
181 2 my $self = shift;
182 2 while(1) {
183 3 ▶ return $self unless($self->l);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->l 2 1 3
184 1 my $to = $self->readlink;
185 1 ▶ return $to if($to->is_absolute);
Branch: 1 branch
BranchTrueFalseTotal
if $to->is_absolute 0 1 1
186 1 $self = $self->new($self->dirname . $to);
187 }
188 } # end subroutine resolve definition
189 ########################################################################
190  
191 =head2 relative_symlink
192  
193 Where $path and $linkname are both relative to the current directory.
194  
195 $path->relative_symlink($linkname);
196  
197 =cut
198  
199 3 ▶sub relative_symlink {
Subroutine relative_symlink: called CC=1 SCAR=0.0
Pod documented
200 3 my $self = shift;
201 3 my ($link) = @_;
202  
203 3 my $rel = $self->relative($self->new($link)->dirname);
204 3 return($rel->symlink($link));
205 } # end subroutine relative_symlink definition
206 ########################################################################
207  
208 =head2 utime
209  
210 Update the file timestamps.
211  
212 $file->utime($atime, $mtime);
213  
214 Optionally, set both to the same time.
215  
216 $file->utime($time);
217  
218 Also see touch().
219  
220 =cut
221  
222 2 ▶sub utime {
Subroutine utime: called CC=5 SCAR=16.5
Pod documented
223 2 my $self = shift;
224 2 ▶ @_ or croak("not enough arguments to utime()");
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("not enough arguments to utime()") 2 0 2
225 2 my $at = shift;
226 2 ▶ my $mt = @_ ? shift(@_) : $at;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
227 2 ▶ if($self->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_dir) 2 0 2
228 2 $self = $self->bare;
229 }
230 2 ▶ utime($at, $mt, $self) or croak("cannot utime '$self' $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::utime $at, $mt, $self or croak("cannot utime '${self}' $!") 1 1 2
231 } # end subroutine utime definition
232 ########################################################################
233  
234 =head2 chmod
235  
236 $path->chmod($mode);
237  
238 =cut
239  
240 1 ▶sub chmod :method {
Subroutine chmod: called CC=2 SCAR=7.1
Pod documented
241 1 my $self = shift;
242 1 my ($mode) = @_;
243  
244 1 ▶ chmod($mode, "$self") or croak("cannot chmod '$self' $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod $mode, "$self" or croak("cannot chmod '${self}' $!") 1 0 1
245 } # end subroutine chmod definition
246 ########################################################################
247  
248 =head2 rename
249  
250 Calls the builtin rename() on the $path and returns a new object with
251 that name.
252  
253 $path = $path->rename($newname);
254  
255 =cut
256  
257 2 ▶sub rename :method {
Subroutine rename: called CC=2 SCAR=7.0
Pod documented
258 2 my $self = shift;
259 2 my ($name) = @_;
260  
261 2 ▶ rename($self, $name) or
Branch: 1 branch
BranchTrueFalseTotal
CORE::rename $self, $name or croak("cannot rename '${self}' to '${name}' $!") 2 0 2
262 croak("cannot rename '$self' to '$name' $!");
263 2 return($self->new($name));
264 } # end subroutine rename definition
265 ########################################################################
266  
267 =head1 Stat Object
268  
269 The stat() and lstat() methods both return a File::stat object.
270  
271 =head2 stat
272  
273 my $st = $obj->stat;
274  
275 =cut
276  
277 4 ▶sub stat {
Subroutine stat: called CC=2 SCAR=6.9
Pod documented
278 4 my $self = shift;
279 4 ▶ my $st = File::stat::stat("$self") or
Branch: 1 branch
BranchTrueFalseTotal
my $st = &File::stat::stat("$self") or croak("cannot stat '${self}' $!") 3 1 4
280 croak("cannot stat '$self' $!");
281 3 return($st);
282 } # end subroutine stat definition
283 ########################################################################
284  
285 =head2 lstat
286  
287 Same as stat, but does not dereference symlinks.
288  
289 my $st = $obj->lstat;
290  
291 =cut
292  
293 3 ▶sub lstat {
Subroutine lstat: called CC=4 SCAR=14.0
Pod documented
294 3 my $self = shift;
295  
296 3 ▶ if($self->is_dir and $self->l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_dir and $self->l) 2 1 3
Condition66%2/3
$self->is_dir and $self->l
!ll&&!rl&&r
012
MC/DC50%1/2
$self->is_dir and $self->l
$self->is_dir $self->l
Truth table66%2/3
A and B
A= $self->is_dirB= $self->l
ABresult
0X0
100
111
297 2 $self = $self->bare;
298 }
299 3 ▶ my $st = File::stat::lstat("$self") or
Branch: 1 branch
BranchTrueFalseTotal
my $st = &File::stat::lstat("$self") or croak("cannot lstat '${self}' $!") 3 0 3
300 croak("cannot lstat '$self' $!");
301 3 return($st);
302 } # end subroutine lstat definition
303 ########################################################################
304  
305 =head2 is_same
306  
307 Returns true if the two paths are the same. This is by string equality,
308 then (if both paths exist) by device+inode equality.
309  
310 $bool = $path->is_same($other);
311  
312 =cut
313  
314 0 ▶sub is_same {
Subroutine is_same: not called CC=9 SCAR=45.0
Pod documented
315 0 my $self = shift;
316 0 my ($other) = @_;
317 0 ▶ unless(ref $other) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other) 0 0 0
318 0 ▶ my $proto = ($self->is_file and $other =~ m#/$#) ?
Branch: 1 branch
BranchTrueFalseTotal
$self->is_file && $other =~ m[/$] ? : 0 0 0
Condition0%0/3
$self->is_file and $other =~ m[/$]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_file and $other =~ m[/$]
$self->is_file $other =~ m[/$]
Truth table0%0/3
A and B
A= $self->is_fileB= $other =~ m[/$]
ABresult
0X0
100
111
319 $self->dir_class : $self;
320 0 $other = $proto->new($other);
321 }
322 0 ▶ return(1) if($self eq $other);
Branch: 1 branch
BranchTrueFalseTotal
if $self eq $other 0 0 0
323 0 ▶ return(0) if($self->is_dir != $other->is_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dir != $other->is_dir 0 0 0
324 0 my $n = 0;
325 # TODO just check absolutely?
326 # this currently probably misses non-existent files where the dirname
327 # resolves to the same location.
328 0 my ($s1, $s2) = map({eval {$_->stat}} $self, $other);
329 0 ▶ return(0) unless($s1 and $s2);
Branch: 1 branch
BranchTrueFalseTotal
unless $s1 and $s2 0 0 0
Condition0%0/3
$s1 and $s2
!ll&&!rl&&r
000
MC/DC0%0/2
$s1 and $s2
$s1 $s2
Truth table0%0/3
A and B
A= $s1B= $s2
ABresult
0X0
100
111
330 return(
331 0 ▶ $s1->dev eq $s2->dev and
Condition0%0/3
$s1->dev eq $s2->dev && $s1->ino eq $s2->ino
!ll&&!rl&&r
000
MC/DC0%0/2
$s1->dev eq $s2->dev && $s1->ino eq $s2->ino
$s1->dev eq $s2->dev $s1->ino eq $s2->ino
Truth table0%0/3
A && B
A= $s1->dev eq $s2->devB= $s1->ino eq $s2->ino
ABresult
0X0
100
111
332 $s1->ino eq $s2->ino
333 );
334 } # end subroutine is_same definition
335 ########################################################################
336  
337 =head1 AUTHOR
338  
339 Eric Wilhelm @ <ewilhelm at cpan dot org>
340  
341 http://scratchcomputing.com/
342  
343 =head1 BUGS
344  
345 If you found this module on CPAN, please report any bugs or feature
346 requests through the web interface at L<http://rt.cpan.org>. I will be
347 notified, and then you'll automatically be notified of progress on your
348 bug as I make changes.
349  
350 If you pulled this development version from my /svn/, please contact me
351 directly.
352  
353 =head1 COPYRIGHT
354  
355 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
356  
357 =head1 NO WARRANTY
358  
359 Absolutely, positively NO WARRANTY, neither express or implied, is
360 offered with this software. You use this software at your own risk. In
361 case of loss, no person or entity owes you anything whatsoever. You
362 have been warned.
363  
364 =head1 LICENSE
365  
366 This program is free software; you can redistribute it and/or modify it
367 under the same terms as Perl itself.
368  
369 =cut
370  
371 # vi:ts=2:sw=2:et:sta
372 13 1;
« Fu/File.pm ↑ Summary Fu/File/Temp.pm »