#! perl -slw use strict; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CODE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } our $TMPL //= '{a|b}cd{f|g|h}'; my @as = map { /\|/ ? [ split '\|', $_ ] : length() ? [$_] : (); } split '\{([^}]+)\}', $TMPL; nFor { print join '', map $as[ $_ ][ $_[ $_ ] ], 0 .. $#_ } map scalar @$_, @as; __END__ [ 1:50:21.76] C:\test>junk43 -TMPL="{a|b}def{g|h}j{K|l}m{n|o}p{q|r}" adefgjKmnpq adefgjKmnpr adefgjKmopq adefgjKmopr adefgjlmnpq adefgjlmnpr adefgjlmopq adefgjlmopr adefhjKmnpq adefhjKmnpr adefhjKmopq adefhjKmopr adefhjlmnpq adefhjlmnpr adefhjlmopq adefhjlmopr bdefgjKmnpq bdefgjKmnpr bdefgjKmopq bdefgjKmopr bdefgjlmnpq bdefgjlmnpr bdefgjlmopq bdefgjlmopr bdefhjKmnpq bdefhjKmnpr bdefhjKmopq bdefhjKmopr bdefhjlmnpq bdefhjlmnpr bdefhjlmopq bdefhjlmopr