http://qs321.pair.com?node_id=11124863


in reply to Please help me understand this permutation sub?

Your sub is confusing. Note that the return statement is never executed. (You can replace it with die.) The code requires you to know exactly what happens when @set becomes empty. Are you sure you know the values of the special variable $#set, the range operator 0..$#set, and how foreach handles that? The code below avoids these issues, but is equivalent. At first, my logic appears more complicated, but I think you will find it easier to follow. Add the same debugging if you wish. Once you see what is going on, you should be able to see how the terse original is really the same thing.
use strict; use warnings; sub permutation { my ($perm,@set) = @_; unless(@set) { print "$perm\n" || die("Print error:$!"); } else { permutation($perm.$set[$_], (@set[0..$_-1],@set[$_+1..$#set]) ) foreach (0..$#set); } return; } my @input = (qw/a b c d/); permutation('',@input);

UPDATE: I now believe that you original code had a serious error and it was only by pure luck that it worked at all. The '||' operator before the 'return' should be an 'and' operator. With this change, function appears to work exactly the same. The difference is that now, the 'return' is executed immediately after every print. The behavior of 'foreach' in this special case is no longer an issue. Note that the lower priority operator 'and' instead of '&&' is needed to make the string bind to the print rather than the '&&'. The reverse logic is needed because print returns a true value on success. I have never seen a failure.

Bill