Path::Tiny - File path utility
version 0.056
use Path::Tiny; # creating Path::Tiny objects $dir = path("/tmp"); $foo = path("foo.txt"); $subdir = $dir->child("foo"); $bar = $subdir->child("bar.txt"); # stringifies as cleaned up path $file = path("./foo.txt"); print $file; # "foo.txt" # reading files $guts = $file->slurp; $guts = $file->slurp_utf8; @lines = $file->lines; @lines = $file->lines_utf8; $head = $file->lines( {count => 1} ); # writing files $bar->spew( @data ); $bar->spew_utf8( @data ); # reading directories for ( $dir->children ) { ... } $iter = $dir->iterator; while ( my $next = $iter->() ) { ... }
This module provide a small, fast utility for working with file paths. It is friendlier to use than File::Spec and provides easy access to functions from several other core file handling modules. It aims to be smaller and faster than many alternatives on CPAN while helping people do many common things in consistent and less error-prone ways.
Path::Tiny does not try to work for anything except Unix-like and Win32 platforms. Even then, it might break if you try something particularly obscure or tortuous. (Quick! What does this mean: ///../../..//./././a//b/.././c/././? And how does it differ on Win32?)
///../../..//./././a//b/.././c/././
All paths are forced to have Unix-style forward slashes. Stringifying the object gives you back the path (after some clean up).
File input/output methods flock handles before reading or writing, as appropriate (if supported by the platform).
flock
The *_utf8 methods (slurp_utf8, lines_utf8, etc.) operate in raw mode. On Windows, that means they will not have CRLF translation from the :crlf IO layer. Installing Unicode::UTF8 0.58 or later will speed up *_utf8 situations in many cases and is highly recommended.
*_utf8
slurp_utf8
lines_utf8
:crlf
$path = path("foo/bar"); $path = path("/tmp", "file.txt"); # list $path = path("."); # cwd $path = path("~user/file.txt"); # tilde processing
Constructs a Path::Tiny object. It doesn't matter if you give a file or directory path. It's still up to you to call directory-like methods only on directories and file-like methods only on files. This function is exported automatically by default.
Path::Tiny
The first argument must be defined and have non-zero length or an exception will be thrown. This prevents subtle, dangerous errors with code like path( maybe_undef() )->remove_tree.
path( maybe_undef() )->remove_tree
If the first component of the path is a tilde ('~') then the component will be replaced with the output of glob('~'). If the first component of the path is a tilde followed by a user name then the component will be replaced with output of glob('~username'). Behaviour for non-existent users depends on the output of glob on the system.
glob('~')
glob('~username')
glob
On Windows, if the path consists of a drive identifier without a path component (C: or D:), it will be expanded to the absolute path of the current directory on that volume using Cwd::getdcwd().
C:
D:
Cwd::getdcwd()
If called with a single Path::Tiny argument, the original is returned unless the original is holding a temporary file or directory reference in which case a stringified copy is made.
$path = path("foo/bar"); $temp = Path::Tiny->tempfile; $p2 = path($path); # like $p2 = $path $t2 = path($temp); # like $t2 = path( "$temp" )
This optimizes copies without proliferating references unexpectedly if a copy is made by code outside your control.
$path = Path::Tiny->new("foo/bar");
This is just like path, but with method call overhead. (Why would you do that?)
path
$path = Path::Tiny->cwd; # path( Cwd::getcwd ) $path = cwd; # optional export
Gives you the absolute path to the current directory as a Path::Tiny object. This is slightly faster than path(".")->absolute.
path(".")->absolute
cwd may be exported on request and used as a function instead of as a method.
cwd
$path = Path::Tiny->rootdir; # / $path = rootdir; # optional export
Gives you File::Spec->rootdir as a Path::Tiny object if you're too picky for path("/").
File::Spec->rootdir
path("/")
rootdir may be exported on request and used as a function instead of as a method.
rootdir
$temp = Path::Tiny->tempfile( @options ); $temp = Path::Tiny->tempdir( @options ); $temp = tempfile( @options ); # optional export $temp = tempdir( @options ); # optional export
tempfile passes the options to File::Temp->new and returns a Path::Tiny object with the file name. The TMPDIR option is enabled by default.
tempfile
File::Temp->new
TMPDIR
The resulting File::Temp object is cached. When the Path::Tiny object is destroyed, the File::Temp object will be as well.
File::Temp
File::Temp annoyingly requires you to specify a custom template in slightly different ways depending on which function or method you call, but Path::Tiny lets you ignore that and can take either a leading template or a TEMPLATE option and does the right thing.
TEMPLATE
$temp = Path::Tiny->tempfile( "customXXXXXXXX" ); # ok $temp = Path::Tiny->tempfile( TEMPLATE => "customXXXXXXXX" ); # ok
The tempfile path object will normalized to have an absolute path, even if created in a relative directory using DIR.
DIR
tempdir is just like tempfile, except it calls File::Temp->newdir instead.
tempdir
File::Temp->newdir
Both tempfile and tempdir may be exported on request and used as functions instead of as methods.
$abs = path("foo/bar")->absolute; $abs = path("foo/bar")->absolute("/tmp");
Returns a new Path::Tiny object with an absolute path (or itself if already absolute). Unless an argument is given, the current directory is used as the absolute base path. The argument must be absolute or you won't get an absolute result.
This will not resolve upward directories ("foo/../bar") unless canonpath in File::Spec would normally do so on your platform. If you need them resolved, you must call the more expensive realpath method instead.
canonpath
realpath
On Windows, an absolute path without a volume component will have it added based on the current drive.
path("foo.txt")->append(@data); path("foo.txt")->append(\@data); path("foo.txt")->append({binmode => ":raw"}, @data); path("foo.txt")->append_raw(@data); path("foo.txt")->append_utf8(@data);
Appends data to a file. The file is locked with flock prior to writing. An optional hash reference may be used to pass options. The only option is binmode, which is passed to binmode() on the handle used for writing.
binmode
binmode()
append_raw is like append with a binmode of :unix for fast, unbuffered, raw write.
append_raw
append
:unix
append_utf8 is like append with a binmode of :unix:encoding(UTF-8). If Unicode::UTF8 0.58+ is installed, a raw append will be done instead on the data encoded with Unicode::UTF8.
append_utf8
:unix:encoding(UTF-8)
Unicode::UTF8
$name = path("foo/bar.txt")->basename; # bar.txt $name = path("foo.txt")->basename('.txt'); # foo $name = path("foo.txt")->basename(qr/.txt/); # foo $name = path("foo.txt")->basename(@suffixes);
Returns the file portion or last directory portion of a path.
Given a list of suffixes as strings or regular expressions, any that match at the end of the file portion or last directory portion will be removed before the result is returned.
$canonical = path("foo/bar")->canonpath; # foo\bar on Windows
Returns a string with the canonical format of the path name for the platform. In particular, this means directory separators will be \ on Windows.
\
$file = path("/tmp")->child("foo.txt"); # "/tmp/foo.txt" $file = path("/tmp")->child(@parts);
Returns a new Path::Tiny object relative to the original. Works like catfile or catdir from File::Spec, but without caring about file or directories.
catfile
catdir
@paths = path("/tmp")->children; @paths = path("/tmp")->children( qr/\.txt$/ );
Returns a list of Path::Tiny objects for all files and directories within a directory. Excludes "." and ".." automatically.
If an optional qr// argument is provided, it only returns objects for child names that match the given regular expression. Only the base name is used for matching:
qr//
@paths = path("/tmp")->children( qr/^foo/ ); # matches children like the glob foo*
path("foo.txt")->chmod(0777); path("foo.txt")->chmod("0755"); path("foo.txt")->chmod("go-w"); path("foo.txt")->chmod("a=r,u+wx");
Sets file or directory permissions. The argument can be a numeric mode, a octal string beginning with a "0" or a limited subset of the symbolic mode use by /bin/chmod.
The symbolic mode must be a comma-delimited list of mode clauses. Clauses must match qr/\A([augo]+)([=+-])([rwx]+)\z/, which defines "who", "op" and "perms" parameters for each clause. Unlike /bin/chmod, all three parameters are required for each clause, multiple ops are not allowed and permissions stugoX are not supported. (See File::chmod for more complex needs.)
qr/\A([augo]+)([=+-])([rwx]+)\z/
stugoX
path("/tmp/foo.txt")->copy("/tmp/bar.txt");
Copies a file using File::Copy's copy function.
copy
$obj = path("/tmp/foo.txt")->digest; # SHA-256 $obj = path("/tmp/foo.txt")->digest("MD5"); # user-selected $obj = path("/tmp/foo.txt")->digest( { chunk_size => 1e6 }, "MD5" );
Returns a hexadecimal digest for a file. An optional hash reference of options may be given. The only option is chunk_size. If chunk_size is given, that many bytes will be read at a time. If not provided, the entire file will be slurped into memory to compute the digest.
chunk_size
Any subsequent arguments are passed to the constructor for Digest to select an algorithm. If no arguments are given, the default is SHA-256.
$name = path("/tmp/foo.txt")->dirname; # "/tmp/"
Returns the directory portion you would get from calling File::Spec->splitpath( $path->stringify ) or "." for a path without a parent directory portion. Because File::Spec is inconsistent, the result might or might not have a trailing slash. Because of this, this method is deprecated.
File::Spec->splitpath( $path->stringify )
"."
A better, more consistently approach is likely $path->parent->stringify, which will not have a trailing slash except for a root directory.
$path->parent->stringify
if ( path("/tmp")->exists ) { ... } # -e if ( path("/tmp")->is_dir ) { ... } # -d if ( path("/tmp")->is_file ) { ... } # -e && ! -d
Implements file test operations, this means the file or directory actually has to exist on the filesystem. Until then, it's just a path.
Note: is_file is not -f because -f is not the opposite of -d. -f means "plain file", excluding symlinks, devices, etc. that often can be read just like files.
is_file
-f
-d
Use -f instead if you really mean to check for a plain file.
$fh = path("/tmp/foo.txt")->filehandle($mode, $binmode); $fh = path("/tmp/foo.txt")->filehandle({ locked => 1 }, $mode, $binmode);
Returns an open file handle. The $mode argument must be a Perl-style read/write mode string ("<" ,">", "<<", etc.). If a $binmode is given, it is set during the open call.
$mode
$binmode
open
An optional hash reference may be used to pass options. The only option is locked. If true, handles opened for writing, appending or read-write are locked with LOCK_EX; otherwise, they are locked with LOCK_SH. When using locked, ">" or "+>" modes will delay truncation until after the lock is acquired.
locked
LOCK_EX
LOCK_SH
See openr, openw, openrw, and opena for sugar.
openr
openw
openrw
opena
if ( path("/tmp")->is_absolute ) { ... } if ( path("/tmp")->is_relative ) { ... }
Booleans for whether the path appears absolute or relative.
while ( ! $path->is_rootdir ) { $path = $path->parent; ... }
Boolean for whether the path is the root directory of the volume. I.e. the dirname is q[/] and the basename is q[].
dirname
q[/]
basename
q[]
This works even on MSWin32 with drives and UNC volumes:
MSWin32
path("C:/")->is_rootdir; # true path("//server/share/")->is_rootdir; #true
$iter = path("/tmp")->iterator( \%options );
Returns a code reference that walks a directory lazily. Each invocation returns a Path::Tiny object or undef when the iterator is exhausted.
$iter = path("/tmp")->iterator; while ( $path = $iter->() ) { ... }
The current and parent directory entries ("." and "..") will not be included.
If the recurse option is true, the iterator will walk the directory recursively, breadth-first. If the follow_symlinks option is also true, directory links will be followed recursively. There is no protection against loops when following links. If a directory is not readable, it will not be followed.
recurse
follow_symlinks
The default is the same as:
$iter = path("/tmp")->iterator( { recurse => 0, follow_symlinks => 0, } );
For a more powerful, recursive iterator with built-in loop avoidance, see Path::Iterator::Rule.
@contents = path("/tmp/foo.txt")->lines; @contents = path("/tmp/foo.txt")->lines(\%options); @contents = path("/tmp/foo.txt")->lines_raw; @contents = path("/tmp/foo.txt")->lines_utf8; @contents = path("/tmp/foo.txt")->lines( { chomp => 1, count => 4 } );
Returns a list of lines from a file. Optionally takes a hash-reference of options. Valid options are binmode, count and chomp. If binmode is provided, it will be set on the handle prior to reading. If count is provided, up to that many lines will be returned. If chomp is set, any end-of-line character sequences (CR, CRLF, or LF) will be removed from the lines returned.
count
chomp
CR
CRLF
LF
Because the return is a list, lines in scalar context will return the number of lines (and throw away the data).
lines
$number_of_lines = path("/tmp/foo.txt")->lines;
lines_raw is like lines with a binmode of :raw. We use :raw instead of :unix so PerlIO buffering can manage reading by line.
lines_raw
:raw
lines_utf8 is like lines with a binmode of :raw:encoding(UTF-8). If Unicode::UTF8 0.58+ is installed, a raw UTF-8 slurp will be done and then the lines will be split. This is actually faster than relying on :encoding(UTF-8), though a bit memory intensive. If memory use is a concern, consider openr_utf8 and iterating directly on the handle.
:raw:encoding(UTF-8)
:encoding(UTF-8)
openr_utf8
path("foo/bar/baz")->mkpath; path("foo/bar/baz")->mkpath( \%options );
Like calling make_path from File::Path. An optional hash reference is passed through to make_path. Errors will be trapped and an exception thrown. Returns the list of directories created or an empty list if the directories already exist, just like make_path.
make_path
path("foo.txt")->move("bar.txt");
Just like rename.
rename
$fh = path("foo.txt")->openr($binmode); # read $fh = path("foo.txt")->openr_raw; $fh = path("foo.txt")->openr_utf8; $fh = path("foo.txt")->openw($binmode); # write $fh = path("foo.txt")->openw_raw; $fh = path("foo.txt")->openw_utf8; $fh = path("foo.txt")->opena($binmode); # append $fh = path("foo.txt")->opena_raw; $fh = path("foo.txt")->opena_utf8; $fh = path("foo.txt")->openrw($binmode); # read/write $fh = path("foo.txt")->openrw_raw; $fh = path("foo.txt")->openrw_utf8;
Returns a file handle opened in the specified mode. The openr style methods take a single binmode argument. All of the open* methods have open*_raw and open*_utf8 equivalents that use :raw and :raw:encoding(UTF-8), respectively.
open*
open*_raw
open*_utf8
An optional hash reference may be used to pass options. The only option is locked. If true, handles opened for writing, appending or read-write are locked with LOCK_EX; otherwise, they are locked for LOCK_SH.
$fh = path("foo.txt")->openrw_utf8( { locked => 1 } );
See "filehandle" for more on locking.
$parent = path("foo/bar/baz")->parent; # foo/bar $parent = path("foo/wibble.txt")->parent; # foo $parent = path("foo/bar/baz")->parent(2); # foo
Returns a Path::Tiny object corresponding to the parent directory of the original directory or file. An optional positive integer argument is the number of parent directories upwards to return. parent by itself is equivalent to parent(1).
parent
parent(1)
$real = path("/baz/foo/../bar")->realpath; $real = path("foo/../bar")->realpath;
Returns a new Path::Tiny object with all symbolic links and upward directory parts resolved using Cwd's realpath. Compared to absolute, this is more expensive as it must actually consult the filesystem.
absolute
If the path can't be resolved (e.g. if it includes directories that don't exist), an exception will be thrown:
$real = path("doesnt_exist/foo")->realpath; # dies
$rel = path("/tmp/foo/bar")->relative("/tmp"); # foo/bar
Returns a Path::Tiny object with a relative path name. Given the trickiness of this, it's a thin wrapper around File::Spec->abs2rel().
File::Spec->abs2rel()
path("foo.txt")->remove;
Note: as of 0.012, remove only works on files.
This is just like unlink, except for its error handling: if the path does not exist, it returns false; if deleting the file fails, it throws an exception.
unlink
# directory path("foo/bar/baz")->remove_tree; path("foo/bar/baz")->remove_tree( \%options ); path("foo/bar/baz")->remove_tree( { safe => 0 } ); # force remove
Like calling remove_tree from File::Path, but defaults to safe mode. An optional hash reference is passed through to remove_tree. Errors will be trapped and an exception thrown. Returns the number of directories deleted, just like remove_tree.
remove_tree
safe
If you want to remove a directory only if it is empty, use the built-in rmdir function instead.
rmdir
rmdir path("foo/bar/baz/");
$data = path("foo.txt")->slurp; $data = path("foo.txt")->slurp( {binmode => ":raw"} ); $data = path("foo.txt")->slurp_raw; $data = path("foo.txt")->slurp_utf8;
Reads file contents into a scalar. Takes an optional hash reference may be used to pass options. The only option is binmode, which is passed to binmode() on the handle used for reading.
slurp_raw is like slurp with a binmode of :unix for a fast, unbuffered, raw read.
slurp_raw
slurp
slurp_utf8 is like slurp with a binmode of :unix:encoding(UTF-8). If Unicode::UTF8 0.58+ is installed, a raw slurp will be done instead and the result decoded with Unicode::UTF8. This is just as strict and is roughly an order of magnitude faster than using :encoding(UTF-8).
path("foo.txt")->spew(@data); path("foo.txt")->spew(\@data); path("foo.txt")->spew({binmode => ":raw"}, @data); path("foo.txt")->spew_raw(@data); path("foo.txt")->spew_utf8(@data);
Writes data to a file atomically. The file is written to a temporary file in the same directory, then renamed over the original. An optional hash reference may be used to pass options. The only option is binmode, which is passed to binmode() on the handle used for writing.
spew_raw is like spew with a binmode of :unix for a fast, unbuffered, raw write.
spew_raw
spew
spew_utf8 is like spew with a binmode of :unix:encoding(UTF-8). If Unicode::UTF8 0.58+ is installed, a raw spew will be done instead on the data encoded with Unicode::UTF8.
spew_utf8
$stat = path("foo.txt")->stat; $stat = path("/some/symlink")->lstat;
Like calling stat or lstat from File::stat.
stat
lstat
$path = path("foo.txt"); say $path->stringify; # same as "$path"
Returns a string representation of the path. Unlike canonpath, this method returns the path standardized with Unix-style / directory separators.
/
path("foo/bar")->subsumes("foo/bar/baz"); # true path("/foo/bar")->subsumes("/foo/baz"); # false
Returns true if the first path is a prefix of the second path at a directory boundary.
This does not resolve parent directory entries (..) or symlinks:
..
path("foo/bar")->subsumes("foo/bar/../baz"); # true
If such things are important to you, ensure that both paths are resolved to the filesystem with realpath:
my $p1 = path("foo/bar")->realpath; my $p2 = path("foo/bar/../baz")->realpath; if ( $p1->subsumes($p2) ) { ... }
path("foo.txt")->touch; path("foo.txt")->touch($epoch_secs);
Like the Unix touch utility. Creates the file if it doesn't exist, or else changes the modification and access times to the current time. If the first argument is the epoch seconds then it will be used.
touch
Returns the path object so it can be easily chained with spew:
path("foo.txt")->touch->spew( $content );
path("bar/baz/foo.txt")->touchpath;
Combines mkpath and touch. Creates the parent directory if it doesn't exist, before touching the file. Returns the path object like touch does.
mkpath
$vol = path("/tmp/foo.txt")->volume; # "" $vol = path("C:/tmp/foo.txt")->volume; # "C:"
Returns the volume portion of the path. This is equivalent equivalent to what File::Spec would give from splitpath and thus usually is the empty string on Unix-like operating systems or the drive letter for an absolute path on MSWin32.
splitpath
Simple usage errors will generally croak. Failures of underlying Perl unctions will be thrown as exceptions in the class Path::Tiny::Error.
Path::Tiny::Error
A Path::Tiny::Error object will be a hash reference with the following fields:
op — a description of the operation, usually function call and any extra info
op
file — the file or directory relating to the error
file
err — hold $! at the time the error was thrown
err
$!
msg — a string combining the above data and a Carp-like short stack trace
msg
Exception objects will stringify as the msg field.
If flock is not supported on a platform, it will not be used, even if locking is requested.
See additional caveats below.
On BSD, Perl's flock implementation may not work to lock files on an NFS filesystem. Path::Tiny has some heuristics to detect this and will warn once and let you continue in an unsafe mode. If you want this failure to be fatal, you can fatalize the 'flock' warnings category:
use warnings FATAL => 'flock';
AIX requires a write handle for locking. Therefore, calls that normally open a read handle and take a shared lock instead will open a read-write handle and take an exclusive lock.
All the *_utf8 methods use :encoding(UTF-8) -- either as :unix:encoding(UTF-8) (unbuffered) or :raw:encoding(UTF-8) (buffered) -- which is strict against the Unicode spec and disallows illegal Unicode codepoints or UTF-8 sequences.
Unfortunately, :encoding(UTF-8) is very, very slow. If you install Unicode::UTF8 0.58 or later, that module will be used by some *_utf8 methods to encode or decode data after a raw, binary input/output operation, which is much faster.
If you need the performance and can accept the security risk, slurp({binmode => ":unix:utf8"}) will be faster than :unix:encoding(UTF-8) (but not as fast as Unicode::UTF8).
slurp({binmode => ":unix:utf8"})
Note that the *_utf8 methods read in raw mode. There is no CRLF translation on Windows. If you must have CRLF translation, use the regular input/output methods with an appropriate binmode:
$path->spew_utf8($data); # raw $path->spew({binmode => ":encoding(UTF-8)"}, $data; # LF -> CRLF
Consider PerlIO::utf8_strict for a faster PerlIO layer alternative to :encoding(UTF-8), though it does not appear to be as fast as the Unicode::UTF8 approach.
If you have Perl 5.10 or later, file input/output methods (slurp, spew, etc.) and high-level handle opening methods ( filehandle, openr, openw, etc. ) respect default encodings set by the -C switch or lexical open settings of the caller. For UTF-8, this is almost certainly slower than using the dedicated _utf8 methods if you have Unicode::UTF8.
filehandle
-C
_utf8
A standard MooseX::Types library is available at MooseX::Types::Path::Tiny. A Type::Tiny equivalent is available as Types::Path::Tiny.
These are other file/path utilities, which may offer a different feature set than Path::Tiny.
File::chmod
File::Fu
IO::All
Path::Class
These iterators may be slightly faster than the recursive iterator in Path::Tiny:
Path::Iterator::Rule
File::Next
There are probably comparable, non-Tiny tools. Let me know if you want me to add a module to the list.
This module was featured in the 2013 Perl Advent Calendar.
Please report any bugs or feature requests through the issue tracker at https://github.com/dagolden/Path-Tiny/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/dagolden/Path-Tiny
git clone https://github.com/dagolden/Path-Tiny.git
David Golden <dagolden@cpan.org>
Chris Williams <bingos@cpan.org>
David Steinbrunner <dsteinbrunner@pobox.com>
Doug Bell <madcityzen@gmail.com>
Gabor Szabo <szabgab@cpan.org>
Gabriel Andrade <gabiruh@gmail.com>
George Hartzell <hartzell@cpan.org>
Geraud Continsouzas <geraud@scsi.nc>
Goro Fuji <gfuji@cpan.org>
Karen Etheridge <ether@cpan.org>
Martin Kjeldsen <mk@bluepipe.dk>
Michael G. Schwern <mschwern@cpan.org>
Smylers <Smylers@stripey.com>
Toby Inkster <tobyink@cpan.org>
김도형 - Keedi Kim <keedi@cpan.org>
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
To install Path::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Path::Tiny
CPAN shell
perl -MCPAN -e shell install Path::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.