views:

297

answers:

6

Hi, My sample code as below, there are two styles subname and subnam()

#!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(); and &marine;) is more popular if there is no arguments in a sub? Thank you.

+17  A: 

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.

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.

brian d foy
You're also giving yourself and other maintainers of the code a hint that it is a subroutine name.
David Dorward
Ether
+7  A: 
daxim
brian was 28 seconds quicker.
daxim
Graeme Perrow
@daxim, thank you :-)
Nano HE
@Graeme: it's all in perlsub.
brian d foy
+1  A: 

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")

Neeraj
why the downvote?.. is there sth wrong with it?
Neeraj
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
+1  A: 

In addition to the information provided by others, the documentation (perlsub) explains the rules for using parentheses.

toolic
+4  A: 

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
+11  A: 

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:
mobrule
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.
Dave Webb
@Dave, the `` 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 { 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