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

Yitzchak Scott-Thoennes sthoenna at efn.org
Wed Jan 1 21:31:59 CST 2003


On Wed, 1 Jan 2003 18:28:06 -0800, jmates at sial.org wrote:
>* Sanford Morton <smorton at pobox.com> [2003-01-01T16:58-0800]:
>> How may I get the name of the present working directory within Perl,
>> similar to the unix `pwd` command?
>
>perldoc Cwd
>
>> 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?
>
>There are probably modules, or one can drag the information from
>stat(2) kicking and screaming.  Figuratively.

Instead, you can just do a "-d _" after you do the stat.  "_" is a special
"file handle" that checks the info from the most recent stat, lstat, or
file test operator.

Example:
~/bleadperl/perl/t $perl -wle'
> for (<*>) {
>    $uid = (stat $_)[4];
>    if (-d _) {
>       print "dir:  $_";
>    } else {
>       print "file: $_ (uid: $uid)";
>    }
> }'
dir:  base
dir:  cmd
dir:  comp
file: harness (uid: 500)
dir:  io
dir:  japh
dir:  lib
dir:  op
dir:  pod
file: README (uid: 500)
dir:  run
file: TEST (uid: 500)
file: test.pl (uid: 500)
file: TestInit.pm (uid: 500)
dir:  uni
dir:  win32
dir:  x2p

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list