use strict; use warnings; print join "\n", comb('A'..'C'); BEGIN { my @c_out; sub comb { @c_out = (); permute('', $_, @_) for (0..$#_); return @c_out; } sub permute { my ($str, $depth, @chars) = @_; if (!$depth--) { push @c_out, $str.$_ for @chars; } else { permute($str.$chars[$_], $depth, @chars[($_+1)..($#chars)]) for (0..$#chars); } } }