Perl 调用带有前导“&"的子程序 [英] Perl calling subroutine with leading '&'
问题描述
我时不时地看到 Perl 脚本,其中调用子程序时带有前导&".这是遗产,还是有任何好处?调用不带与号的子程序也能正常工作.
Every now and then I see Perl scripts where subroutines are called with a leading '&'. Is this legacy, or does it give any benefit? As calling the subroutine without the ampersand sign works as well.
sub mysub {
print "mysub\n";
}
mysub;
&mysub;
Thx/赫尔曼
推荐答案
使用 &
调用通常是一种代码味道,有人不知道他们在做什么并且处于 Perl4 思维模式.在您的具体示例中,它的工作原理完全相同.但是,使用 &
调用会禁用函数原型,因此高级用户可能会在某些情况下使用它.在这种情况下,您应该会在呼叫旁边看到为什么注释.
Calling with &
is generally a code smell that somebody doesn't know what they're doing and are in a Perl4 mindset. In your specific example, it works exactly the same. However, calling with &
disables function prototypes, so advanced users may use it in certain circumstances. You should expect to see a comment why next to the call in that case.
这篇关于Perl 调用带有前导“&"的子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!