Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: and this or that

by dws (Chancellor)
on Feb 05, 2001 at 23:40 UTC ( [id://56485]=note: print w/replies, xml ) Need Help??


in reply to and this or that

Your precedence problem goes away with a little untangling. Move the error handling for opendir() in closer to the call, rather than out past the readdir(). Here's I'd code it.
for my $next (@ARGV){ opendir(PWD, "$next") or die "$next: $!"; my @files = readdir(PWD) or die "$next: $!"; closedir(PWD); print join("\n", @files), "\n"; }
(Updated to add error handling after readdir(), too.)

Or, if you really want to use precedence, and if you want to ignore non-directories in @ARGV, consider

for my $next (@ARGV) { opendir(PWD, "$next") and do { my @files = readdir(PWD) or die "$next: $!"; closedir(PWD); print join("\n", @files), "\n"; } }

Replies are listed 'Best First'.
Re: Re: and this or that
by Fastolfe (Vicar) on Feb 06, 2001 at 01:01 UTC
    for my $next (@ARGV) { opendir(PWD, "$next") and do { my @files = readdir(PWD) or die "$next: $!"; closedir(PWD); print join("\n", @files), "\n"; } }
    TIMTOWDI. I personally find this a little clearer, and is no less idiomatic:
    foreach my $next (@ARGV) { if (opendir(PWD, "$next")) { my @files = readdir(PWD) or die "$next: $!"; closedir(PWD); print join("\n", @files), "\n"; } }
    While using and and do like that is only slightly less clear than using a true 'if' block, it is slightly less clear than using a true 'if' block. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found