Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange things inside Perl diamond operator

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?

like image 484
alexei guirik Avatar asked Oct 24 '11 17:10

alexei guirik


1 Answers

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}";
like image 198
Zaid Avatar answered Sep 24 '22 23:09

Zaid