use Algorithm::Combinatorics ':all'; foreach (permutations([$x, $y, $z])) { doStuff($_) if $_->[0]; }