我应该将不带参数的 Perl 子例程称为 Marine() 或 Marine 吗?

发布于 2024-08-17 20:59:31 字数 417 浏览 9 评论 0原文

根据下面的示例代码,有两种调用子例程的样式:subnamesubname()

#!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";
}

如果调用中没有参数,&marine();&marine; 哪一个是更好的选择?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初熏 2024-08-24 20:59:31

学习 Perl 中,这个例子就来自于此,我们位于一开始就向您展示子程序。我们只是告诉您使用 & ,这样作为 Perler 初学者,您就不会遇到这样的问题:您定义了一个与 Perl 内置函数同名的子例程,然后又想知道为什么它不起作用。前面的 & 总是调用您定义的子例程。初学者经常创建自己的子例程 log 来打印消息,因为他们习惯于在他们使用的其他技术中这样做。在 Perl 中,这是内置的数学函数。

当您习惯使用 Perl 并且了解 Perl 内置函数后(浏览 perlfunc ),删除 && 有一些你几乎不需要的特殊魔力:

 marine();

如果你已经预先声明了子例程,你可以省略 (),但我通常会保留 < code>() 即使对于空参数列表也是如此。它更加健壮一些,因为您向 Perl 提示 marine 是一个子例程名称。对我来说,作为一个子程序,我可以更快地认识到这一点。

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.

兮颜 2024-08-24 20:59:31

使用不带括号的 & 的副作用是使用 @_ 调用子例程。该程序

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

产生以下输出:

g:
g: 1 2 3
g:
g:

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:
情栀口红 2024-08-24 20:59:31

首先使用 sub 关键字声明子例程,然后调用它们是一种很好的风格。 (当然有办法解决这个问题,但是为什么要让事情变得比必要的更复杂呢? )

不要使用 & 语法,除非您知道它对 @_ 和使用原型声明的子例程的确切作用。它非常晦涩难懂,很少需要,并且是由于意外行为而导致错误的来源。把它留下吧 – Perl::Critic 恰当地说:

从 Perl 5 开始,调用子例程时,& 符号是完全可选的。


现在,考虑到这些样式提示,我更喜欢调用样式 1 中不需要参数的子例程,即 marine();。原因是

  1. 与确实需要参数的子例程的视觉一致性,
  2. 它不能与不同的关键字混淆。

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.
滥情空心 2024-08-24 20:59:31

作为一般规则,我建议如下:

  1. 除非您需要 &因为您正在超越内置函数,或者您没有参数列表,所以省略它。

  2. 始终包含 (),如在 Marine() 中。

我这样做是为了代码的可读性。第一条规则清楚地表明我何时通过使内部 Perl 函数变得不同来覆盖它们。第二个清楚地表明我何时调用函数。

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.

套路撩心 2024-08-24 20:59:31

Perl 允许您在函数调用中省略括号。
因此,您可以通过两种不同的方式使用参数调用函数:
your_function(arg1,arg2,arg3);

你的函数arg1,arg2,arg3;
您更喜欢哪种形式,这是一个选择问题。对于具有 C 语言背景的用户来说,前者更为直观。
我个人将前者用于我定义的函数,后者用于内置函数,例如:

print "something" 而不是 print("something")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文