() 在 Perl 中的子例程定义中完成什么作用?

发布于 2024-09-29 06:49:56 字数 244 浏览 2 评论 0原文

以下代码直接从 Tie::File 模块的源代码中提取。在这种情况下,O_ACCMODE 定义中的空括号有何作用?我知道子例程原型的用途是什么,但这种用法似乎与此无关。

use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONLY';
sub O_ACCMODE () { O_RDONLY | O_RDWR | O_WRONLY }

The following code is lifted directly from the source of the Tie::File module. What do the empty parentheses accomplish in the definition of O_ACCMODE in this context? I know what subroutine prototypes are used for, but this usage doesn't seem to relate to that.

use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONLY';
sub O_ACCMODE () { O_RDONLY | O_RDWR | O_WRONLY }

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

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

发布评论

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

评论(3

始终不够爱げ你 2024-10-06 06:49:57

() 的原型使子例程符合内联条件。例如,constant pragma 使用它。

请参阅 perlsub 中的常量函数

The prototype of () makes the subroutine eligible for inlining. This is used by the constant pragma, for example.

See Constant Functions in perlsub.

这样的小城市 2024-10-06 06:49:56

它还告诉解析器 O_ACCMODE 在任何情况下都不接受参数(&O_ACCMODE() 除外,您可能永远不必考虑它)。这使得它的行为就像大多数人期望的那样。

举个简单的例子,在:

sub FOO { 1 }
sub BAR { 2 }

print FOO + BAR;

最后一行解析为 print FOO(+BAR()) 并且打印的值为 1,因为当在没有括号的情况下调用无原型 sub 时,它会尝试像 listop 一样运行并尽可能正确地消化术语。

In:

sub FOO () { 1 }
sub BAR () { 2 }

print FOO + BAR;

最后一行解析为 print FOO() + BAR() 并且打印的值为 3,因为 () 原型告诉解析器没有参数 FOO 是预期的或有效的。

It also tells the parser that O_ACCMODE doesn't take an argument under any condition (except &O_ACCMODE() which you will likely never have to think about). This makes it behave like most people expect a constant to.

As a quick example, in:

sub FOO { 1 }
sub BAR { 2 }

print FOO + BAR;

the final line parses as print FOO(+BAR()) and the value printed is 1, because when a prototypeless sub is called without parens it tries to act like a listop and slurp terms as far right as it can.

In:

sub FOO () { 1 }
sub BAR () { 2 }

print FOO + BAR;

The final line parses as print FOO() + BAR() and the value printed is 3, because the () prototype tells the parser that no arguments to FOO are expected or valid.

泛滥成性 2024-10-06 06:49:56

来自 perlsub 关于常量函数的主题:

原型为 () 的函数是
内联的潜在候选者

From perlsub on the topic of constant functions:

Functions with a prototype of () are
potential candidates for inlining

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