Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: if (-d$_) failes because of white space

by almut (Canon)
on May 13, 2009 at 19:05 UTC ( [id://763884]=note: print w/replies, xml ) Need Help??


in reply to Re: if (-d$_) failes because of white space
in thread if (-d$_) failes because of white space

opendir DIR,$dir or die "Can't open directory.\n"; @dir = readdir DIR; ... foreach(@dir) { if (-d$_) {print $_," is a directory.\n";} elsif (-f$_) {print $_," is a file.\n";} else {print $_," is not a file or a directory!\n";} }

readdir() gives you directory entries without the path component. So you have to either chdir() into the directory before your foreach loop, or write

foreach(@dir) { my $path = "$dir/$_"; # prepend dir if (-d $path) {print $path," is a directory.\n";} elsif (-f $path) {print $path," is a file.\n";} else {print $path," is not a file or a directory!\n";} }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://763884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-16 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found