February 2012
Intermediate to advanced
1184 pages
37h 17m
English
readdir DIRHANDLEThis function reads directory entries (which are simple
filenames) from a directory handle opened by opendir. In scalar context, this function
returns the next directory entry, if any; otherwise, it returns undef. In list context, it returns all the
rest of the entries in the directory, which will be a null list if there
are no entries. For example:
opendir(THISDIR, ".") || die "serious dainbramage: $!"; @allfiles = readdir THISDIR; closedir THISDIR; say "@allfiles";
That prints all files in the current directory on one line. If you
want to avoid the “.” and “..” entries, incant one of these (whichever
you think is least unreadable):
@allfiles = grep { $_ ne "." && $_ ne ".." } readdir THISDIR;
@allfiles = grep { ! /^[.][.]?\z/ } readdir THISDIR;
@allfiles = grep { ! /^\.{1,2}\z/ } readdir THISDIR;
@allfiles = grep !/^\.\.?\z/, readdir THISDIR;And to avoid all .* files (like
the ls program):
@allfiles = grep !/^\./, readdir THISDIR;
To get just text files, say this:
@textfiles = grep –T, readdir THISDIR;
But watch out on that last one because the result of readdir ...
Read now
Unlock full access