in reply to syntax for calling a function

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()

Replies are listed 'Best First'.
Re^2: syntax for calling a function
by TGI (Parson) on Jun 27, 2008 at 23:20 UTC

    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

Re^2: syntax for calling a function
by alexm (Chaplain) on Jun 27, 2008 at 19:08 UTC
    &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(@_).