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

Perl globbing syntax with $variables?

by shadowfox (Sexton)
on Aug 01, 2012 at 20:02 UTC ( #984884=perlquestion: print w/ replies, xml ) Need Help??
shadowfox has asked for the wisdom of the Perl Monks concerning the following question:

This first part works fine, but I prefer to store paths in variables at the top of my script, thus the second part, is the syntax wrong or is it not possible in this context? I did search but I couldn't find any revelant code examples or obvious reasons as to why it isn't working. I'm just globbing all matching files from the directory specified and doing various stuff with them, but it bugs me that I don't see whats wrong, I did also try escaping it <\$WantedFiles>
@FILES = <//server/path/WantedFiles*.*> ; foreach $file(@FILES){ #do stuff... }
So why doesn't this?
$WantedFiles = "//server/path/WantedFiles*.*"; @FILES = <$WantedFiles> ; foreach $file(@FILES){ #do stuff... }

Comment on Perl globbing syntax with $variables?
Select or Download Code
Re: Perl globbing syntax with $variables?
by dave_the_m (Parson) on Aug 01, 2012 at 20:23 UTC
    @FILES = glob $WantedFiles;
    The diamond operator is dual-purpose - both read a line from a filehandle, and expand a file wildcard list. Where it's ambiguous which form you want, you have to tell perl explicitly.

    Dave.

Re: Perl globbing syntax with $variables?
by toolic (Chancellor) on Aug 01, 2012 at 20:23 UTC

    B::Deparse (Tip #6 from the Basic debugging checklist) shows that <> in your 1st example is indeed a glob, but <> in the 2nd example is not a glob:

    1st:

    use File::Glob (); use warnings; @FILES = glob('//server/path/WantedFiles*.*'); foreach $file (@FILES) { (); }

    2nd:

    $WantedFiles = '//server/path/WantedFiles*.*'; @FILES = <$WantedFiles>; foreach $file (@FILES) { (); }

    I/O Operators

Re: Perl globbing syntax with $variables?
by abualiga (Scribe) on Aug 01, 2012 at 20:41 UTC

    I think the problem is in '*.*'. Perhaps this would work better:

    $WantedFiles = '//server/path/WantedFiles'; @files = <$WantedFiles/*>;

    If you also wanted the dot files from that directory, then:

    @files = <$WantedFiles/* $WantedFiles/.*>;

    Perl expands variables to current values before globbing them (Learning Perl).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984884]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-04-16 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (419 votes), past polls