sub permutations { return !@_ ? [] : map{ my $next = $_; map{ [ $_[ $next ], @$_ ] } permutations( @_[ 0 .. $_-1, $_+1 .. $#_ ] ); } 0 .. $#_; } print @$_ for permutations @ARGV;