Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..

by ikegami (Pope)
on Oct 19, 2011 at 19:04 UTC ( #932477=note: print w/replies, xml ) Need Help??


in reply to Is map's sub block really being evaluated in list context? wantarray() returns undef..

Aside from being documented ("Evaluates BLOCK or EXPR in list context"), it's very obviously evaluated in list context.

$ perl -E'say map { scalar( $_, $_ ) } 4;' 4 $ perl -E'say map { $_, $_ } 4;' 44

Even when map isn't.

$ perl -E'say scalar map { scalar( $_, $_ ) } 4;' 1 $ perl -E'say scalar map { $_, $_ } 4;' 2

wantarray "returns true if the context of the currently executing subroutine or eval is looking for a list value", emphasis mine.

$ perl -E'@a = sub { map { say wantarray // "undef" || 0 } 1; 1 }->()' 1 $ perl -E'$a = sub { map { say wantarray // "undef" || 0 } 1; 1 }->()' 0 $ perl -E' sub { map { say wantarray // "undef" || 0 } 1; 1 }->()' undef

map itself is in void context in each of those. The block is evaluated in list context in each of those. Only the context in which the subroutine is evaluated is changing. This is what wantarray reports.

wantarray works as documented in map.

  • Comment on Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932477]
help
Chatterbox?
[marioroy]: Wow. It's cool seeing Github coments "Update Monster.pm, Create Wild_hairs, Create alternate- PurpleOnGreen..., Then a location, Create Aleenia. Amazing, really. Thank you for sharing.
Discipulus when naming is clear and you know how to arrange the data, then coding it is a mere writing work
[Lady_Aleena]: marioroy, welcome to my insanity. 8)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-05-29 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?