NAME
Path::Extended::File
SYNOPSIS
use Path::Extended::File;
my $file = Path::Extended::File->new('path/to/file');
# you can get information of the file
print $file->basename; # file
print $file->absolute; # /absolute/path/to/file
# you can get an object for the parent directory
my $parent_dir = $file->parent;
# Path::Extended::File object works like an IO handle
$file->openr;
my $first_line = $file->getline;
print <$file>;
close $file;
# it also can do some extra file related tasks
$file->copy_to('/other/path/to/file');
$file->unlink if $file->exists;
$file->slurp(chomp => 1, callback => sub {
my $line = shift;
print $line, "\n" unless substr($line, 0, 1) eq '#';
});
file('/path/to/other_file')->save(\@lines, { mkdir => 1 });
# it has a logger, too
$file->log( fatal => "Couldn't open $file: $!" );
DESCRIPTION
This class implements file-specific methods. Most of them are simple wrappers of the equivalents from various File::* or IO::* classes. See also Path::Class::Entity for common methods like copy
and move
.
METHODS
new
takes a path or parts of a path of a file, and creates a Path::Extended::File object. If the path specified is a relative one, it will be converted to the absolute one internally. Note that this doesn't open a file even when you pass an extra file mode (which will be considered as a part of the file name).
basename
returns a base name of the file via File::Basename::basename
.
open
opens the file with a specified mode, and returns the $self object to chain methods, or undef if there's anything wrong (the handle opened is stored internally). You can use characters like "r" and "w", or symbols like "<" and ">". If you want to specify IO layers, use the latter format (e.g. "<:raw"). If the file is already open, it closes at first and opens again.
openr, openw
These are shortcuts for ->open("r") and ->open("w") respectively.
sysopen
takes the third (and the fourth if necessary) arguments (i.e. mode and permission) of the native sysopen
, and opens the file, and returns the $self object, or undef if there's anything wrong. The handle opened is stored internally.
binmode
may take an argument (to specify I/O layers), and arranges for the stored file handle to handle binary data properly. No effect if the file is not open.
close
closes the stored file handle and removes it from the object. No effect if the file is not open.
print, printf, say, getline, getlines, read, sysread, write, syswrite, autoflush, flush, printflush, getc, ungetc, truncate, blocking, eof, fileno, error, sync, fcntl, ioctl
are simple wrappers of the equivalents of IO::Handle. No effect if the file is not open.
lock_ex, lock_sh
locks the stored file handle with flock
. No effect if the file is not open.
seek, sysseek, tell
are simple wrappers of the equirvalent built-in functions. Note that Path::Extended doesn't export constants like SEEK_SET
, SEEK_CUR
, SEEK_END
.
mtime
returns a mtime of the file/directory. If you pass an argument, you can change the mtime of the file/directory.
size
returns a size of the file/directory.
remove
unlink the file.
slurp
may take a hash (or a hash referernce) option, then opens the file, does various things for each line with callbacks if necessary, and returns an array of the lines (list context), or the concatenated string (scalar context).
Options are:
- binmode
-
arranges for the file handle to read binary data properly if set to true.
- chomp
-
chomps the end-of-lines if set to true.
- decode
-
decodes the lines with the specified encoding.
- callback
-
does arbitrary things through the specified code reference.
- filter
-
slurp
usually returns all the (processed) lines. With this option (which should be a string or a regex),slurp
returns only the lines that match the filter rule. - ignore_return_value
-
slurp
usually stores everything on memory, but sometimes you don't need a return value (especially when you do something with acallback
). If this is set to true,slurp
doesn't store lines on memory. Note that if you useslurp
in the void context, this will be set to true internally.
grep
my @found = $file->grep('string or regex', ...);
save
takes a string or an array reference of lines, and an optional hash (or a hash reference), then opens the file, does various things for each line (or the entire string) with callbacks if necessary, and saves the content to the file, and returns the $self object or undef if there's anything wrong.
Options are:
- mkdir
-
creates a parent directory if necessary.
- mode, append
-
takes a mode specification ("w", "a", or equivalent symbols). The default is a write mode, and if you set
append
option to true, it will be changed to a append mode. - lock
-
locks exclusively while writing.
- binmode
-
arranges for the file handle to write binary data properly.
- encode
-
encodes the lines (or the entire string) with the specified encoding.
- callback
-
does arbitrary things through the specified code reference.
- mtime
-
changes the last modified time to the specified time.
touch
changes file access and modification times, or creates a blank file when it doesn't exist.
AUTHOR
Kenichi Ishigaki, <ishigaki@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2008 by Kenichi Ishigaki.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.