Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl globbing syntax with $variables?

by shadowfox (Beadle)
on Aug 01, 2012 at 20:02 UTC ( [id://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 (Monsignor) 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 (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:

    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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found