Path::Extended::File
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: $!" );
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.
copy
move
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).
returns a base name of the file via File::Basename::basename.
File::Basename::basename
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.
These are shortcuts for ->open("r") and ->open("w") respectively.
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.
sysopen
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.
closes the stored file handle and removes it from the object. No effect if the file is not open.
are simple wrappers of the equivalents of IO::Handle. No effect if the file is not open.
locks the stored file handle with flock. No effect if the file is not open.
flock
are simple wrappers of the equirvalent built-in functions. Note that Path::Extended doesn't export constants like SEEK_SET, SEEK_CUR, SEEK_END.
SEEK_SET
SEEK_CUR
SEEK_END
returns a mtime of the file/directory. If you pass an argument, you can change the mtime of the file/directory.
returns a size of the file/directory.
unlink the file.
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:
arranges for the file handle to read binary data properly if set to true.
chomps the end-of-lines if set to true.
decodes the lines with the specified encoding.
does arbitrary things through the specified code reference.
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.
slurp
slurp usually stores everything on memory, but sometimes you don't need a return value (especially when you do something with a callback). If this is set to true, slurp doesn't store lines on memory. Note that if you use slurp in the void context, this will be set to true internally.
callback
my @found = $file->grep('string or regex', ...);
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.
creates a parent directory if necessary.
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.
append
locks exclusively while writing.
arranges for the file handle to write binary data properly.
encodes the lines (or the entire string) with the specified encoding.
changes the last modified time to the specified time.
changes file access and modification times, or creates a blank file when it doesn't exist.
Kenichi Ishigaki, <ishigaki@cpan.org>
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.
To install Path::Extended, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Path::Extended
CPAN shell
perl -MCPAN -e shell install Path::Extended
For more information on module installation, please visit the detailed CPAN module installation guide.