DESCRIPTION

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?)

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).

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.

CONSTRUCTORS

path

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.

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.

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.

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().

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.

tempfile, tempdir

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.

The resulting File::Temp object is cached. When the Path::Tiny object is destroyed, the File::Temp object will be as well.

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.

METHODS

absolute

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.

On Windows, an absolute path without a volume component will have it added based on the current drive.

append, append_raw, append_utf8

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.

append_raw is like append with a binmode of :unix for fast, unbuffered, raw write.

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.

chmod

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.)

digest

Returns a hexadecimal digest for a file. Any arguments are passed to the constructor for Digest to select an algorithm. If no arguments are given, the default is SHA-256.

dirname

$name = path("/tmp/foo.txt")->dirname; # "/tmp/"

Returns the directory name portion of the path. This is roughly equivalent to what File::Spec would give from splitpath and thus usually has the trailing slash. If that's not desired, stringify directories or call parent on files.

filehandle

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.

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.

iterator

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.

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.

Because the return is a list, lines in scalar context will return the number of lines (and throw away the data).

$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_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.

mkpath

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.

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.

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.

parent

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).

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.

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

relative

$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().

remove

path("foo.txt")->remove;

Note: as of 0.012, remove only works on files.

This is just like unlink, except if the path does not exist, it returns false rather than throwing an exception.

remove_tree

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.

If you want to remove a directory only if it is empty, use the built-in rmdir function instead.

slurp, slurp_raw, 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_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).

spew, spew_raw, spew_utf8

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_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.

touch

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.

Returns the path object so it can be easily chained with spew:

path("foo.txt")->touch->spew( $content );

touchpath

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.

volume

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.

EXCEPTION HANDLING

Simple usage errors will generally croak. Failures of underlying Perl unctions will be thrown as exceptions in the class 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

file — the file or directory relating to the error

err — hold $! at the time the error was thrown

msg — a string combining the above data and a Carp-like short stack trace

Exception objects will stringify as the msg field.

CAVEATS

File locking

If flock is not supported on a platform, it will not be used, even if locking is requested.

See additional caveats below.

NFS and BSD

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 and locking

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.

utf8 vs UTF-8

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).

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:

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.

Default IO layers and the open pragma

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.