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.
- some_function() will almost always return a single thing.
- We know this because some_function() expects that the assignment is always to a scalar, otherwise why normalize to a scalar?
- But, since some_function() builds an array, it might return more than one thing.
- 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:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.