Perl 中函数的别名

发布于 2024-10-08 19:45:43 字数 165 浏览 0 评论 0原文

在 Perl 中,如果我希望 foo() 完全执行 bar() 的操作,我可以这样做:

sub foo {return bar(@_);} 

有更好的方法吗?更接近 Ruby 的“别名”运算符吗?

In Perl, if I want foo() to do exactly what bar() does, I can do this:

sub foo {return bar(@_);} 

Is there a better way? Something closer to Ruby's "alias" operator?

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

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

发布评论

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

评论(4

小草泠泠 2024-10-15 19:45:43

Exporter 和类似模块采用的更好的方法是编辑符号表:(

*foo = \&bar;

编辑是因为我们正在讨论的是 Perl)

The A better way, employed by Exporter and similar modules, is to edit your symbol table:

*foo = \&bar;

(edited because this is Perl we are talking about)

素染倾城色 2024-10-15 19:45:43

还有另一种方法可以做到这一点:

sub foo {goto &bar}

即使随后重新定义了 bar,这也保留了 foobar 之间的等效性。

这是一个详细的演示:

use strict;
use warnings;

sub bar {
    print "This is the original bar().\n";
}

*bar_copy = \&bar;

sub bar_link {
    goto &bar;
}

print "Executing bar_copy:\n";
bar_copy();
print "Executing bar_link:\n";
bar_link();

*bar = sub {print "This is bar(), redefined.\n"};

print "Executing bar_copy after redefinition:\n";
bar_copy();
print "Executing bar_link after redefinition:\n";
bar_link();

打印

Executing bar_copy:
This is the original bar().
Executing bar_link:
This is the original bar().
Subroutine main::bar redefined at C:\scripts\temp.pl line 19.
Executing bar_copy after redefinition:
This is the original bar().
Executing bar_link after redefinition:
This is bar(), redefined.

There is another way to do it:

sub foo {goto &bar}

This preserves the equivalence between foo and bar even if bar is subsequently redefined.

Here is a detailed demo:

use strict;
use warnings;

sub bar {
    print "This is the original bar().\n";
}

*bar_copy = \&bar;

sub bar_link {
    goto &bar;
}

print "Executing bar_copy:\n";
bar_copy();
print "Executing bar_link:\n";
bar_link();

*bar = sub {print "This is bar(), redefined.\n"};

print "Executing bar_copy after redefinition:\n";
bar_copy();
print "Executing bar_link after redefinition:\n";
bar_link();

which prints

Executing bar_copy:
This is the original bar().
Executing bar_link:
This is the original bar().
Subroutine main::bar redefined at C:\scripts\temp.pl line 19.
Executing bar_copy after redefinition:
This is the original bar().
Executing bar_link after redefinition:
This is bar(), redefined.
俯瞰星空 2024-10-15 19:45:43
*foo = \&bar;

分配对 glob 的引用会将 glob 的该部分替换为所引用的事物。所以这使得 &foo 与 &bar 完全相同。

*foo = *bar;  # or \*bar

这也有效,但也为 foo 和 bar 之间的标量、数组、散列、文件句柄和格式设置别名。

*foo = \&bar;

Assigning a reference to a glob replaces that portion of the glob with the thingy referred to. So this makes &foo exactly the same as &bar.

*foo = *bar;  # or \*bar

This also works, but also aliases the scalar, array, hash, filehandle, and format between foo and bar.

墟烟 2024-10-15 19:45:43

Perlmonks 提供:

sub bar { print( "Hello World\n" ); }

BEGIN { *foo = \&bar; }

foo();
bar();

Courtesy Perlmonks:

sub bar { print( "Hello World\n" ); }

BEGIN { *foo = \&bar; }

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