Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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... }

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.

    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
Node Status?
node history
Node Type: perlquestion [id://984884]
Approved by marto
Front-paged by Corion
help
Chatterbox?
[stevieb]: my RPi::WiringPi now fully supports: GPIO, LCDs, digital potentiometers, analog to digital converters, shift registers, barometric pressure sensor, direct access to SPI bus, and ability for more :)
[stevieb]: ...just need to perform many tests and POD review this week, then I'm going to put together some real projects and post about them

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-01-22 22:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (190 votes). Check out past polls.