ecuguru has asked for the wisdom of the Perl Monks concerning the following question:

#Works my @allfiles = </test/*.txt>; foreach my $item (@allfiles) { #Fails my $dir = '/test/*.txt'; my @allfiles = <$dir>; foreach my $item (@allfiles) {
$dir correctly prints out /test/*.*
My For Each loop will fail if I try to assign allfiles to a scalar.
My for Each loop works great if I hard code the directory location.

Any idea how to pass a scalar as a directory, rather than hard coding?
Thanks!

disclaimer: I could have sworn that I posted this last week, and it's not here. So if I posted this as anon, I can't find it. If someone saw this and can point to the solution, many many thanks.

Replies are listed 'Best First'.
Re: Directory location from scalar
by Zaxo (Archbishop) on Aug 29, 2005 at 05:56 UTC
    I could have sworn that I posted this last week, . . .

    You did, Pass variable as Directory.

    The <> form of wildcard expansion has semantic weaknesses, taking $dir as a reference to a filehandle. Use glob instead. my @allfiles = glob $dir;

    After Compline,
    Zaxo

Re: Directory location from scalar
by sk (Curate) on Aug 29, 2005 at 05:54 UTC
Re: Directory location from scalar
by Roger (Parson) on Aug 29, 2005 at 08:52 UTC
    Or you could use the module File::Find. Which I consider to be more handy than glob's or <>.