use Algorithm::Combinatorics qw[ permutations ]; my $iter = permutations( [ reverse 0 .. 9 ] ); print join '', @$_ while defined( $_ = $iter->next ); 9876543210 9876543201 9876543120 9876543102 9876543021 9876543012 9876542310 9876542301 9876542130 9876542103 9876542031 9876542013 9876541320 ...