P
PerlFAQ Server
This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
7.22: What's the difference between calling a function as &foo and foo()?
(contributed by brian d foy)
Calling a subroutine as &foo with no trailing parentheses ignores the
prototype of "foo" and passes it the current value of the argument list,
@_. Here's an example; the "bar" subroutine calls &foo, which prints its
arguments list:
sub bar { &foo }
sub foo { print "Args in foo are: @_\n" }
bar( qw( a b c ) );
When you call "bar" with arguments, you see that "foo" got the same @_:
Args in foo are: a b c
Calling the subroutine with trailing parentheses, with or without
arguments, does not use the current @_ and respects the subroutine
prototype. Changing the example to put parentheses after the call to
"foo" changes the program:
sub bar { &foo() }
sub foo { print "Args in foo are: @_\n" }
bar( qw( a b c ) );
Now the output shows that "foo" doesn't get the @_ from its caller.
Args in foo are:
The main use of the @_ pass-through feature is to write subroutines
whose main job it is to call other subroutines for you. For further
details, see perlsub.
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
7.22: What's the difference between calling a function as &foo and foo()?
(contributed by brian d foy)
Calling a subroutine as &foo with no trailing parentheses ignores the
prototype of "foo" and passes it the current value of the argument list,
@_. Here's an example; the "bar" subroutine calls &foo, which prints its
arguments list:
sub bar { &foo }
sub foo { print "Args in foo are: @_\n" }
bar( qw( a b c ) );
When you call "bar" with arguments, you see that "foo" got the same @_:
Args in foo are: a b c
Calling the subroutine with trailing parentheses, with or without
arguments, does not use the current @_ and respects the subroutine
prototype. Changing the example to put parentheses after the call to
"foo" changes the program:
sub bar { &foo() }
sub foo { print "Args in foo are: @_\n" }
bar( qw( a b c ) );
Now the output shows that "foo" doesn't get the @_ from its caller.
Args in foo are:
The main use of the @_ pass-through feature is to write subroutines
whose main job it is to call other subroutines for you. For further
details, see perlsub.
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.