Cwd(3perl)
π Cwd β get pathname of current working directory π
okay bestie, welcome to Cwd (that's part of the Perl Programmers Reference Guide, for the record), the module that tells you where you are. like, literally, on the filesystem. it's giving GPS but for your process. πΊοΈβ¨
β¨ SYNOPSIS β¨
use Cwd;
my $dir = getcwd;
use Cwd 'abs_path';
my $abs_path = abs_path($file);
like two lines and you're already slaying. π
πΈ DESCRIPTION πΈ
so this module hands you functions that figure out the pathname of the current working directory. and ngl, the recommendation here is real: use getcwd (or one of its other *cwd() cousins) in all your code so everything stays portable. no cap.
by default, it exports cwd(), getcwd(), fastcwd(), and fastgetcwd() straight into the caller's namespace. and on Win32? you also get getdcwd(). π
π getcwd and friends
every single one of these gets called with no arguments and returns the absolute path of the current working directory. iconic behaviour honestly.
getcwd
my $cwd = getcwd();
returns the current working directory. if something goes wrong it returns "undef", and $! gets set to tell you what the error was. π
it exposes the POSIX function getcwd(3), or re-implements it if that's not available. resourceful queen.
cwd
my $cwd = cwd();
cwd() is the most natural form for whatever architecture you're on. for most systems it's identical to `pwd` β except with no trailing line terminator. tiny detail, big difference. π
fastcwd
my $cwd = fastcwd();
okay so this one is a more dangerous version of getcwd(), but potentially faster. β οΈ
it might conceivably chdir() you out of a directory that it can't chdir() you back into. that's the risk, bestie. if fastcwd hits a problem it'll return undef, but it will probably leave you in a different directory. yikes.
for a measure of extra security, if everything appears to have worked, the fastcwd() function checks that it leaves you in the same directory it started in. if it has changed, it will "die" with the message "Unstable directory path, current directory changed unexpectedly". that should never happen. π
fastgetcwd
my $cwd = fastgetcwd();
fastgetcwd() is provided as a synonym for cwd(). that's it, that's the whole job. π
getdcwd
my $cwd = getdcwd();
my $cwd = getdcwd('C:');
getdcwd() is also provided on Win32, so you can get the current working directory on a specified drive β because Windows keeps a separate current working directory for each drive. messy of them, but okay. if you don't specify a drive, the current drive is assumed.
this function simply calls the Microsoft C library _getdcwd() function. no extra drama, just a handoff. π€
π¦ abs_path and friends
these ones are exported only on request. they each take a single argument and return the absolute pathname for it. and if you give them no argument at all, they'll just use the current working directory. flexible. π
abs_path
my $abs_path = abs_path($file);
uses the same algorithm as getcwd(). symbolic links and relative-path components ("." and "..") get resolved so you get the canonical pathname back, just like realpath(3). on error it returns "undef", with $! set to indicate the error. β¨
realpath
my $abs_path = realpath($file);
a synonym for abs_path(). same energy, different name. π«Ά
fast_abs_path
my $abs_path = fast_abs_path($file);
a more dangerous, but potentially faster version of abs_path. β οΈ choose wisely.
π $ENV{PWD}
if you ask to override your chdir() built-in function, like so:
use Cwd qw(chdir);
then your PWD environment variable will be kept up to date. π
heads up though: it will only be kept up to date if all packages which usechdirimport it fromCwd. one stray renegade package and the whole vibe is off. π
π NOTES π
- since path separators are different on some operating systems (
'/'on Unix,':'on MacPerl, etc...) we recommend you use theFile::Specmodules wherever portability is a concern. fr fr, do this one. - actually, on Mac OS, the
getcwd(),fastgetcwd()andfastcwd()functions are all aliases for thecwd()function, which, on Mac OS, calls`pwd`. likewise, theabs_path()function is an alias forfast_abs_path(). so many nicknames, one queen. π
π AUTHOR π
maintained by perl5-porters <perl5-porters@perl.org>. π
Β©οΈ COPYRIGHT Β©οΈ
copyright (c) 2004 by the Perl 5 Porters. all rights reserved.
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
portions of the C code in this library are copyright (c) 1994 by the Regents of the University of California. all rights reserved. the license on this code is compatible with the licensing of the rest of the distribution β please see the source code in Cwd.xs for the details. π
π SEE ALSO π
File::chdir
okay that's the whole page, bestie, you're officially a Cwd expert now π§β¨ this was perl v5.44.0, dated 2026-10-17, and it's giving Cwd(3perl) β go get yourself an absolute path and take up space. ππ¦πΈ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms