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.

Replies are listed 'Best First'.
Re^2: What is the point of the & sigil for function refs?
by Random_Walk (Prior) 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!