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

Re^3: wantarray alternative

by LanX (Canon)
on Jul 10, 2013 at 19:53 UTC ( #1043544=note: print w/ replies, xml ) Need Help??


in reply to Re^2: wantarray alternative
in thread wantarray alternative

> 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.

UPDATE

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)


Comment on Re^3: wantarray alternative
Download Code
Re^4: wantarray alternative
by tqisjim (Beadle) on Jul 10, 2013 at 20:22 UTC
    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)

        OK, since the day is shot anyway...

        What do experienced programmers expect? It seems like there was a lot of confusion on CB about the first or last element (both wrong).

        I expect the answer is 2, which I've confirmed by testing. But obviously, I've seen the source code.

        If I were looking at that statement without seeing the source code, I would have to test or read the source code to expect anything at all. Since the statement is called lowercase (I might've used convert_to_lowercase), one is unlikely to use this function to count the number of input arguments. So intuitively, the statement should not return 2. In summary, the point of my effort.

        The solution I'd prefer to implement would return Jim. But the most robust answer would probably be to throw an exception.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-09-21 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls