将数组展开到perl中的参数列表

Pab*_*mer 1 perl arguments perl-module

我有类似下面的代码

my @array = ["hello","hi","fish"];
sub this_sub {
  my $first = $_[0];
  my $second = $_[1];
  my $third = $_[2];
}
this_sub(@array);
Run Code Online (Sandbox Code Playgroud)

如何使数组扩展为参数列表,以便第一个,第二个和第三个将从数组中的字符串中获取值.如下.

  • first ="你好"
  • second ="hi"
  • 第三="鱼"

cho*_*oba 5

你的代码错了.要将列表分配给数组,请将其括在正常的括号中:

my @array = ("hello", "hi", "fish");
Run Code Online (Sandbox Code Playgroud)

方括号定义一个匿名数组,即对列表的引用,这是一个标量:

my $array_ref = ["hello", "hi", "fish"];
Run Code Online (Sandbox Code Playgroud)

如果要发送引用,则必须在sub中取消引用它:

sub this_sub {
    my ($first, $second, $third) = @{ $_[0] };
}
Run Code Online (Sandbox Code Playgroud)