#!/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\
Calling a subroutine using the &NAME;
syntax makes current @_
visible to it. This is documented in perlsub:
If a subroutine is called using the
&
form, the argument list is optional, and if omitted, no@_
array is set up for the subroutine: the@_
array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.
So, it's definitely a feature.
Straight from the camel's mouth:
If a subroutine is called using the ``&'' form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.
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