Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Another perl battleship

by spesk (Novice)
on Jan 08, 2019 at 15:01 UTC ( [id://1228214]=note: print w/replies, xml ) Need Help??


in reply to Re: Another perl battleship
in thread Another perl battleship

Thanks jwkrahn, appreciate the feedback

My understanding was using ' & ' was best style practice, based on some of the code bases/people I've worked with, perhaps that is just an idiomatic thing where I work though.

Replies are listed 'Best First'.
Re^3: Another perl battleship
by haukex (Archbishop) on Jan 08, 2019 at 17:55 UTC
    My understanding was using ' & ' was best style practice

    In "modern" Perl, that is no longer the case. Calling a sub with & disables prototype checking, and also calling a sub as &foo; leaves @_ unmodified. Since both of these behaviors fall in the "one should only do this if one knows what one is doing" category, calling subs without the & is generally recommended (unless one knows what one is doing :-) ). From perlsub (emphasis mine):

    A subroutine may be called using an explicit & prefix. The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The & is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the &$subref() or &{$subref}() constructs, although the $subref->() notation solves that problem. See perlref for more about all that.

    Subroutines may be called recursively. If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.

    &foo(1,2,3); # pass three arguments foo(1,2,3); # the same foo(); # pass a null list &foo(); # the same &foo; # foo() get current args, like foo(@_) !! foo; # like foo() IFF sub foo predeclared, else "foo"

    Not only does the & form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing. See Prototypes below.

    Update: For even more, see "Pitfalls and Misfeatures" in Modern Perl.

      Thanks for the response haukex. Will update my sub calls, the examples provided by perlsub make this easier to understand.
Re^3: Another perl battleship
by BillKSmith (Monsignor) on Jan 08, 2019 at 16:59 UTC
    The document perlsub appears to say that the & form is always optional. However, it then specifies a few special cases where it is required. The practice of using the & only when necessary has the advantage of alerting human readers that special argument handling is intended. Casual use of the & form for calling subroutines which specify signatures could lead to subtle errors.
    Bill
      Thank you very much for the response Bill.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228214]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-28 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found