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