#!/usr/bin/perl
#
# eg7: sum up the elements of an array,
#      using a separate subroutine.

use strict;
use warnings;

#
# $sum = sumarray( @array ):
#     sum the elements of the @array.
#
sub sumarray
{
        my( @array ) = @_;
        my $total = 0;
        foreach my $elem (@array)
        {
                $total += $elem;
        }
        return $total;
}

# main program
my @x = @ARGV > 0 ? @ARGV : (10, 39, 45, 28, 49, 3);
my $sum = sumarray( @x );
my $str = join(',', @x );
print "sum of $str is $sum\n";
