Think of a set of text lines starting with a common string, e.g. indented code. my $preamble = reduce { my $len = min(length $a, length $b); --$len while substr($a, 0, $len) ne substr($b, 0, $len); return substr($a, 0, $len); } @lines; Note that after the first invocation chances are high that `$a` already has the final result. I'm wondering if there is a better approach in comparing the two strings in the reduce block. The while loop does not seem to be the best approach. It's also less readable since it does not convey the intent of the code (find common preamble of `$a` and `$b`).