if (my @capt = $string =~ /(foo)|(bar)/) { $N = @capt; for (reverse @capt) { last if defined; $N--; } if ($N == 1) { ... } else { ... } }