The "Perl Cookbook" is very cool! But some examples can and must be optimized :^) . This is my optimization for "permute()", from page 148. My variant ~ 3 times faster.
#!/usr/bin/perl # # variant from Cookbook # 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]); } } } # # my variant # (has different interface than permute(), see tests) # sub permute2 { my ($hold, @items) = @_; print("@items\n"), return if $hold+1==@items; foreach $i ($hold..$#items) { @items[$hold, $i] = @items[$i, $hold]; permute2($hold+1, @items); } } # # Tests # #@arr=(1..8); @arr=split " ",<>; permute([@arr],[]); print "---\n"; permute2(0, @arr);