in reply to getting files names

Use glob or opendir and readdir to get a list of filenames, then loop through the list.

For example,

my $dir = '.' my @list = glob($dir . '/*'); # <- amended per merlyn's response
or
my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; @list = readdir($dh); closedir($dh);
will return a list of files in the current working directory. My preference is to use glob for this sort of thing. Usually, the number of files in a directory will be small (no more than a few thousand), so storing the names in an array should not be a problem. You may want to filter @list, so that it contains only the type of file you want, but this is easy using grep. Glob can process wildcards, so @list could be restricted by something like
@list = glob($dir .'/A*');

Alternatively, you could call readdir in a scalar context, and process the files one at a time. The code for this would look something like:
my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; while(readdir($dh)){ # do stuff to $_ } closedir($dh);

As I said, since the number of files in a folder tends to be fairly small, I prefer to use glob, possibly in conjunction with grep to get a list of file names, and then loop through that list.


Corrected call to glob; see merlyn's response.

emc

Insisting on perfect safety is for people who don't have the balls to live in the real world.

—Mary Shafer, NASA Dryden Flight Research Center

Replies are listed 'Best First'.
Re^2: getting files names
by merlyn (Sage) on Apr 30, 2007 at 14:47 UTC
    You said:
    my $dir = '.' my @list = glob($dir);
    But that's not going to work. Glob wants a glob pattern. As ".", there's not much of a pattern there. See some of the other answers in this thread.