Rudd Zwolinski Rudd Zwolinski - 7 months ago 68
Perl Question

Can you force either a scalar or array ref to be an array in Perl?

I have a perl variable

that gets returned from a service. The value is supposed to be an array, and
should be an array reference. However, when the array has only one item in it,
will be set to that value, and not a referenced array that contains that one item.

I want to do a
loop on the expected array. Without checking
ref($results) eq 'ARRAY'
, is there any way to have something equivalent to the following:

foreach my $result (@$results) {
# Process $result

That particular code sample will work for the reference, but will complain for the simple scalar.

EDIT: I should clarify that there is no way for me to change what is returned from the service. The problem is that the value will be a scalar when there is only one value and it will be an array reference when there is more than one value.


im not sure there's any other way than:

$result = [ $result ]   if ref($result) ne 'ARRAY';  
foreach .....