开发者

Should I call Perl subroutines with no arguments as marine() or marine?

开发者 https://www.devze.com 2022-12-16 01:47 出处:网络
As per my sample code below, there are two styles to call a subroutine: subname and subname(). #!C:\\Perl\\bin\\perl.exe

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?


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.


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:


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消