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";
}
}