Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling subroutine:Using &

Status
Not open for further replies.

spookie

Programmer
May 30, 2001
655
IN
Hi,
Is it good practice to use & to call a subroutine ?

Your thoughts please.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
It's not considered good, but it's not considered bad as far as I know. But you have to know how its use affects programs.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I think generally, using & implies to the interpreter that you're calling one of your own subroutines and not one exported by a module or any standard Perl function.

For instance:
Code:
sub join {
   my ($channel) = @_;

   $irc->join ($channel);
}

Code:
join ("#lobby");

Perl warns
"Ambiguous call to subroutine resolved as main::join() at script.pl line X"

&join ("#lobby");

no Perl warning for that

At least that's how it goes in my experience...

Since join() is a core function, if you define a sub by that name and call it without &, Perl warns you and then assumes you meant your local version of join and not the core method (if you wanted the core method and you had a sub named "join", you have to call it as "CORE::join()" instead).

I'd say it's probably better to use &.

-------------
Cuvou.com | The NEW Kirsle.net
 
Thanks for your inputs KevinADC, Kirsle.

I personally never use & while calling sub. But recently i saw couple of scripts with & prefix and i thought, hey this is more readable than not using &.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
leaving the & off means the interpreter treats the sub as a prototype, I don't understand fully what that means.

But it's like creating a sub that works like an internal command.

also callng a sub with () apprently leaves the current @_ exposed to the called sub.

Something I never knew till today.

This link may help
"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
opps , that should be
callng a sub withOUT ()

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Must admit, I've always left off the &, and used the my_sub() notation to call them, which causes perl to behave like most other languages WRT scoping etc. so I've never run across this problem.

But overloading the built-in functions with same-named ones of your own devising (on purpose) sounds like a really bad idea...


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top