O'Reilly logo
  • Michael Schreckengost thinks this is interesting:

sub max ( $max_so_far, @rest ) { foreach (@rest) { if ($_ > $max_so_far) { $max_so_far = $_; } } $max_so_far; }


Cover of Learning Perl, 7th Edition


This subroutine, as written, does not handle negative values. To fix, add the following statement before the foreach loop:
$max_so_far = shift(@rest);
This initializes the high water mark to the first element in the array prior to starting the loop.