Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Perl <dir_with_wildcards>

by haroyken (Initiate)
on Dec 14, 2017 at 07:36 UTC ( [id://1205508]=perlquestion: print w/replies, xml ) Need Help??

haroyken has asked for the wisdom of the Perl Monks concerning the following question:

O wise monks I beseech thy help in understanding this snippet of perl:

Basically I had files stores in multiple dir and want to assign the location of these files to an array

E.g.

/home/a1/data/loga1.txt (data also contains other files with different extension)

/home/b2/data/logb2.txt

/home/c3/data/logc3.txt

 my @array = </home/*/data/*.txt>;

does the job but I don't understand why

Normally I've seen the diamond operator with STDIN, etc. or filehandle. How does it work with a directories with wildcards like this ?

Thank you very much

Replies are listed 'Best First'.
Re: Perl <dir_with_wildcards>
by haukex (Archbishop) on Dec 14, 2017 at 07:54 UTC

    "Hidden" a bit further down under I/O Operators:

    If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context.

    This is implemented by glob and File::Glob. Note that glob has a few caveats one should be aware of: It does not list filenames beginning with a dot by default, and it splits its argument on whitespace, which may be important in case you are interpolating variables into the pattern - you can use File::Glob ':bsd_glob'; to alleviate the latter caveat. Your particular usage </home/*/data/*.txt> (or equivalently, glob('/home/*/data/*.txt')) should be fine though.

      Huh, wow, that works...

      Considering how many times I've used <> in its more "normal" usages, I'm surprised I never stumbled on this via a typo :)

      And it's been that way since (at least) 5.8.8. That's as far back as the perldoc.perl.org site goes.


      Mike
        And after getting used to mixing readline and glob behavior inside <...> , you'll probably find it more irritating that glob is also used to generate recombinations of strings without reading the file system

        If non-empty braces are the only wildcard characters used in the glob, no filenames are matched, but potentially many strings are returned. For example, this produces nine strings, one for each pairing of fruits and colors:

        my @many = glob "{apple,tomato,cherry}={green,yellow,red}";

        Reason is, Perl started as shell° scripting on steroids, it had to compete with similar glob mechanisms there.

        (I'm using this feature often, but am still irritated)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Wikisyntax for the Monastery

        °) well <{ba,k,c,}sh> scripting if you want ;)

        The globbing feature of the <> operator dates back at least as far as the Perl-5.5.* versions, as it appeared in some of my earliest Perl books (Learning Perl, 2nd Ed; Programming Perl, 2nd Ed). That takes us into the late 90's.


        Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found