Strange things inside Perl diamond operator

448 views Asked by At

Please, can anyone help me with this:

perl -e 'print for <{a,b,c}{1,2,3}>'

I just don't understand how it works. And it works! Producing

a1a2a3b1b2b3c1c2c3

on output.

Does anyone know what is happening inside diamond operator?

1

There are 1 answers

1
Zaid On BEST ANSWER

It's another way to represent globbing. Basically the curlies tell the glob operator to take each comma-separated element inside and combine across all possibilities.

A clearer way to see this is to comma-separate the individual outputs:

$ perl -e 'print join ",", <{a,b,c}{1,2,3}>;'
a1,a2,a3,b1,b2,b3,c1,c2,c3

From perldoc -f glob :

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:

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