Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: What is the point of the & sigil for function refs?

by merlyn (Sage)
on Feb 04, 2005 at 12:14 UTC ( #428029=note: print w/ replies, xml ) Need Help??


in reply to What is the point of the & / ampersand sigil for function refs?

The & prefix unambiguously means "this is a user-defined subroutine call coming up next". You can omit it when you are using parens after your function name and your function name doesn't conflict with a built-in, or if you've declared/defined your function before you use it and the name doesn't conflict with a built-in.

The & prefix also disables prototype checking, but you shouldn't be using prototypes in the first place, so I consider that a very minor point, but if I didn't mention it, someone else would surely raise that flag. {grin}

In our Learning Perl book, we suggest that you always use the ampersand when you're first learning Perl, because otherwise you'll be a bit befuddled when the following code doesn't work:

sub log { print STDERR "logging: ", @_, "\n"; } ... log("starting"); ... log("finishing"); ...
Yes, you're not calling your log subroutine... you're calling the built-in "logarithm" function. Oops.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on Re: What is the point of the & sigil for function refs?
Download Code
Re^2: What is the point of the & sigil for function refs?
by Random_Walk (Parson) on Feb 04, 2005 at 12:28 UTC

    > The & prefix also disables prototype checking, but you shouldn't be using prototypes in the first place,
    > so I consider that a very minor point, but if I didn't mention it, someone else would surely raise that flag. {grin}

    I was going to ask for clarification here as I thought prototypes were good ju-perl. How mistaken I was, a supersearch lead me to this node Gratuitous use of Perl Prototypes. Another step on the path to enlightenment.

    Thanks,
    R.

    Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-07-06 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls