1、 写一个名为total的子程序,它可以返回给定列表中数字相加的总和。
#!/usr/bin/perl
my @fred = qw/ 1 3 5 7 9 /;
my $fred_total = total (@fred);
print "The total of \@fred is $fred_total.\n";
print "Enter some numbers on separate lines: ";
my $user_total = total(<STDIN>);
print "The total of those numbers is $user_total.\n";
sub total{
my $sum;
foreach (@_){
$sum += $_ ;
}
$sum;
}
2、 使用之前程序中的子程序,计算从1加到1000的总和
#!/usr/bin/perl
print "The summary of 1 to 1000 is equal to ".total(1..1000)."\n";
sub total{
my $sum;
foreach (@_){
$sum +=