10/6 Perl question: I want to use something like
@files = readdir(DIR);
and get the a list of files, not just the first one in DIR.
How do I tell readdir to give me the whole list, not just one?
I rtfm'ed and it only said this behavior was possible.
\_ perl works better if you don't treat it as C.
@file = readdir($DIR);
\_ perl morks better if you have a clue.
readdir takes an open FILEHANDLE, not a var.
\_ Believe it or not, with IO::Dir, it is
possible to generate anonymous references to
directory handles which you can then assign
to regular variables.
\_ why not just write a trivial function that does this (preferably
using readdir)? Also, have you considered using perl's glob()?
It could be what you need ..
\_ opendir(DIR, $somedir) || die "can't opendir $somedir: $!";
while( $name = readdir(DIR))
{
next if( $name eq ".");
next if( $name eq "..");
push( @files, $name);
}
closedir (DIR);
\_ or if what you really want is just the files in that
directory:
opendir DIR, $somedir;
@files = grep {-f} readdir DIR;
closedir DIR;
-geordan
\_ You are rad.
\_ What is that grep {-f} doing? That was what I didn't
get in the perlfunc example.
\_ It's the function for grep to check for truth. in this
case it's the "test" function, -f, which tests if the
argument is a normal file. --scotsman
\_ You could also use this to get just directories
(-d), links (-l), sockets (-S)... see the perlfunc
page for the rest of the flags. -geordan
\_ or:
@files = <$somedir/*>;
\_ even more rad.
\_ Note that this will return all entries that would be globbed
by the shell for *, including directories, links, sockets,
etc. Also I believe this actually forks off a shell process
to run the glob, so it might be slower if you care. -geordan |