Your program has a bug. Had you enabled warnings, you would have found out why. You never initialize $max, which means that in numeric context, $max is treated as if 0. Which means that if all your values are negative, at the end of the procedure, $max is never assigned a new value and still undefined.