Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: wantarray alternative (Context propagation)

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


in reply to wantarray alternative

context is propagated into the return statement.

see Context propagation into subs and evals

and according to perldoc: map in scalar context, returns the total number of elements so generated.

But the following code forces the map-statement into a sliced list.

DB<156> sub tst { (map {lc} @_)[0.. wantarray * $#_] } DB<157> @list=tst("A".."D") => ("a", "b", "c", "d") DB<158> $scalar=tst("A".."D") => "a"

and if the last value is enough for you in scalar context do

DB<159> sub tst { (map {lc} @_)[0.. $#_] } DB<160> @list=tst("A".."D") => ("a", "b", "c", "d") DB<161> $scalar=tst("A".."D") => "d"

you haven't answered my question in Re^5: wantarray alternative yet, so you have both alternatives now.

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: wantarray alternative (Context propagation)
Select or Download Code
Re^2: wantarray alternative
by tqisjim (Beadle) on Jul 10, 2013 at 21:24 UTC

    I don't seem to be answering as fast as you're asking.

    If the explanation is that map is being called in scalar context, then the following is unexpected:

    sub lowercase { my @out = map { lc } @_ ; return @out ; } $name = lowercase( 'Jim', "Jeff" ) ; ## $name == 2

    Even though map is called in an array context, the results don't change.

    Very clever solution, yours. Clever, but not elegant.

      > If the explanation is that map is being called in scalar context, then the following is unexpected:

      no! whatever comes after return is executed in the context of the sub's call.

      so in this case¹ $name = scalar @out

      It's true many people expect a LIST to be returned but thats generally wrong in scalar context.

      A helper routine  sub listify { (@_)[0..$#_] } might help to assure this "expected" behaviour, whenever you need to return a LIST².

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      PS: please read the thread I linked before asking more questions.

      ¹) which isn't the code from the post I replied to, where a map-statement is returned

      UPDATE

      > Clever, but not elegant.

      Larry will be so devastated to hear this ... :´(

      UPDATE

      ²) FWIW

      DB<112> sub listify { (@_)[0..$#_] } DB<113> sub tst { listify map{lc} @_ } DB<114> $s =tst("A".."D") => "d" DB<115> @l =tst("A".."D") => ("a", "b", "c", "d")

        Larry will be so devastated to hear this ... :´(

        Nuff said

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2015-07-02 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls