Perl-subroutine

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(chomp(<STDIN>));
sub total{
	my $vari_total = 0;
foreach(@_){
	$vari_total += $_;
	print "$_\n";
}
$vari_total;
}
#end

输出:
1
3
5
7
9
The total of @fred is 25.
在foreach循环中@_代指数组”老地方“即可以迭代@_代指的数组、
$_代指数组中元素的值;本例中代指1 3 5 7 9

你可能感兴趣的:(perl)