So, if we wanted only the pm-ending files, we
could use a skip-over function inside the loop:
while ($name = readdir DIR) {
next unless $name =~ /\.pm$/;
... more processing ...
}
Note here that the syntax is that of a regular expression, not a
glob. And if we wanted all the non-dot files, we could say that:
next if $name =~ /^\./;
Or if we wanted everything but the common dot (current directory) and
dot-dot (parent directory) entries, we could explicitly say that:
next if $name eq "." or $name eq "..";
Now we'll look at the part that gets most people mixed up, so
pay close attention. The filenames returned by the
readdir operator have no
pathname component. It's just the name
within the directory. So, we're not looking at
/etc/passwd, we're just looking at
passwd. (And because this is another difference
from the globbing operation, it's easy to see how people get
confused.)
So you'll need to patch up the name to get the full name:
opendir SOMEDIR, $dirname or die "Cannot open $dirname: $!";
while (my $name = readdir SOMEDIR) {
next if $name =~ /^\./; # skip over dot files
$name = "$dirname/$name"; # patch up the path
next unless -f $name and -r $name; # only readable files
...
}