use strict; use warnings; my @input = (qw/a b c d/); permutation('', @input); sub permutation { my ($perm, @set) = @_; if (!@set) { print "$perm\n"; return; } for my $partition (0 .. $#set) { my $firstEnd = $partition - 1; my $lastStart = $partition + 1; my @newSet = @set[0 .. $firstEnd]; push @newSet, @set[$lastStart .. $#set]; permutation($perm . $set[$partition], @newSet); } }