views:

328

answers:

3

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

+15  A: 

IMO, the only time there's any reason to use & is if you're obtaining or calling a coderef, like:

sub foo() {
    print "hi\n";
}

my $x = \&foo;
&$x();

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 @array and %hash specifications 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 with foo(@_). Using &foo is infinitesimally faster than foo(@_). I wouldn't recommend it unless you've definitively found by profiling that you need that micro-optimization.

chaos
Could you please expand on the part about "non-default call behavior"?
Rob Kennedy
Done.
chaos
Except, ` you should probably just dereference it, like any other ref: $x->().
Robert P
Michael Carman
j_random_hacker
+11  A: 

The &subroutine() form disables prototype checking. This may or may not be what you want.

http://www.perl.com/doc/manual/html/pod/perlsub.html#Prototypes

Prototypes allow you to specify the numbers and types of your subroutine arguments, and have them checked at compile time. This can provide useful diagnostic assistance.

Prototypes don't apply to method calls, or calls made in the old-fashioned style using the & prefix.

The & is necessary to reference or dereference a subroutine or code reference

e.g.

sub foo {
   # a subroutine
}

my $subref = \&foo; # take a reference to the subroutine

&$subref(@args);  # make a subroutine call using the reference.

my $anon_func = sub { ... }; # anonymous code reference
&$anon_func(); # called like this

Protypes aren't applicable to subroutine references either.

The &subroutine form is also used in the so-called magic goto form.

The expression goto &subroutine replaces the current calling context with a call to the named subroutine, using the current value of @_.

In essence, you can completely switch a call to one subroutine with a call to the named one. This is commonly seen in AUTOLOAD blocks, where a deferred subroutine call can be made, perhaps with some modification to @_ , but it looks to the program entirely as if it was a call to the named sub.

e.g.

sub AUTOLOAD {
    ...
    push @_, @extra_args; # add more arguments onto the parameter list
    goto &subroutine ; # change call another subroutine, as if we were never here
}

}

see detailed explanation of this technique here

cms
+8  A: 

I'm a frequent abuser of &, but mostly because I'm doing weird interface stuff. If you don't need one of these situations, don't use the &. Most of these are just to access a subroutine definition, not call a subroutine. It's all in perlsub.

  1. Taking a reference to a named subroutine. This is probably the only common situation for most Perlers:

     my $sub = \ &foo;
    
  2. Checking that a subroutine is defined, as you might in test suites:

     if( defined &foo ) { ... }
    
  3. Removing a subroutine definition, which shouldn't be common:

     undef &foo;
    
  4. Providing a dispatcher subroutine whose only job is to choose the right subroutine to call. This is the only situation I use & to call a subroutine, and when I expect to call the dispatcher many, many times and need to squeeze a little performance out of the operation:

     sub figure_it_out_for_me {
        # all of these re-use the current @_
          if( ...some condition... ) { &foo     } 
       elsif( ...some other...     ) { &bar     }
       else                          { &default }
       }
    

There are some places where you could use them, but there are better ways:

  1. To call a subroutine with the same name as a Perl built-in. Just don't have subroutines with the same name as a Perl built-in. Check perlfunc to see the list of built-in names you shouldn't use.

  2. To disable prototypes. If you don't know what that means or why you'd want it, don't use the &. Some black magic code might need it, but in those cases you probably know what you are doing.

  3. To deference and execute a subroutine reference. Just use the -> notation.

brian d foy
cjm
@cjm: indeed. That's why I said exactly that in the first paragraph.
brian d foy