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