Sample Code:

use List::Util qw( first ) ; sub lowercase { return first {$_} map { lc } @_ unless wantarray ; return map { lc } @_ ; } $jim = lowercase( 'Jim' ) ;

I've exhausted my imagination trying to reduce the lowercase function to a single line. But surely this problem has been solved by better minds than mine. Monks?

Thanks! -Jim

UPDATED I've posted several clarifications based on the replies so far. My original question is whether there's an equivalent function to first that is context sensitive.

UPDATE 2

Three conditions: @me = lowercase( qw( TQIS Jim ) ) ; ## returns qw( tqis jim ) $me = lowercase( 'Jim' ) ; ## returns 'jim' @me = lowercase( qw( Jim ) ) ; ## returns qw( jim )

UPDATE 3

tye's solution below, which uses a separate function named context, is elegant and concise and pretty close to what I'm looking for.

To restate my question one final time: What is the right way to get perl to do the right thing? I assumed there'd be an obvious answer. Although no consensus, I *did* get a lively conversation.


In reply to wantarray alternative by tqisjim

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.