- 论坛徽章:
- 0
|
本帖最后由 广西邮储银行 于 2011-03-20 13:08 编辑
下面的脚本我用./4-3.pl和perl ./4-3.pl执行时前者失败后者成功 想问问是什么原因。
[19:54:56|view:perl]cat 4-3.pl
#/usr/bin/env perl
sub total{
my $sum;
foreach (@_){
$sum+=$_
}
$sum
}
sub above_average{
my @b;
my $avg=&total(@_)/($#_+1);
foreach (@_){
if($_ > $avg){
push @b,$_;
}
}
@b;
}
my @fred=above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";
my @barney=above_average(100,1..10);
print "\@barney is @barney\n";
print "(Should be just 100)\n";
[19:55:02|view:perl]ls -l 4-3.pl
-rwxr--r-- 1 view oinstall 369 3月 17 19:51 4-3.pl
[19:55:09|view:perl]./4-3.pl
./4-3.pl: line 2: sub: command not found
./4-3.pl: line 3: my: command not found
./4-3.pl: line 4: syntax error near unexpected token `@_'
./4-3.pl: line 4: `foreach (@_){'
[19:55:18|view:perl]perl ./4-3.pl
@fred is 6 7 8 9 10
(Should be 6 7 8 9 10)
@barney is 100
(Should be just 100) |
|