<p>I have a perl variable <code>$results</code> that gets returned from a service.  The value is supposed to be an array, and <code>$results</code> should be an array reference.  However, when the array has only one item in it, <code>$results</code> will be set to that value, and not a referenced array that contains that one item.</p>

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

<pre><code>foreach my $result (@$results) {
    # Process $result
}
</code></pre>

<p>That particular code sample will work for the reference, but will complain for the simple scalar.</p>

<p>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.</p>

[{'answer_id': 3126, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': -1, 'selected': False, 'text': '<p>I\'ve just tested this with:</p>\n\n<blockquote>\n<pre><code>#!/usr/bin/perl -w\nuse strict;\n\nsub testit {\n\n my @ret = ();\n if (shift){\n   push @ret,1;\n   push @ret,2;\n   push @ret,3;\n}else{\n  push @ret,"oneonly";\n}\n\nreturn \\@ret;\n}\n\nforeach my $r (@{testit(1)}){\n  print $r." test1\\n";\n}\nforeach my $r (@{testit()}){\n   print $r." test2\\n";\n}\n</code></pre>\n</blockquote>\n\n<p>And it seems to work ok, so I\'m thinking it has something to do with the result getting returned from the service?\nIf you have no control over the returning service this might be hard one to crack</p>\n'}, {'answer_id': 3134, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 6, 'selected': True, 'text': "<p>im not sure there's any other way than:</p>\n\n<blockquote>\n<pre><code>$result = [ $result ]   if ref($result) ne 'ARRAY';  \nforeach .....\n</code></pre>\n</blockquote>\n"}, {'answer_id': 11722, 'author': 'Brad Gilbert', 'author_id': 1337, 'author_profile': 'https://Stackoverflow.com/users/1337', 'pm_score': 0, 'selected': False, 'text': "<p>I would re-factor the code inside the loop and then do</p>\n\n<pre><code>if( ref $results eq 'ARRAY' ){\n    my_sub($result) for my $result (@$results);\n}else{\n    my_sub($results);\n}\n</code></pre>\n\n<p>Of course I would only do that if the code in the loop was non-trivial.</p>\n"}, {'answer_id': 16243, 'author': 'user1917', 'author_id': 1917, 'author_profile': 'https://Stackoverflow.com/users/1917', 'pm_score': 4, 'selected': False, 'text': '<p>Another solution would be to wrap the call to the server and have it always return an array to simplify the rest of your life:</p>\n\n<pre><code>sub call_to_service\n{\n    my $returnValue = service::call();\n\n    if (ref($returnValue) eq "ARRAY")\n    {\n        return($returnValue);\n    }\n    else\n    {\n       return( [$returnValue] );\n    }\n}\n</code></pre>\n\n<p>Then you can always know that you will get back a reference to an array, even if it was only one item.</p>\n\n<pre><code>foreach my $item (@{call_to_service()})\n{\n  ...\n}\n</code></pre>\n'}, {'answer_id': 196099, 'author': 'draegtun', 'author_id': 12195, 'author_profile': 'https://Stackoverflow.com/users/12195', 'pm_score': 2, 'selected': False, 'text': "<p>Well if you can't do...</p>\n\n<pre><code>for my $result ( ref $results eq 'ARRAY' ? @$results : $results ) {\n    # Process result\n}\n</code></pre>\n\n<p>or this...</p>\n\n<pre><code>for my $result ( ! ref $results ? $results : @$results ) {\n    # Process result\n}\n</code></pre>\n\n<p>then you might have to try something hairy scary like this!....</p>\n\n<pre><code>for my $result ( eval { @$results }, eval $results ) {\n    # Process result\n}\n</code></pre>\n\n<p>and to avoid that dangerous string eval it becomes really ugly fugly!!....</p>\n\n<pre><code>for my $result ( eval { $results-&gt;[0] } || $results, eval { @$results[1 .. $#{ $results }] } ) {\n    # Process result\n}\n</code></pre>\n\n<p>PS. My preference would be to abstract it away in sub ala call_to_service() example given by reatmon.</p>\n"}, {'answer_id': 40756797, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>You can do it like this:</p>\n\n<pre><code>my @some_array\npush (@some_array, results);\nforeach my $elt(@some_array){\n  #do something\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/3112', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/219/']