Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Directory location from scalar

by ecuguru (Monk)
on Aug 29, 2005 at 05:50 UTC ( [id://487333]=perlquestion: print w/replies, xml ) Need Help??

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 <>.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://487333]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-19 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found