I have heard that people shouldn’t be using & to call Perl subs, i.e:
function($a,$b,...);
# opposed to
&function($a,$b,...);
I know for one the argument list becomes optional, but what are some cases where it is appropriate to use the & and the cases where you should absolutely not be using it?
Also how does the performace increase come into play here when omitting the &?
IMO, the only time there’s any reason to use
&is if you’re obtaining or calling a coderef, like:The main time that you can use it that you absolutely shouldn’t in most circumstances is when calling a sub that has a prototype that specifies any non-default call behavior. What I mean by this is that some prototypes allow reinterpretation of the argument list, for example converting
@arrayand%hashspecifications to references. So the sub will be expecting those reinterpretations to have occurred, and unless you go to whatever lengths are necessary to mimic them by hand, the sub will get inputs wildly different from those it expects.I think mainly people are trying to tell you that you’re still writing in Perl 4 style, and we have a much cleaner, nicer thing called Perl 5 now.
Regarding performance, there are various ways that Perl optimizes sub calls which
&defeats, with one of the main ones being inlining of constants.There is also one circumstance where using
&provides a performance benefit: if you’re forwarding a sub call withfoo(@_). Using&foois infinitesimally faster thanfoo(@_). I wouldn’t recommend it unless you’ve definitively found by profiling that you need that micro-optimization.