in reply to calling f(arg1,arg2,arg3) better than &f

Like just about every other Perl construct, there are times when it makes sense to use &f and others when it doesn't. This construct reuses the current @_ as it's argument list. This can be handy if you want the called function to have some effect on the caller's argument list which the caller will subsequently need to make use of. In most other contexts, it is probaby more clear to f(@_) explicitly so that what is happening is more clear. The effiency differences between &f and f(@_) are negligible except in the most extreme cases, though f(@_) does do extra work to construct a new @_ for f to use.

Your coworker's reference or 'argument checking' is probably referring to the fact that when you call a function as &f then any prototype it has is ignored. This isn't relevant if you have not established a prototype for the function in question, so your coworkers criticism does not apply. It is probably a bad idea to call a prototyped functions as &f as confusion is indeed likely to occur.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on Re: calling f(arg1,arg2,arg3) better than &f

Replies are listed 'Best First'.
Re^2: calling f(arg1,arg2,arg3) better than &f
by edwardt_tril (Sexton) on Feb 08, 2006 at 22:27 UTC
    Hi I have got another explanation that Using & means that it's a user defined subroutine. For example:
    &print is a user define print and will not use Perls print function.
    Also, if you don't specify the "&", subroutines must be defined before you make a call to it.
    But from PErl Best Practise:
    sub lock{ my ($file)=@_; print " user defined lock\n"; } sub link{ my ($text, $url) =@_; print " user defined link $text $url\n"; } print "calling lock: ".lock; #calls locl, built-in lock hidden; print "calling &lock: ".&lock; # calls user defined lock print "calling &link: ".&link("123","123"); # user defined link print "calling &link: ".&link; #user defined won't print 123 print "calling link: ".link("123","123"); # built-in link
    So my question: is this a strong case for using &?