Difference between &function and function() in perl [duplicate]
Asked Answered
J

2

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.

Judi answered 18/1, 2012 at 9:40 Comment(6)
See What's the difference between calling a function as &foo and foo()? in Perl FAQ 7.Darsey
hi @manatwork, that explains difference between &foo and &foo() but why is ampersand being used at all if not necessary...when is it necessary?Judi
In the Description section perlsub enumerates when is & optional.Darsey
You can't call a subroutine that hasn't been previously defined if there's a use strict; pragma -- which there generally should be.Anisette
This probably belongs on Stack Overflow, since it's not Unix-specific.Anisette
Corrected version of @manatwork's helpful link: What's the difference between calling a function as &foo and foo()?. (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.)Bayles
C
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 &.

Crews answered 18/1, 2012 at 18:24 Comment(2)
What other way is there to distinguish user subs from builtins?Nesmith
@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?Crews
K
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.
Kerseymere answered 18/1, 2012 at 15:19 Comment(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.Bayles

© 2022 - 2024 — McMap. All rights reserved.