子程序
由于所有的被调用的子程序都要返回值,因此使用特殊的返回值语法在大多数情况下是一种浪费。因此Larry 将之简化了。当Perl 遍历此子程序时,将会计算每一步的值。此子程序中最后计算的值将被返回。
例如,下面的子程序:
sub sum_of_fred_and_barney{
print “Hey, you called the sum_of_fred_and_barney suroutine!/n”;
$fred + $barney; #返回值
}
子程序中最后一个被计算的表达式为$fred + $barney,因此$fred 和$barney 的和将被返回。下面是一些调用代码:
$fred = 3;
$barney = 4;
$wilma = $sum_of_fred_and_barney; #$wilma 得到7
print “/$wilma is $wilma./n”;
$betty = 3 * &sum_of_fred_and_barney; #$betty 得到21
print “/n$betty is $betty./n”;
Perl 语言入门(第四版)
blei@163.com 55 / 201 9/21/2006
上述代码将得到下面的输出:
Hey, you call the sum_of_fred_and_barney subroutine!
$wilma is 7.
Hey, you call the sum_of_fred_and_barney subroutine!
$betty is 21.
2094

被折叠的 条评论
为什么被折叠?



