Brace expansion is a feature of glob that generates multiple strings by expanding expressions enclosed in {}.
my @list = glob "{red,green,blue}"; # ("red", "green", "blue")
my @files = glob "file.{txt,pdf,doc}"; # e.g. ("file.txt", "file.pdf", "file.doc")
my @names = glob "{Mr,Mrs,Dr}.Smith"; # ("Mr.Smith", "Mrs.Smith", "Dr.Smith")
my @list = glob "{A,B}{1,2}"; # ("A1", "A2", "B1", "B2")
my @list = glob "{A,B}{1,2}{x,y}"; # ("A1x", "A1y", "A2x", "A2y", "B1x", "B1y", "B2x", "B2y")
my @bits = glob "{0,1}{0,1}{0,1}"; # ("000", "001", "010", "011", "100", "101", "110", "111")
my @words = glob "{big,small}{cat,dog}"; # ("bigcat", "bigdog", "smallcat", "smalldog")
my @list = glob "{,un}happy"; # ("happy", "unhappy") my @list = glob "colo{,u}r";# ("color", "colour")
my @files = glob "*.{pl,pm}"; # e.g. ("main.pl", "test.pl", "main.pm", "test.pm") my @files = glob("*.txt");# or: my @files = <*.txt>;
# in scalar context, glob returns one match foreach my $file (glob("*.pl")) { print "Processing $file\n"; }
| Expression | Expands to |
|---|---|
{A,B} |
("A", "B") |
{1,2,3} |
("1", "2", "3") |
{A,B}{1,2} |
("A1", "A2", "B1", "B2") |
{0,1}{0,1} |
("00", "01", "10", "11") |
{,u} |
("", "u") |
*.{pl,pm} |
.pl and .pm files in the current directory. |