#! perl -slw use strict; sub permutations { return !@_ ? [] : map{ my( $elem, $n ) = ( $_, 1 ); map{ [ $elem, @$_ ] } permutations( grep{ !/$elem/ or !$n-- } @_ ); } @_; } print @$_ for permutations @ARGV;