#!/usr/bin/perl -n
# permute - tchrist@perl.com
permut([split], []);
sub permut {
my @head = @{ $_[0] };
my @tail = @{ $_[1] };
unless (@head) {
# stop recursing when there are no elements in the head
print "@tail\n";
} else {
# for all elements in @head, move one from @head to @tail
# and call permut() on the new @head and @tail
my(@newhead,@newtail,$i);
foreach $i (0 .. $#head) {
@newhead = @head;
@newtail = @tail;
unshift(@newtail, splice(@newhead, $i, 1));
permut([@newhead], [@newtail]);
}
}
}
