Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: why avoid & on function call (updated x 3)

by LanX (Saint)
on Dec 27, 2020 at 16:32 UTC ( [id://11125789]=note: print w/replies, xml ) Need Help??


in reply to why avoid & on function call

In Perl 4 the &sigil was mandatory, Perl 5 changed this.

IIRC it has now a bunch of side effects like°

  • disabling prototype check when passing arguments
  • reusing the caller's @_ when not passing arguments

This can be very useful in edge cases <1‰, but will more likely cause trouble in normal use.

The only time you can't avoid &sigils in standard Perl5 is when referencing a named function ˛

$code_ref = \&sub_name

More in perlsub ...

HTH :)

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

Updates

°) other use case are:

  • calling a post-declared sub without parens().

    E.G. like when wanting to use AUTOLOAD while avoiding parenthesis for syntactic sugar.

  • calling a sub named after a built-in

    E.g. CGI.pm used to have a sub &tr for HTML's "table row" tag which collided with Perl's tr (IIRC)

  • ˛) some built-ins require & with named subs as arguments, like goto, map , grep , sort

Replies are listed 'Best First'.
Re^2: why avoid & on function call (updated x 3)
by Anonymous Monk on Dec 28, 2020 at 19:38 UTC
    defined &func, exists &func, and goto &func (distinct from the troublesome goto LABEL) are some other examples when you would use the ampersand to refer to a named function. -Dan
      > are some other examples when you would use the ampersand to refer to a named function.

      goto was already mentioned :)

      > > ˛) some built-ins require & with named subs as arguments, like goto, map , grep , sort

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found