# a subroutine that sums the elements in an array
# and returns the sum
sub sum_array {
my(@vals) = @_; # put parameters in array @vals
my($sum) = 0; # initialize the sum to 0
# $sum is private so we don't need to worry about
# clobbering any global variable named $sum!
foreach $i (@vals) {
$sum = $sum + $i;
}
return($sum);
}
# main program
print "Enter a bunch of numbers on a line\n";
$_ = ;
@nums = split;
print "The sum is ", &sum_array(@nums), "\n";