Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-10-21 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (109 votes), past polls