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

Book Home Programming PerlSearch this book

32.9. Cwd

use Cwd;
$dir = getcwd();    # Where am I?

use Cwd 'chdir';
chdir "/tmp";       # Updates $ENV{PWD}.

use Cwd 'realpath';
print realpath("/usr////spool//mqueue/../");  # prints /var/spool

The Cwd module provides platform-independent functions to determine your process's current working directory. This is better than shelling out to pwd(1) because non-POSIX-conforming systems aren't guaranteed to have such a command, and Perl runs on more than just POSIX platforms. The getcwd function, which is exported by default, returns the current working directory using whatever mechanism is deemed safest on the current platform. If you import the chdir function, it overrides the built-in operator with the module's operator, which maintains the $ENV{PWD} environment variable; commands you might launch later that would care about that variable would then have a consistent view of their world. The realpath function resolves its pathname argument of any symbolic links and relative-path components to return a full path directory in canonical form, just like realpath(3).

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.