As of now, I know of two ways to open and read a directory in Perl. You can use opendir
, readdir
and closedir
or you can simply use glob
to get the contents of the directory.
Example:
Using opendir
, readdir
and closedir
:
opendir my $dh, $directory;
my @contents = readdir $dh;
closedir $dh;
Using glob
:
my @contents = <$directory/*>;
I have been informed that in some situations the glob
method can produce unpredictable results (for example it can act differently on different operating systems when it encounters special characters in directory names).
The thing I love about the glob
method is how "quick and dirty" it is. It's one simple line and it gets the job done, but if it doesn't work in all situations that can introduce unexpected and difficult to find bugs.
I was wondering if there was a sort of "shorthand" way to use the
opendir
,readdir
,closedir
method?
Maybe something like this fancy method for slurping a file in one line I recently discovered.