home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

7.2.7 Cwd - Get Pathname of Current Working Directory

use Cwd;
$dir = cwd();           # get current working directory safest way

$dir = getcwd();        # like getcwd(3) or getwd(3)

$dir = fastcwd();       # faster and more dangerous

use Cwd 'chdir';        # override chdir; keep PWD up to date
chdir "/tmp";
print $ENV{PWD};        # prints "/tmp"

cwd() gets the current working directory using the most natural and safest form for the current architecture. For most systems it is identical to `pwd` (but without the trailing line terminator).

getcwd() does the same thing by re-implementing getcwd (3) or getwd (3) in Perl.

fastcwd() looks the same as getcwd() , but runs faster. It's also more dangerous because you might chdir out of a directory that you can't chdir back into.

It is recommended that one of these functions be used in all code to ensure portability because the pwd program probably only exists on UNIX systems.

If you consistently override your chdir built-in function in all packages of your program, then your PWD environment variable will automatically be kept up to date. Otherwise, you shouldn't rely on it. (Which means you probably shouldn't rely on it.)

Previous: 7.2.6 Config - Access Perl Configuration Information Programming Perl Next: 7.2.8 DB_File - Access to Berkeley DB
7.2.6 Config - Access Perl Configuration Information Book Index 7.2.8 DB_File - Access to Berkeley DB