【问题标题】:What is the difference between Perl subroutines with "&" (ampersand) and without [duplicate]带有“&”(与号)和不带 [重复] 的 Perl 子例程有什么区别
【发布时间】:2014-03-12 23:41:08
【问题描述】:

Perl 中接下来的两行有什么区别:

PopupMsg("Hello");

&PopupMsg("Hello");

...

sub PopupMsg
{
    subroutines code here...
}

请注意,在某些情况下,我必须使用第一行,而在某些情况下,我必须使用第二行,否则会出错。

【问题讨论】:

标签: perl subroutine


【解决方案1】:

使用与号& 调用子例程是一种不好的做法。只要您使用括号或将符号预先声明为子例程名称,调用就可以正常编译。

当您将子例程作为数据项处理时,与号是必需的,例如引用它。

my $sub = \&PopupMsg;

【讨论】:

    【解决方案2】:

    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 中进一步解释了原型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2021-04-06
      • 2011-05-19
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2015-04-05
      相关资源
      最近更新 更多