簡體   English   中英

如何將Perl數組引用中的元素作為子例程的單獨參數傳遞?

[英]How can I pass the elements in a Perl array reference as separate arguments to a subroutine?

我有一個列表,其中包含我想傳遞給函數的參數。 我該如何調用該功能?

例如,假設我有這個功能:

sub foo {
  my ($arg0, $arg1, $arg2) = @_;
  print "$arg0 $arg1 $arg2\n";
}

讓我們說我有:

my $args = [ "la", "di", "da" ];

如何在不寫foo情況下調用foo foo($$args[0], $$args[1], $$args[2])

您可以通過在其前面粘貼@來取消引用數組引用。

foo( @$args );

或者如果你想更明確:

foo( @{ $args } );

這應該這樣做:

foo(@$args)

這實際上不是一個apply函數。 該語法只是將數組引用取消引用回到plain數組。 man perlref告訴你更多關於referecences

嘗試這個:

foo(@$args);
foo(@$args);

或者,如果你有foo的引用:

my $func = \&foo;
...
$func->(@$args);

這很簡單。 FOO(@ {$ ARGS})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM