fast_abs_path#

Canonicalise a path strictly: resolve symlinks and ./.., or return undef if any component is missing.

Synopsis#

use Cwd qw(fast_abs_path);
my $abs = fast_abs_path($path);
my $abs = fast_abs_path();      # defaults to current directory

What you get back#

A plain Perl string with the absolute, symlink-free path, or undef if any part of the path cannot be resolved (including the leaf). Unlike abs_path, there is no parent-plus-leaf fallback - a missing final component is treated as an error.

Examples#

Verify that a path exists and get its canonical form in one step:

use Cwd qw(fast_abs_path);
my $abs = fast_abs_path('/etc/passwd');
defined $abs or die "not found: $!";
print $abs, "\n";                 # /etc/passwd

Reject non-existent files up front:

use Cwd qw(fast_abs_path);
print defined fast_abs_path('/no/such/file') ? "ok" : "missing";
    # missing

Resolve the current directory quickly:

use Cwd qw(fast_abs_path);
print fast_abs_path(), "\n";      # equivalent to getcwd()

Edge cases#

  • No argument - the current directory.

  • A path that does not exist, including the empty string - undef, with $! set to ENOENT. Use abs_path when a missing leaf should still produce a path.

  • A directory is resolved by changing into it and back; a file by resolving its directory, following the file itself when it is a symlink.

See also#

  • abs_path - same resolution, with a parent-plus-leaf fallback

  • realpath - POSIX-style alias for abs_path

  • getcwd - absolute path of the current directory only