tqisjim has asked for the wisdom of the Perl Monks concerning the following question:
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.
Three conditions: @me = lowercase( qw( TQIS Jim ) ) ; ## returns qw( tqis jim ) $me = lowercase( 'Jim' ) ; ## returns 'jim' @me = lowercase( qw( Jim ) ) ; ## returns qw( jim )
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.