The eval method (or the isArray() I've told tye to submit somewhere on CPAN) is the best way to find out if something responds to @{}.

In general, I find that code like that is attempting to compensate for "clever" code elsewhere. And, by "clever", I mean "poorly written". I'm betting that whatever populates $x does something like:

sub some_function { # Stuff here that populates @x return @x > 1 ? \@x : $x[0]; }
That meme is one of the most horrendous ones to work with.
  1. some_function() will almost always return a single thing.
  2. We know this because some_function() expects that the assignment is always to a scalar, otherwise why normalize to a scalar?
  3. But, since some_function() builds an array, it might return more than one thing.
  4. So, the caller has to know this and normalize back to an array.

So, given that the proper normalization (as performed by sane callers) is to an array, why is some_function() normalizing to a scalar? Simply put, there's no good reason to, other than FUD. Remove the FUD, trust the array, and stop the silliness.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: elegant array assignment by dragonchild
in thread elegant array assignment by lackita

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.