Using ampersands and parens when calling a Perl sub

前端 未结 3 879
粉色の甜心
粉色の甜心 2021-02-13 09:28
#!/usr/bin/perl

sub t {
  print \"in t\\n\";
  print \"@_\\n\";
  &s;
}

sub s {
  print \"in s\\n\";
  print \"@_\\n\";
}

t(1,2);
print \"out\\n\";
print \"@_\\n\         


        
3条回答
  •  -上瘾入骨i
    2021-02-13 10:29

    When using & before the sub name and no argument list is passed, the current @_ is passed as argument instead. So, it is a feature.

    Here are the different ways to call a subroutine:

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

    from perldoc perlsub

提交回复
热议问题