Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

what's the difference when calling a function with or without &

sub test { print "test"; } what is calling it

test() vs. &test();

Replies are listed 'Best First'.
Re: syntax for calling a function
by pc88mxer (Vicar) on Jun 27, 2008 at 18:54 UTC
    The first is preferred and the second is deprecated.

    Using the ampersand allows you to call a function without parentheses:

    &foo; sub foo { print "Hello\n" }
    Perl will interpret a bareword as a subroutine call with no arguments, but then you have to make sure that perl knows your bareword is a subroutine:
    foo; # does not call foo() sub foo { print "Hello\n" } foo; # calls foo()

      Be very careful with &foo;. This is a powerful, but dangerous way to call a function and should only be done when the special semantics are intended.

      Powerful? Dangerous? Special semantics? This form of sub call will automagically grab the current @_ for its arguments. Combine that with Perl's pass by reference subroutines and serious weirdness may ensue.

      See Messing with @_ for an example of how this can be used and/or abused.


      TGI says moo

      &foo; sub foo { print "Hello\n" }

      Don't forget that the following calls are identical:

      &foo; &foo(@_); foo(@_);

      Therefore, &foo; should be used with care (passing @_ explicitly is much more friendly, IMHO).

      Update: chromatic is right, I should've checked perlsub before posting.

        Don't forget that the following calls are identical:

        They aren't, though. The leading & explicitly bypasses any prototype checking. &foo(@_) may be very different from foo(@_).

Re: syntax for calling a function
by toolic (Bishop) on Jun 27, 2008 at 19:04 UTC
    Read the free document, perlsub.

    The Perl Best Practices book also has a section making recommendations on usage. It's not as free, but it is worth the small investment, in my opinion.

Re: what is the syntax for calling a function
by Fletch (Bishop) on Jun 27, 2008 at 19:10 UTC

    What does reading the documentation lead you to believe? I mean it's not like the synopsis itself answers this question or anything . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: syntax for calling a function
by zentara (Cardinal) on Jun 28, 2008 at 13:40 UTC
    To avoid confusion, in gui code you will often see the &somesub syntax, because often gui's will automatically pass the calling widget as it's first argument to the sub, and the & supresses it. Like:
    -command=>[\&raiseit,$win]) # passes $win as first arg # instead of widget or event # not a 100% foolproof rule
    So, even though the somesub() syntax is preferred ( and most correct), be prepared to see other weird constructs. Everytime I write a sub, I usually put
    sub somesub{ print "@_\n"; .... .... }
    as it's first line, just to be sure what I'm getting. Sometimes I surprised by what comes in.

    I'm not really a human, but I play one on earth CandyGram for Mongo