带"&"(与号)和不带 Perl 子例程有什么区别



perl中的下两行有什么区别:

PopupMsg("Hello");
&PopupMsg("Hello");
...
sub PopupMsg
{
    subroutines code here...
}

请注意,在某些情况下,我必须使用第一行,而在第二行,另一个明智的问题我会遇到错误。

使用AMPERSAND &调用子例程是不好的做法。如果您使用括号或已将符号定为子例程名称,则呼叫将罚款。

当您处理子例程作为数据项时,必须进行ampersand。

my $sub_ref = &PopupMsg;
$sub_ref->(); # calling subroutine as reference.

请参阅http://perldoc.perl.org/perlsub.html:

NAME(LIST);  # & is optional with parentheses.
NAME LIST;   # Parentheses optional if predeclared/imported.
&NAME(LIST); # Circumvent prototypes.
&NAME;       # Makes current @_ visible to called subroutine.

原型在http://perldoc.perl.org/perlsub.html#prototypes中进一步解释。

最新更新