FAQ 7.22 What's the difference between calling a function as &foo and foo()?

Discussion in 'Perl Misc' started by PerlFAQ Server, Feb 15, 2011.

  1. 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.
    PerlFAQ Server, Feb 15, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. Manuel
    Replies:
    6
    Views:
    292
    Russell Wood
    Jan 7, 2006
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    381
    JaredGrubb
    Sep 24, 2008
  4. Surgeon
    Replies:
    135
    Views:
    925
  5. Brandon Dimcheff
    Replies:
    2
    Views:
    138
    Brandon Dimcheff
    Oct 7, 2008
Loading...

Share This Page