perlでpermutation

「Perl クックブック」より。

#!/usr/bin/perl

permute([qw(one two three four)],[]);

sub permute {
my @items = @{ $_[0] };
my @perms = @{ $_[1] };

unless (@items) {
print "@perms\n";
}else{
my(@newitems, @newperms, $i);
foreach $i (0 .. $#items) {
@newitems = @items;
@newperms = @perms;
unshift(@newperms, splice(@newitems, $i, 1));
permute([@newitems], [@newperms]);
}
}
}


実行結果

# perl permute.pl
four three two one
three four two one
four two three one
two four three one
three two four one
two three four one
four three one two
three four one two
four one three two
one four three two
three one four two
one three four two
four two one three
two four one three
four one two three
one four two three
two one four three
one two four three
three two one four
two three one four
three one two four
one three two four
two one three four
one two three four


なるほど・・・が、ソースの意味がほとんどわからない・・・。