20

Possible Duplicate:
When should I use the & to call a Perl subroutine?

In perl scripts, why is that method of invoking the function is written differently some times. I see &function and sometimes function(). Are they both the same and this is just a style that one would want to flaunt? If they are the same, why are they both available, would not one just suffice? I am guessing there is some semantic difference between the both the methods which distinguishes them from each other ... but at what kind of circumstances?

--- Since I cannot answer my own question for timeout reasons -- I am updating the answer in the section of question itself. When I get a chance to update the answer block, I will put it there.

I found the relevant text in 'Learning Perl' book..thanks for the tip though. Chapter 4: Subroutines -- Omitting the Ampersand.

I was more interested in ampersand & usage for perl functions. If a subroutine is already defined before being invoked, then subroutine can be invoked without using & while calling the function similar to invoking the builtin functions. & is also used to distinguish between the builtin functions and the user defined functions if the function to be invoked uses the same name that of one of the builtin function, provided it is defined before being invoked.

Usage of (), is merely to justify the passing of the arguments to the subroutines, while if not used, the default list of current arguments are passed in the form @_. If the arguments are specified in () for a subroutine, it is assumed to be a subroutine and is invoked even if not previously defined while parsing.

6
  • 11
    See What's the difference between calling a function as &foo and foo()? in Perl FAQ 7.
    – manatwork
    Commented Jan 18, 2012 at 9:58
  • hi @manatwork, that explains difference between &foo and &foo() but why is ampersand being used at all if not necessary...when is it necessary?
    – user14449
    Commented Jan 18, 2012 at 10:09
  • In the Description section perlsub enumerates when is & optional.
    – manatwork
    Commented Jan 18, 2012 at 10:20
  • You can't call a subroutine that hasn't been previously defined if there's a use strict; pragma -- which there generally should be. Commented Jan 18, 2012 at 11:07
  • This probably belongs on Stack Overflow, since it's not Unix-specific. Commented Jan 18, 2012 at 11:09

2 Answers 2

22

It has very specific uses:

  • & tells Perl to ignore the sub's prototype.
  • & can allow you to use the caller's @_. (&foo; with no parens or arguments).
  • goto &foo; and defined &foo.
  • Getting a reference (e.g. \&foo).

Some people (mostly beginners) use it to distinguish user subs from builtin functions, since builtin functions cannot be preceded by &.

2
  • What other way is there to distinguish user subs from builtins? Commented Nov 26, 2014 at 11:20
  • 3
    @jackthehipster, Naming conventions, though I question the need for it. Who cares who wrote it. Does it matter that utf8::encode is a builtin and that Encode::encode_utf8 isn't?
    – ikegami
    Commented Nov 26, 2014 at 12:18
5

As mentioned by @manatwork and @KeithThompson you can find information in these articles:

  1. A general description - What's the difference between calling a function as &foo and foo()?
  2. Subtle information about using & or not for a function call - perlsub: Perl Subroutines: Description.
1
  • In case you're wondering about my one-char. edit: Your answer rendered with an incorrectly URL-encoded URL (even though it was correct in the Markdown source), which caused the anchor part of the "What's the difference..." not to function correctly (one landed at the top of the lengthy target page instead of at the anchor of interest). I suspect this was a historical Markdown-rendering bug that has since been fixed for new answers. By forcing your answer to be saved again, the problem went away.
    – mklement0
    Commented Aug 21, 2015 at 16:02