SPUG: Re: How to get pwd (current dir name)?
Jeremy Mates
jmates at sial.org
Wed Jan 1 20:28:06 CST 2003
* 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.
sub print_file_type {
my $file = shift;
my %st_mode_map = (
'010000' => 'named pipe (fifo)',
'020000' => 'character special',
'040000' => 'directory',
'060000' => 'block special',
'100000' => 'file',
'120000' => 'link',
'140000' => 'socket',
'160000' => 'whiteout'
);
my $mode = (lstat($file))[2];
my $type = sprintf("%06o", $mode & 0170000);
my $humantype = $st_mode_map{$type};
print "$file $humantype $type $mode\n";
}
--
Jeremy Mates http://www.sial.org/
OpenPGP: 0x11C3D628 (4357 1D47 FF78 24BB 0FBF 7AA8 A846 9F86 11C3 D628)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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