SPUG: How to get pwd (current dir name)?

Brian Hatch spug at ifokr.org
Wed Jan 1 21:31:06 CST 2003

> How may I get the name of the present working directory within Perl,
> similar to the unix `pwd` command?

If you are a fan of backticks:

	my $pwd = `pwd`;
	chomp $pwd;

But as meantioned, Cwd module is a better choice.

> Also, how may I use the mode field from the stat($filename) function
> to test if a file is a directory, similar to -d $filename?

	$blah = stat($filename);
	# do stuff with $blah

	if ( -d _ ) {

The underscore, when used as an argument of '-d' and friends,
means use most recent stat() values, rather than re-stating
them.  So as long as you do no stat's between the $blah
line and the if line, _ will hold the value associated with
$filename.  No addl stat or mode AND (&) mathematics needed.

Brian Hatch                  "'Welcome to Babylon 5, the
   Systems and                last best hope for a quick buck.'"
   Security Engineer

Every message PGP signed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
Url : http://mail.pm.org/archives/spug-list/attachments/20030101/c9c255ab/attachment.bin

More information about the spug-list mailing list