I'm trying to understand this Perl code...
If there is one stream it works, if there are 2 or more streams it warns with odd number of elements in anonymous hash. It seems to return an array in that case. How do I add the array elements correctly to @streams? It appears to add correctly for the HASH case in the if clause. Is the else clause bunk?
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);
I'm adding the next lines...
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).")";
}
It now errors with
Web API returned error code S:Server: calling getStreamDefects: No stream found for name null. $VAR1 = -1; me Can't use string ("-1") as a HASH ref while "strict refs" in use at xyz-handler.pl line 317.
some Dumper output
$VAR1 = {
'streamIdList' => [
{
'name' => 'asdfasdfadsfasdfa'
},
{
'name' => 'cpp-62bad47d63cfb25e76b29a4801c61d8d'
}
],
'includeDefectInstances' => 'true',
'includeHistory' => 'true'
};