Fu.pm

filter: Statementstmt 74.1% 66 / 89 Branchbran 36.3% 8 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 18 / 22 Podpod 100% 9 / 9 totaltotal 69.3% 102 / 147 CC 11 SCAR 15.1
CC
11
Cov
66%
which
20.0
home
6.0
dir
5.6
CRAP 4.5
« Fu/File/Temp.pm ↑ Summary Fu/Dir.pm »
1 package File::Fu;
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 =head1 NAME
9  
10 File::Fu - file and directory objects
11  
12 =head1 SYNOPSIS
13  
14 The directory constructor:
15  
16 use File::Fu;
17  
18 my $dir = File::Fu->dir("bar");
19 print "$dir\n"; # 'bar/'
20  
21 my $file = $dir + 'bar.txt';
22 print "$file\n"; # 'bar/bar.txt'
23  
24 my $d2 = $dir % 'baz'; # 'barbaz/'
25 my $d3 = $dir / 'bat'; # 'bar/bat/'
26  
27 my $file2 = $dir / 'bat' + 'foo.txt'; # 'bar/bat/foo.txt'
28  
29 The file constructor:
30  
31 my $file = File::Fu->file("foo");
32 $file->e and warn "$file exists";
33 $file->l and warn "$file is a link";
34 warn "file is in ", $file->dir;
35  
36 =head1 ABOUT
37  
38 This class provides the toplevel interface to File::Fu directory and
39 file objects, with operator overloading which allows precise path
40 composition and support for most builtin methods, as well as creation of
41 temporary files/directories, finding files, and more.
42  
43 The interface and style are quite different than the perl builtins or
44 File::Spec. The syntax is concise. Errors are thrown with croak(), so
45 you never need to check a return code.
46  
47 =cut
48  
49 13 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 13 ▶use File::Fu::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 13 ▶use File::Fu::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 13 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 13 ▶use constant dir_class => 'File::Fu::Dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 13 ▶use constant file_class => 'File::Fu::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head1 Constructors
59  
60 The actual objects are in the 'Dir' and 'File' sub-namespaces.
61  
62 =head2 dir
63  
64 my $dir = File::Fu->dir($path);
65  
66 See L<File::Fu::Dir/new>
67  
68 =cut
69  
70 33 ▶sub dir {
Subroutine dir: called CC=4 SCAR=17.2
Pod documented
71 33 my $package = shift;
72  
73 33 ▶ $package or croak("huh?");
Branch: 1 branch
BranchTrueFalseTotal
$package or croak("huh?") 33 0 33
74 # also as a function call
75 33 ▶ unless($package and $package->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package and $package->isa(__PACKAGE__)) 0 33 33
Condition33%1/3
$package and $package->isa(__PACKAGE__)
!ll&&!rl&&r
0033
MC/DC0%0/2
$package and $package->isa(__PACKAGE__)
$package $package->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $packageB= $package->isa(__PACKAGE__)
ABresult
0X0
100
111
76 0 unshift(@_, $package);
77 0 $package = __PACKAGE__;
78 }
79  
80 33 $package->dir_class->new(@_);
81 } # end subroutine dir definition
82 ########################################################################
83  
84 =head2 file
85  
86 my $file = File::Fu->file($path);
87  
88 See L<File::Fu::File/new>
89  
90 =cut
91  
92 20 ▶sub file {
Subroutine file: called CC=2 SCAR=8.4
Pod documented
93 20 my $package = shift;
94  
95 # also as a function call
96 20 ▶ unless($package->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->isa(__PACKAGE__)) 0 20 20
97 0 unshift(@_, $package);
98 0 $package = __PACKAGE__;
99 }
100  
101 20 $package->file_class->new(@_);
102 } # end subroutine file definition
103 ########################################################################
104  
105 =head1 Class Constants
106  
107 =head2 tmp
108  
109 Your system's '/tmp/' directory (or equivalent of that.)
110  
111 my $dir = File::Fu->tmp;
112  
113 =cut
114  
115 {
116 13 my $tmp; # XXX needs locking?
117 5 ▶sub tmp {
Subroutine tmp: called CC=2 SCAR=6.9
Pod documented
118 5 my $package = shift;
119 5 ▶ $tmp and return($tmp);
Branch: 1 branch
BranchTrueFalseTotal
$tmp and return $tmp 3 2 5
120 2 return($tmp = $package->dir(File::Spec->tmpdir));
121 }}
122 ########################################################################
123  
124 =head2 home
125  
126 User's $HOME directory.
127  
128 my $dir = File::Fu->home;
129  
130 =cut
131  
132 {
133 13 my $home; # XXX needs locking!
134 0 ▶sub home {
Subroutine home: not called CC=2 SCAR=17.9
Pod documented
135 0 my $package = shift;
136 0 ▶ $home and return($home);
Branch: 1 branch
BranchTrueFalseTotal
$home and return $home 0 0 0
137 0 return($home = $package->dir($ENV{HOME}));
138 }} # end subroutine home definition
139 ########################################################################
140  
141 =head2 program_name
142  
143 The absolute name of your program. This will be relative from the time
144 File::Fu was loaded. It dies if the name is '-e'.
145  
146 my $prog = File::Fu->program_name;
147  
148 If File::Fu was loaded after a chdir and the $0 was relative, calling
149 program_name() throws an error. (Unless you set $0 correctly before
150 requiring File::Fu.)
151  
152 =head2 program_dir
153  
154 Returns what typically corresponds to program_name()->dirname, but
155 just the compile-time cwd() when $0 is -e/-E.
156  
157 my $dir = File::Fu->program_dir;
158  
159 =cut
160  
161 {
162 # fun startup stuff and various logic:
163 13 my $prog = $0;
164 13 my $name_sub;
165 my $dir_sub;
166 13 ▶if(lc($prog) eq '-e') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $prog eq '-e') { } 0 13 13
167 0 my $prog_dir = Cwd::cwd();
168 0 $dir_sub = eval(qq(sub {shift->dir("$prog_dir")}));
169 0 $name_sub = eval(qq(sub {croak("program_name => '$prog'")}));
170 }
171 else {
172 13 ▶ if(-e $prog) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $prog) { } 13 0 13
173 13 my $prog_name = __PACKAGE__->file($prog)->absolutely;
174 13 my $prog_dir = $prog_name->dirname;
175 1 ▶ $name_sub = eval(qq(sub {shift->file('$prog_name')}));
Subroutine __ANON__: called CC=1 SCAR=0.0
176 1 ▶ $dir_sub = eval(qq(sub {shift->dir('$prog_dir')}));
Subroutine __ANON__: called CC=1 SCAR=0.0
177 }
178 else {
179 # runtime error
180 0 ▶ $dir_sub = sub {croak("$prog not found => no program_dir known")};
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 ▶ $name_sub = sub {croak("$prog not found => no program_name known")};
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 }
183 }
184 13 *program_name = $name_sub;
185 13 *program_dir = $dir_sub;
186 } # program_name/program_dir
187 ########################################################################
188  
189 =head1 Class Methods
190  
191 =head2 THIS_FILE
192  
193 A nicer way to say __FILE__.
194  
195 my $file = File::Fu->THIS_FILE;
196  
197 =cut
198  
199 1 ▶sub THIS_FILE {
Subroutine THIS_FILE: called CC=1 SCAR=0.0
Pod documented
200 1 my $package = shift;
201 1 my $name = (caller)[1];
202 1 return $package->file($name);
203 } # end subroutine THIS_FILE definition
204 ########################################################################
205  
206 =head2 cwd
207  
208 The current working directory.
209  
210 my $dir = File::Fu->cwd;
211  
212 =cut
213  
214 3 ▶sub cwd {
Subroutine cwd: called CC=2 SCAR=7.1
Pod documented
215 3 my $package = shift;
216  
217 3 ▶ defined(my $ans = Cwd::cwd()) or croak("cwd() failed");
Branch: 1 branch
BranchTrueFalseTotal
defined(my $ans = Cwd::cwd()) or croak("cwd() failed") 3 0 3
218 3 return $package->dir($ans);
219 } # end subroutine cwd definition
220 ########################################################################
221  
222 =head2 which
223  
224 Returns File::Fu::File objects of ordered candidates for $name found in
225 the path.
226  
227 my @prog = File::Fu->which($name) or die "cannot find $name";
228  
229 If called in scalar context, returns a single File::Fu::File object or throws an error if no candidates were found.
230  
231 my $prog = File::Fu->which($name);
232  
233 =cut
234  
235 0 ▶sub which {
Subroutine which: not called CC=4 SCAR=30.0
Pod documented
236 0 my $package = shift;
237 0 ▶ croak("must have an argument") unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
238 0 my ($what) = @_;
239  
240 0 require File::Which;
241 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
242 0 return map({$package->file($_)} File::Which::which($what));
243 }
244 else {
245 0 ▶ my $found = scalar(File::Which::which($what)) or
Branch: 1 branch
BranchTrueFalseTotal
my $found = scalar File::Which::which($what) or croak("cannot locate '${what}' in PATH") 0 0 0
246 croak("cannot locate '$what' in PATH");
247 0 return $package->file($found);
248 }
249 } # which ##############################################################
250  
251 =head1 Temporary Directories and Files
252  
253 These class methods call the corresponding File::Fu::Dir methods on the
254 value of tmp(). That is, you get a temporary file/dir in the '/tmp/'
255 directory.
256  
257 =head2 temp_dir
258  
259 my $dir = File::Fu->temp_dir;
260  
261 =cut
262  
263 1 ▶sub temp_dir {
Subroutine temp_dir: called CC=1 SCAR=0.0
Pod documented
264 1 my $package = shift;
265 1 $package->tmp->temp_dir(@_);
266 } # end subroutine temp_dir definition
267 ########################################################################
268  
269 =head2 temp_file
270  
271 my $handle = File::Fu->temp_file;
272  
273 =cut
274  
275 3 ▶sub temp_file {
Subroutine temp_file: called CC=1 SCAR=0.0
Pod documented
276 3 my $package = shift;
277 3 $package->tmp->temp_file(@_);
278 } # end subroutine temp_file definition
279 ########################################################################
280  
281 =head1 Operators
282  
283 If you choose not to use the overloaded operators, you can just say
284 C<$obj-E<gt>stringify()> or "$obj" whenever you want to drop the
285 object-y nature and treat the path as a string.
286  
287 The operators can be convenient for building-up path names, but you
288 probably don't want to think of them as "math on filenames", because
289 they are nothing like that.
290  
291 The '+' and '/' operators only apply to directory objects.
292  
293 op method mnemonic
294 -- ---------------- --------------------
295 + $d->file($b) ............. plus (not "add")
296 / $d->subdir($b) ........... slash (not "divide")
297  
298 The other operators apply to both files and directories.
299  
300 op method mnemonic
301 -- ---------------- --------------------
302 %= $p->append($b) ........... mod(ify)
303 % $p->clone->append($b)
304 &= $p->map(sub{...}) ........ invoke subref
305 & $p->clone->map(sub {...})
306  
307 Aside: It would be more natural to use C<.=> as append(), but the way
308 perl compiles C<"$obj foo"> into C<$obj . " foo"> makes it impossible to
309 do the right thing because the lines between object and string are too
310 ambiguous.
311  
312 =head1 Subclassing
313  
314 You may wish to subclass File:Fu and override the dir_class() and/or
315 file_class() class methods to point to your own Dir/File subclasses.
316  
317 my $class = 'My::FileFu';
318 my $dir = $class->dir("foo");
319  
320 See L<File::Fu::File> and L<File::Fu::Dir> for more info.
321  
322 =head2 dir_class
323  
324 File::Fu->dir_class # File::Fu::Dir
325  
326 =head2 file_class
327  
328 File::Fu->file_class # File::Fu::File
329  
330 =head1 See Also
331  
332 L<File::Fu::why> if I need to explain my motivations.
333  
334 L<Path::Class>, from which many an idea was taken.
335  
336 L<File::stat>, L<IO::File>, L<File::Spec>, L<File::Find>, L<File::Temp>,
337 L<File::Path>, L<File::Basename>, L<perlfunc>, L<perlopentut>.
338  
339 =head1 AUTHOR
340  
341 Eric Wilhelm @ <ewilhelm at cpan dot org>
342  
343 http://scratchcomputing.com/
344  
345 =head1 BUGS
346  
347 If you found this module on CPAN, please report any bugs or feature
348 requests through the web interface at L<http://rt.cpan.org>. I will be
349 notified, and then you'll automatically be notified of progress on your
350 bug as I make changes.
351  
352 If you pulled this development version from my /svn/, please contact me
353 directly.
354  
355 =head1 COPYRIGHT
356  
357 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
358  
359 =head1 NO WARRANTY
360  
361 Absolutely, positively NO WARRANTY, neither express or implied, is
362 offered with this software. You use this software at your own risk. In
363 case of loss, no person or entity owes you anything whatsoever. You
364 have been warned.
365  
366 =head1 LICENSE
367  
368 This program is free software; you can redistribute it and/or modify it
369 under the same terms as Perl itself.
370  
371 =cut
372  
373 # vi:ts=2:sw=2:et:sta
374 13 1;
« Fu/File/Temp.pm ↑ Summary Fu/Dir.pm »