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.

Community
  • 1
  • 1
  • 11
    See [What's the difference between calling a function as &foo and foo()?](http://perldoc.perl.org/perlfaq7.html#What%27s-the-difference-between-calling-a-function-as-%26foo-and-foo%28%29%3F) in Perl FAQ 7. – manatwork Jan 18 '12 at 09: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? –  Jan 18 '12 at 10:09
  • In the Description section [perlsub](http://perldoc.perl.org/perlsub.html) enumerates when is `&` optional. – manatwork Jan 18 '12 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. – Keith Thompson Jan 18 '12 at 11:07
  • This probably belongs on [so], since it's not Unix-specific. – Keith Thompson Jan 18 '12 at 11:09
  • Corrected version of @manatwork's helpful link: [What's the difference between calling a function as &foo and foo()?](http://perldoc.perl.org/perlfaq7.html#What's-the-difference-between-calling-a-function-as-%26foo-and-foo()%3f). (The original is incorrectly URL-encoded due to an old site rendering bug; as a result, you're taken to the correct page, but not the right anchor, leaving you to go digging for the section of interest on the lengthy target page. Newly created answers and comments don't exhibit this problem anymore.) – mklement0 Aug 21 '15 at 16:28

2 Answers2

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 &.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • What other way is there to distinguish user subs from builtins? – jackthehipster Nov 26 '14 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 Nov 26 '14 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.
mklement0
  • 382,024
  • 64
  • 607
  • 775
maerics
  • 151,642
  • 46
  • 269
  • 291
  • 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 Aug 21 '15 at 16:02