初めてのPerl 第4章 練習問題1
初めてのPerl 第4章 練習問題1
use strict; use warnings; use 5.010; sub total{ my @numbers = @_; my $have; foreach my $number(@numbers){ $have += $number; } return $have; } 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 number is $user_total \n";
以下、出力結果となります。
the total of @fred is 25
Enter some numbers on separate lines: 1
2
3
4
5
the total of those number is 15