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


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

The placement of a sub is important onlymainly when you call it without parentheses, i.e. as a bareword. When such a call is being parsed, the subs already parsed are recognised and the bareword is parsed as their call, otherwise without strict, the bareword is stringified, and with strict, you get an error.
#!/usr/bin/perl use warnings; print "", frob(); # 12 print "", frob; # frob use strict; print "", frob(); # 12 # print "", frob; sub frob { 12 }
Uncommenting the last last print line would make the source unparsable with
Bareword "frob" not allwoed while "strict subs" in use

Updated: as shown in the text, thanks kcott.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^3: why avoid & on function call
by kcott (Archbishop) on Dec 27, 2020 at 19:14 UTC
    "The placement of a sub is important only when you call it without parentheses, ..."

    I disagree with the inclusion of the word only in that statement. Here's a situation where placement is important when parentheses are used:

    $ perl -wE ' sub meaning () { 42 } say meaning(); ' 42
    $ perl -wE ' say meaning(); sub meaning () { 42 } ' main::meaning() called too early to check prototype at -e line 2. 42

    You can, of course, predeclare and leave the definition for later:

    $ perl -wE ' sub meaning (); say meaning(); # ... later in code with other subroutine defintions sub meaning () { 42 } ' 42

    — Ken

Re^3: why avoid & on function call
by LanX (Saint) on Dec 27, 2020 at 17:59 UTC
    Bareword use without parens is also the reason why the sub NAME; syntax without function {BODY} exists.

    One can pre-declare functions for the first parse, which are later defined (or resolved via AUTOLOAD)

    DB<188> sub frob; frob(1,2,3); sub frob { say "@_" } 1 2 3 DB<189>

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