如何将 Perl 数组引用中的元素作为单独的参数传递给子例程?
我有一个列表,其中包含我想要传递给函数的参数。我如何调用该函数?
例如,假设我有这个函数:
sub foo {
my ($arg0, $arg1, $arg2) = @_;
print "$arg0 $arg1 $arg2\n";
}
假设我有:
my $args = [ "la", "di", "da" ];
如何在不编写 foo($$args[0], $$args[1], $$ 的情况下调用
?foo
args[2])
I have a list that contains arguments I want to pass to a function. How do I call that function?
For example, imagine I had this function:
sub foo {
my ($arg0, $arg1, $arg2) = @_;
print "$arg0 $arg1 $arg2\n";
}
And let's say I have:
my $args = [ "la", "di", "da" ];
How do I call foo
without writing foo($$args[0], $$args[1], $$args[2])
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过在数组引用前面添加
@
来取消引用该数组引用。或者如果你想更明确:
You dereference an array reference by sticking
@
in front of it.Or if you want to be more explicit:
这应该可以做到:
这实际上不是一个
apply
函数。该语法只是将数组引用取消引用回普通数组。 man perlref 告诉您有关引用的更多信息。This should do it:
That is not actually an
apply
function. That syntax just dereferences an array reference back to plain array. man perlref tells you more about referecences.试试这个:
Try this:
或者,如果您有对
foo
的引用:Or, if you have a reference to
foo
:这很简单。 foo(@{$args})
It's simple. foo(@{$args})