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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
package File::Fu::File;
$VERSION = v0.0.7;
use warnings;
use strict;
use Carp;
use IO::File ();
=head1 NAME
File::Fu::File - a filename object
=head1 SYNOPSIS
use File::Fu;
my $file = File::Fu->file("path/to/file");
$file %= '.extension';
$file->e and warn "$file exists";
$file->l and warn "$file is a link to ", $file->readlink;
=cut
use base 'File::Fu::Base';
use Class::Accessor::Classy;
lv 'file';
ro 'dir'; aka dir => 'dirname', 'parent';
no Class::Accessor::Classy;
#use overload ();
=head1 Constructor
=head2 new
my $file = File::Fu::File->new($path);
my $file = File::Fu::File->new(@path);
=cut
sub new {
my $package = shift;
my $class = ref($package) || $package;
my $self = {$class->_init(@_)};
bless($self, $class);
return($self);
} # end subroutine new definition
########################################################################
=head2 new_direct
my $file = File::Fu::File->new_direct(
dir => $dir_obj,
file => $name
);
=cut
sub new_direct {
my $package = shift;
my $class = ref($package) || $package;
my $self = {@_};
bless($self, $class);
return($self);
} # end subroutine new_direct definition
########################################################################
=head1 Class Constants
=head2 dir_class
Return the corresponding dir class for this file object.
my $dc = $class->dir_class;
=head2 is_dir
Always false for a file.
=head2 is_file
Always true for a file.
=cut
use constant dir_class => 'File::Fu::Dir';
use constant is_dir => 0;
use constant is_file => 1;
########################################################################
=for internal head2 _init
my %fields = $class->_init(@_);
=cut
sub _init {
my $class = shift;
my @dirs = @_ or croak("file must have a name");
my $file = pop(@dirs);
if($file =~ m#/#) {
croak("strange mix: ", join(',', @_, $file)) if(@dirs);
my %p = $class->dir_class->_init($file);
@dirs = @{$p{dirs}};
$file = pop(@dirs);
}
return(dir => $class->dir_class->new(@dirs), file => $file);
} # end subroutine _init definition
########################################################################
=head1 Parts
=head2 basename
Returns a new object representing only the file part of the name.
my $obj = $file->basename;
=cut
sub basename {
my $self = shift;
$self->new($self->file);
} # end subroutine basename definition
########################################################################
=head1 Methods
=head2 stringify
my $string = $file->stringify;
=cut
sub stringify {
my $self = shift;
my $dir = $self->dir;
#warn "stringify(..., $_[1], $_[2])";
#Carp::carp("stringify ", overload::StrVal($self), " ($self->{file})");
$dir = $dir->is_cwd ? '' : $dir->stringify;
return($dir . $self->file);
} # end subroutine stringify definition
########################################################################
=head2 append
Append a string only to the filename part.
$file->append('.gz');
$file %= '.gz';
(Yeah... I tried to use .=, but overloading hates me.)
=cut
sub append {
my $self = shift;
my ($tail) = @_;
$self->file .= $tail;
$self;
} # end subroutine append definition
########################################################################
=head2 map
$file->map(sub {...});
$file &= sub {...};
=cut
sub map :method {
my $self = shift;
my ($sub) = shift;
local $_ = $self->file;
$sub->();
$self->file = $_;
$self;
} # end subroutine map definition
########################################################################
=head2 absolute
Get an absolute name (without checking the filesystem.)
my $abs = $file->absolute;
=cut
sub absolute {
my ($self) = shift;
return($self->dir->absolute->file($self->file));
} # end subroutine absolutely definition
########################################################################
=head2 absolutely
Get an absolute name (resolved on the filesytem.)
my $abs = $file->absolutely;
=cut
sub absolutely {
my $self = shift;
return($self->dir->absolutely->file($self->file));
} # end subroutine absolutely definition
########################################################################
=head1 Doing stuff
=head2 open
Open the file with $mode ('<', 'r', '>', 'w', etc) -- see L<IO::File>.
my $fh = $file->open($mode, $permissions);
Throws an error if anything goes wrong or if the resulting filehandle
happens to be a directory.
=cut
# TODO should probably have our own filehandle so we can close in the
# destructor and croak there too?
sub open :method {
my $self = shift;
my $fh = IO::File->new($self, @_) or croak("cannot open '$self' $!");
-d $fh and croak("$self is a directory");
return($fh);
} # end subroutine open definition
########################################################################
=head2 piped_open
Opens a read pipe. The file is appended to @command.
my $fh = $file->piped_open(@command);
=cut
sub piped_open {
my $self = shift;
my (@command) = @_;
# TODO some way to decide where self goes in @command
push(@command, $self);
# TODO closing STDIN and such before the fork?
# TODO here is where we need our own filehandle object again
my $pid = open(my $fh, '-|', @command) or
croak("cannot exec '@command' $!");
return($fh);
} # end subroutine piped_open definition
########################################################################
=head2 touch
Update the timestamp of a file (or create it.)
$file->touch;
=cut
sub touch {
my $self = shift;
if(-e $self) {
$self->utime(time);
}
else {
$self->open('>');
}
return($self);
} # end subroutine touch definition
########################################################################
=head2 link
my $link = $file->link($name);
=cut
sub link :method {
my $self = shift;
my ($name) = @_;
link($self, $name) or croak("link '$self' to '$name' failed $!");
return($self->new($name));
} # end subroutine link definition
########################################################################
=head2 symlink
my $link = $file->symlink($linkname);
Note that symlinks are relative to where they live.
my $dir = File::Fu->dir("foo");
my $file = $dir+'file';
# $file->symlink($dir+'link'); is a broken link
my $link = $file->basename->symlink($dir+'link');
=cut
sub symlink :method {
my $self = shift;
my ($name) = @_;
symlink($self, $name) or
croak("symlink '$self' to '$name' failed $!");
return($self->new($name));
} # end subroutine symlink definition
########################################################################
# TODO
# my $link = $file->dwimlink(absolute|relative|samedir => $linkname);
=head2 unlink
$file->unlink;
=cut
sub unlink :method {
my $self = shift;
unlink("$self") or croak("unlink '$self' failed $!");
} # end subroutine unlink definition
########################################################################
=head2 readlink
my $to = $file->readlink;
=cut
sub readlink :method {
my $self = shift;
my $name = readlink($self);
defined($name) or croak("cannot readlink '$self' $!");
return($self->new($name));
} # end subroutine readlink definition
########################################################################
########################################################################
{ # a closure for this variable
my $has_slurp;
=head2 read
Read the entire file into memory (or swap!)
my @lines = $file->read;
my $file = $file->read;
If File::Slurp is available, options to read_file will be passed along.
See L<File::Slurp/read_file>.
=cut
sub read :method {
my $self = shift;
my @args = @_;
$has_slurp ||= eval {require File::Slurp; 1} || -1;
if($has_slurp > 0) {
local $Carp::CarpLevel = 1;
return(File::Slurp::read_file("$self", @args, err_mode => 'croak'));
}
else {
croak("must have File::Slurp for fancy reads") if(@args);
my $fh = $self->open;
local $/ = wantarray ? $/ : undef;
return(<$fh>);
}
} # end subroutine read definition
########################################################################
=head2 write
Write the file's contents.
$file->write($content);
If File::Slurp is available, $content may be either a scalar, scalar
ref, or array ref.
$file->write($content, %args);
=cut
sub write {
my $self = shift;
my ($content, @args) = @_;
$has_slurp ||= eval {require File::Slurp; 1} || -1;
if($has_slurp > 0) {
local $Carp::CarpLevel = 1;
return(File::Slurp::write_file("$self",
{@args, err_mode => 'croak'},
$content
));
}
else {
croak("must have File::Slurp for fancy writes")
if(@args or ref($content));
my $fh = $self->open('>');
print $fh $content;
close($fh) or croak("write '$self' failed: $!");
}
} # end subroutine write definition
########################################################################
} # File::Slurp closure
########################################################################
=head2 copy
$file->copy($dest);
=cut
sub copy {
my $self = shift;
my ($dest) = shift;
my (%opts) = @_;
# decide if this is file-to-dir or file-to-file
if(-d $dest) {
$dest = $self->dir_class->new($dest)->file($self->basename);
}
else {
$dest = $self->new($dest) unless(ref($dest));
}
if($dest->e) {
croak("'$dest' and '$self' are the same file")
if($self->is_same($dest));
}
# TODO here's another good reason to have our own filehandle object:
# This fh-copy should be in there.
my $ifh = $self->open;
my $ofh = $dest->open('>');
binmode($_) for($ifh, $ofh);
while(1) {
my $buf;
defined(my $r = sysread($ifh, $buf, 1024)) or
croak("sysread failed $!");
$r or last;
# why did File::Copy::copy do it like this?
for(my $t = my $w = 0; $w < $r; $w += $t) {
$t = syswrite($ofh, $buf, $r - $w, $w) or
croak("syswrite failed $!");
}
}
close($ofh) or croak("write '$dest' failed: $!");
# TODO some form of rollback?
# TODO handle opts
#if($opts{preserve}) {
# # TODO chmod/chown and such
# $dest->utime($self->stat->mtime);
#}
} # end subroutine copy definition
########################################################################
=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
require File::Fu;
# vi:ts=2:sw=2:et:sta
1;
|