我试图理解这个 Perl 代码......
如果有一个流它可以工作,如果有 2 个或更多流它会警告匿名哈希中的奇数个元素。在这种情况下,它似乎返回了一个数组。如何将数组元素正确添加到@streams?它似乎在 if 子句中正确添加了 HASH 案例。else 子句是假的吗?
my $x = $viewedProjectDataObj->{streams};
if (ref($x) eq 'HASH') {
push(@streams, $x->{id});
} elsif (ref($x) eq 'ARRAY') {
print "$x\n";
print "@$x\n";
my @array = @$x;
foreach my $obj (@array) {
print "in $obj\n";
print Dumper( $obj);
push(@streams, ($obj->{id}) );
}
}
print "streamcount " . @streams % 2;
print Dumper(@streams);
my $stream_defect_filter_spec = {
'streamIdList' => @streams,
'includeDefectInstances' => 'true',
'includeHistory' => 'true',
};
my @streamDefects = $WS->get_stream_defects($defectProxy, \@cids, $stream_defect_filter_spec);
print Dumper(@streamDefects);
我正在添加下一行...
if ($defectSummary->{owner} eq "Various") {
foreach (@streamDefects) {
if (exists($_->{owner})) {
$defectSummary->{owner} = $_->{owner};
last;
}
}
}
my $diref = $streamDefects[0]->{defectInstances};
if ($diref) {
my $defectInstance;
if (ref($diref) eq 'HASH') {
$defectInstance = $diref;
} elsif (ref($diref) eq 'ARRAY') {
$defectInstance = @{$diref}[0];
} else {
die "Unable to handle $diref (".ref($diref).")";
}
它现在错误
Web API 返回错误代码 S:Server: calling getStreamDefects: No stream found for name null。$VAR1 = -1; 我在 xyz-handler.pl 第 317 行使用“strict refs”时不能使用字符串(“-1”)作为 HASH 引用。
一些 Dumper 输出
$VAR1 = {
'streamIdList' => [
{
'name' => 'asdfasdfadsfasdfa'
},
{
'name' => 'cpp-62bad47d63cfb25e76b29a4801c61d8d'
}
],
'includeDefectInstances' => 'true',
'includeHistory' => 'true'
};