I'm really struggling to articulate my question. The real question is: If there's only one value in the return list, how can I return that value in scalar context, not the count of 1?.
sub lowercase {
my @out = map { lc } @_ ;
return @out == 1 && ! wantarray ? $out[0]: @out ;
}
This code is optimized based on all the answers so far, and doesn't use first either. In fact, I've probably used this construction thousands of times.
My question is based on 3 assumptions:
- As mentioned, I prefer to avoid using placeholder variables.
- I find it awkward to refer to 3 forms of a variable in a single statement.
- If I've used this construction thousands of times, and so have others, why hasn't someone developed a single operator?
I assumed that List::Util would include this functionality. That is, if first or some variant could determine and respond to the wantarray context.
UPDATE
(no update)
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.