Perl 函数调用、作用域与闭包详解
1. 函数调用与参数操作
在 Perl 中,你可以通过函数名或引用调用函数。有时为了优化,你可能需要操作 @_ 数组来修改传递给替换函数的参数。示例代码如下:
if ($item < $miditem) {
# split the array down and recurse
@_ = ($item, @array[0 .. $midpoint]);
goto &elem_exists;
}
else {
# split the array up and recurse
@_ = ($item, @array[$midpoint + 1 .. $#array] );
goto &elem_exists;
}
虽然这种优化方式可能不太美观,但当递归代码会导致内存耗尽时,这种方式就显得很实用。
2. Perl 函数调用的陷阱与缺陷
Perl 仍然支持旧风格的函数调用,即使用前导的 & 字符调用函数,不过这种方式已经过时,应该避免使用。示例如下:
# outdated style; avoid
my $result = &calculate_result( 52 );
# very outdated; truly avoid
my $result = do &calculate_result( 42 )
超级会员免费看
订阅专栏 解锁全文
9

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



