Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-03-19 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found