Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: wantarray alternative

by tqisjim (Beadle)
on Jul 10, 2013 at 19:38 UTC ( #1043543=note: print w/replies, xml ) Need Help??

in reply to Re: wantarray alternative
in thread wantarray alternative

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:

  1. As mentioned, I prefer to avoid using placeholder variables.
  2. I find it awkward to refer to 3 forms of a variable in a single statement.
  3. 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)

Replies are listed 'Best First'.
Re^3: wantarray alternative
by LanX (Bishop) on Jul 10, 2013 at 19:53 UTC
    > If there's only one value in the return list, how can I return that value in scalar context, not the count of 1?

    just return the list! In scalar context the last element is taken, so if you are only returning "one value in the return list" thats the same.

    It's the scalar of an array(!) which counts.


    ok not that easy, avoid scalar context and explicitly put the scalar into a list at LHS.

    DB<129> @out=A..C => ("A", "B", "C") DB<130> sub tst { return map{lc} @out } DB<131> @list = tst() => ("a", "b", "c") DB<132> ($scalar) = tst() => "a"

    Though we asked several times you haven't clarified which result you expect in in case of scalar context and longer list!

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I'm having trouble following the thread on CB.
      sub lowercase { return map { lc } @_ ; } $jim = lowercase( 'Jim' ) ; ## $jim == 1
      Not the first, not the last, just the count, right?
        before I answer this, please clarify which is the result expected for

          $name = lowercase( 'Jim',"Jeff" ) ;


        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^3: wantarray alternative
by davido (Archbishop) on Jul 10, 2013 at 19:59 UTC
    return @out == 1 && ! wantarray ? $out[0] : @out;

    So if @out has a single element, return '1'. If it contains zero or 2, or more elements, then if "wantarray", return @out, otherwise return the first element.

    I can't imagine using that once, let alone thousands of times. ;)

    Update: Woops, "&&" is higher on the precedence table than "?:" in any language I know of. My mistake.


      Oops. Thanks for catching that :)
      return ( @out == 1 && ! wantarray ) ? $out[0] : @out;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043543]
and a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-06-19 14:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.