Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Replies are listed 'Best First'.
Re: Perl globbing syntax with $variables?
by dave_the_m (Prior) 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.


Re: Perl globbing syntax with $variables?
by toolic (Bishop) 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:


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


    $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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2016-10-26 16:23 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (344 votes). Check out past polls.