如何将列表中的每个元素作为函数的参数传递

How to pass each element in a list as arguments of a function

我的数据在一个数组中。我需要将数组值传递给 ars_CreateEntry 中的函数 ARSperl。 该数组是根据收到的输入动态形成的,因此它的长度可能会有所不同。

#!/usr/bin/perl

use strict;

use ARS;
use ARS::Login;

my $server   = '125.10.14.1';
my $username = 'RemedyUser';
my $dbsid    = $ENV{'ORACLEID'};
my $dbuser   = 'admin';
my $schema   = 'Form 1';

my $control = remedyars_login( $username )
        or die "ERROR: Cannot login to $server \n";
my $dbh = remedydbi_login( $dbsid, $dbuser )
        or die( "Cannot connect to $dbsid: $DBI::errstr\n" );

my @arr = ( 1, "New", 2, "Test", 3, "Hi" );
my $created_ticket = ars_CreateEntry( $control, $schema, join( ",", @arr ) )
        or die "ERROR: Cannot create host ticket: $ars_errstr\n";

ars_Logoff( $control );

当我 运行 脚本时出现此错误

ERROR: Cannot create host ticket: [ERROR] Invalid number of arguments (ARERR #80000)

直接使用数组(参见ars_CreateEntry examples):

my @arr=(1,"New",2,"Test",3,"Hi");
die unless @arr % 2 == 0; # enforce even number of elements in @arr
my $created_ticket=ars_CreateEntry( $control, $schema, @arr) 
  or die "ERROR: Cannot create host ticket: $ars_errstr\n";

如果 ars_CreateEntry 对参数的顺序不挑剔,最好使用散列而不是数组:

my %params=(1 => "New",2 => "Test", 3 => "Hi");
# hash will be converted to array
my $created_ticket=ars_CreateEntry( $control, $schema, %params) 
  or die "ERROR: Cannot create host ticket: $ars_errstr\n";