http://www.perlmonks.org?node_id=11125806


in reply to Re: why avoid & on function call
in thread why avoid & on function call

Post-declared subs are always resolved as long as Perl knows that it's a sub, that's why Perl5 introduced the "new syntax" ° with parenthesis func()

Pre-declaration is only necessary when avoiding parens in bareword use, like func 1,2,3 because otherwise Perl can't tell at the first parse if its a sub-call or not.

A pre-declaration placeholder can be done by a body-less sub NAME; or with the pragma use subs LIST for multiple names.

Using &ampersands just to spare parens() has too many side-effects and should be limited to cases which can't be resolved by pre-declaration.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) "new" as in "new york", "new model army" or "Bojo Churchill".

Replies are listed 'Best First'.
Re^3: why avoid & on function call (pre-declaration)
by Bod (Parson) on Dec 27, 2020 at 19:54 UTC
    A pre-declaration placeholder can be done by a body-less sub NAME;

    Thanks LanX - I didn't know this was an option. Although in the past I have habitually added an ampersand which I have been doing less and less over the last year or so in favour of always applying parenthesis. So I don't envisage me using this pre-declaration very much but it's good to know it exists.

      > So I don't envisage me using this pre-declaration very much but it's good to know it exists.

      There are use cases, like plugin systems or delayed compilation on demand.

      NB: prototypes can be pre-declared too, so just applying parenthesis instead of ampersand wouldn't help, if you want an explicit prototype check.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery