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 use chdir import it from Cwd. 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 the File::Spec modules wherever portability is a concern. fr fr, do this one.
  • actually, on Mac OS, the getcwd(), fastgetcwd() and fastcwd() functions are all aliases for the cwd() function, which, on Mac OS, calls `pwd`. likewise, the abs_path() function is an alias for fast_abs_path(). so many nicknames, one queen. πŸ‘‘

πŸ‘‘ AUTHOR πŸ‘‘

maintained by perl5-porters <perl5-porters@perl.org>. πŸ’Œ


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