http://qs321.pair.com?node_id=1187563

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.

    Cheers,

    JohnGG

Re: Actual use cases for non-filename behaviour of glob?
by LanX (Saint) 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 file.name{,.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.