Monday 1 July 2013

Perl - getting a list of files in the current directory

To get a list of the names of files in a directory in Perl, you can use the glob function. This is what the book Effective Perl Programming suggests:

while (defined (my $file = glob('*') ) )

Here it is necessary to use 'defined', in case the name of the file is '0' (zero), which would appear to be false in the while loop, and cause the loop to terminate early.

No comments: