- 论坛徽章:
- 0
|
我正在学Perl,看的是第四版的< erl语言入门> ;
今天做了一个练习,感觉有点不对劲,是第四章的第一个练习,题目如下:
写一个名为&total 的子程序,返回一列数字的和.
我的代码如下:- #!/usr/bin/perl -w
- #chapter 4 exercise 1
- sub total{
- my $total=0;
- chomp(@_);
- foreach (@_){
- $total+=$_;
- }
- $total;
- }
- my @terry = qw ( 1 2 3 4 5 );
- $count=total(@terry);
- print "the total of \@terry is $count\n";
- print "==============\n";
- my $user_stdin=total(<stdin>);
- print "the total of \@user is $user_stdin\n";
复制代码 我的问题是,当我的操作如下 有空格或者直接回车的时候会出现警告)- [root@test chapter4]# ./ex4_1.pl
- the total of @terry is 15
- ==============
- 1
- 2
- 3
- 4
-
- 5
- Argument "" isn't numeric in addition (+) at ./ex4_1.pl line 8, <stdin> line 7.
- Argument " " isn't numeric in addition (+) at ./ex4_1.pl line 8, <stdin> line 7.
- the total of @user is 15
复制代码 我想请教下是不是有什么函数或者其他什么的可以把警告去掉? |
|