Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Actual use cases for non-filename behaviour of glob?

by vrk (Chaplain)
on Apr 10, 2017 at 13:36 UTC ( #1187563=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

I was reading the manual entry for glob the other day, and discovered a feature I hadn't used before: brace-only "wildcard" expressions can be used to generate strings with no globbing behaviour (that is, no matching against filenames in the current directory). The manual gives one example:

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

It's closely related to the Perl 6 "X" operator, but limited to string elements only.

It's fun to try different ways to generate all pairs, triples or tuples of some length. For example, the following prints the coordinates of the standard chessboard:

perl -E '$" = ","; my @pos = 'a'..'h'; say for <{@pos}{@{[ 1..@pos ]}} +>'

Other than toy examples, have you used this kind of a glob in production code? What kind of non-trivial use cases can you suggest?

Replies are listed 'Best First'.
Re: Actual use cases for non-filename behaviour of glob?
by johngg (Canon) on Apr 10, 2017 at 22:34 UTC

    I don't know if this was going into production code as I posted it in response to a supplementary question in this bio/dna thread. Building a string to use with glob seemed to me to be the most effective way to generate the series of 10-base strings according to the rules set out.



Re: Actual use cases for non-filename behaviour of glob?
by LanX (Sage) on Apr 10, 2017 at 14:39 UTC
    > non-filename behaviour of glob

    That's probably a misunderstanding, it's directly related to the behaviour of file-globs in Unix shells

    Try something like ls *.{txt,bak} to see what I mean. (untested b/c on Win)

    I used it sometimes in production code, and we already had several questions asking for easy permutation syntax.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      I'm not sure where the misunderstanding is. I often use the set (brace) notation in bash, e.g. when temporarily renaming files (mv{,.old}). The question isn't about the glob syntax -- it's about being able to produce strings from a glob call that have nothing to do with files in the current directory.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2023-02-05 14:55 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.