14

As per my sample code below, there are two styles to call a subroutine: subname and subname().

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

use 5.010;

&marine(); # style 1
&marine; # style 2

sub marine {
    state $n = 0; # private, persistent variable $n
    $n += 1;
    print "Hello, sailor number $n!\n";
}

Which one, &marine(); or &marine;, is the better choice if there are no arguments in the call?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Nano HE
  • 9,109
  • 31
  • 97
  • 137

5 Answers5

23

In Learning Perl, where this example comes from, we're at the very beginning of showing you subroutines. We only tell you to use the & so that you, as the beginning Perler, don't run into a problem where you define a subroutine with the same name as a Perl built-in then wonder why it doesn't work. The & in front always calls your defined subroutine. Beginning students often create their own subroutine log to print a message because they are used to doing that in other technologies they use. In Perl, that's the math function builtin.

After you get used to using Perl and you know about the Perl built-ins (scan through perlfunc), drop the &. There's some special magic with & that you hardly ever need:

 marine();

You can leave off the () if you've pre-declared the subroutine, but I normally leave the () there even for an empty argument list. It's a bit more robust since you're giving Perl the hint that the marine is a subroutine name. To me, I recognize that more quickly as a subroutine.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • 7
    You're also giving yourself and other maintainers of the code a hint that it is a subroutine name. – Quentin Jan 13 '10 at 14:14
  • 5
    Perhaps it might be best to drop the use of `&` in the examples, since it is likely to be used inappropriately. – Ether Jan 13 '10 at 19:00
  • Having taught Perl for a long time, you're "likely" in theory in my "almost never" in practice. – brian d foy Mar 11 '16 at 19:27
15

The side effect of using & without parentheses is that the subroutine is invoked with @_. This program

sub g {
  print "g: @_\n";
}
sub f {
  &g();   # g()
  &g;     # g(@_)
  g();    # g()
  g;      # g()
}
f(1,2,3);

produces this output:

g:
g: 1 2 3
g:
g:
mob
  • 117,087
  • 18
  • 149
  • 283
  • 3
    As much as I love Perl it seems that not a week passes without me learning of a new horror hidden in there somewhere waiting to bite me. – David Webb Jan 13 '10 at 16:39
  • 4
    @Dave, the `&subname;` thing is evil if you do it by accident. But is also potentially useful. Say you have 40 or 50 subroutines that have similar argument sanitization needs. You can create a sanitizer that operates on the calling function's arg list directly. `sub foo { &clean_args; my $foo = shift; etc. }` Yeah, you could just return a separate array and handle it, in the functions, but then you have to mangle your argument handling syntax everywhere. These 'horrors' used sparingly and appropriately make it possible to do difficult things easily. – daotoad Jan 14 '10 at 17:48
7

It's good style to declare your subroutines first with the sub keyword, then call them. (Of course there are ways around it, but why make things more complicated than necessary?)

Do not use the & syntax unless you know what it does exactly to @_ and subroutines declared with prototypes. It is terribly obscure, rarely needed and a source of bugs through unintended behaviour. Just leave it away – Perl::Critic aptly says about it:

Since Perl 5, the ampersand sigil is completely optional when invoking subroutines.

Now, given following these style hints, I prefer to call subroutines that require no parameters in style 1, that is to say marine();. The reasons are

  1. visual consistency with subroutines that do require parameters
  2. it cannot be confused with a different keyword.
daxim
  • 39,270
  • 4
  • 65
  • 132
4

As a general rule I recommend the following:

  1. Unless you need the & because you're over riding a built in function or you have no parameter list omit it.

  2. Always include the () as in marine().

I do both of these for code readability. The first rule makes it clear when I'm overriding internal Perl functions by making them distinct. The second makes it clear when I'm invoking functions.

HerbN
  • 1,509
  • 12
  • 23
1

perl allows you to omit parenthesis in your function call.
So you can call your function with arguments in two different ways:
your_function( arg1,arg2,arg3);
or
your function arg1,arg2,arg3 ;
Its a matter of choice that which form do you prefer. With users from C background the former is more intuitive.
I personally use the former for functions defined by me and latter for built in functions like:

print "something" instead of print("something")

sud03r
  • 19,109
  • 16
  • 77
  • 96
  • I do the same thing (although, there are a couple function of mine where I don't use parens. Namely I wrote a function called "prt" which is basically the same as print – tster Jan 13 '10 at 14:50