如何引用perl sub的返回值

Kyl*_*yle 6 syntax perl reference return-value

代码:

my $compare = List::Compare->new(\@hand, \@new_hand);
print_cards("Discarded", $compare->get_Lonly()) if ($verbose);
Run Code Online (Sandbox Code Playgroud)

print_cards期望(标量,对数组的引用).
get_Lonly返回数组.将它转换为引用的语法是什么,所以我可以将它传递给print_cards? \@{$compare->getLonly()}例如,不起作用.

谢谢!

amo*_*mon 14

你可能想要

print_cards("Discarded", [$compare->get_Lonly])
Run Code Online (Sandbox Code Playgroud)

子例程不返回数组,它们返回值列表.我们可以创建一个数组引用[...].

另一种变体是制作一个显式数组

if ($verbose) {
  my @array = $compare->get_Lonly;
  print_cards("Discarded", \@array)
}
Run Code Online (Sandbox Code Playgroud)

第一个解决方案是这个的捷径.


@{ ... }是一个解引用运算符.它需要一个数组引用.如果你给它一个列表,这不会像你想象的那样工作.