<pre name="code" class="python">第六章 子过程:
象其他的语言一样,Perl也支持自定义的子程序。(注: 我们也把它们叫做函数,不过,函数和子程序在Perl里是一样的东西。
有时候我们甚至叫它们方法.
@_ 存储的是数组参数
@_ $_[0] 和$_[1]
[oracle@jhoa 20150319]$ cat a3.pl
sub max {
$max = shift(@_);
for my $item (@_) {
$max = $item if $max < $item;
}
return $max;
}
$bestday = max(20, 8, 12, 16, 30);
print "\$bestday is $bestday \n";
[oracle@jhoa 20150319]$ perl a3.pl
$bestday is 30
2.3 范围问题:
&foo(1,2,3) # 传递三个参数
foo(1,2,3) # 和上面一样
foo(); # 传递一个空列表
&foo(); # 和上面一样
&foo; # foo() 获取当前的参数,和 foo(@_) 一样,但更快!
foo; # 如果预定义了子过程 foo,那么和 foo() 一样,否则
# 就是光字 "foo"
[oracle@jhoa 20150319]$ cat a4.pl
sub max {
print "this is test\n";
};
$subref = max;
print "\$subref is $subref \n";
[oracle@jhoa 20150319]$ perl a4.pl
this is test
$subref is 1
获取函数返回值:
间接调用子过程(通过名字或引用
第六章 子过程 和子过程引用
最新推荐文章于 2019-04-12 10:52:56 发布