Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

To get the pathname of the current working directory (like 'pwd' in a unix shell), use the 'cwd()' function from the module 'Cwd'.

Example:

#!/usr/bin/perl
 
use Cwd;
 
#-- get current directory
my $pwd = cwd();
 
#-- change dir to /tmp
chdir("/tmp");
 
#-- do something there
do_something();
 
#-- go back to original directory
chdir($pwd);

In Windows systems, you have a current working directory for every drive available; Cwd provides on Windows systems the function 'getdcwd()' to obtain the current working directory of any drive.

Example:

#-- get the current working directory of D drive
getdcwd('D:');


use Cwd()

When using cwd(), the a core module Cwd() is needed.

#!/usr/bin/perl
#
#-- get current directory
use Cwd();
my $pwd = cwd();

Reference http://perldoc.perl.org/Cwd.html