From the documentation for map:
Evaluates BLOCK or EXPR in list context, so each element of LIST may produce zero, one, or more elements in the returned value.
use strict; use warnings; use v5.12; my @array = map{ wantarray() } 0 .. 1; my @rv = test_context(); sub test_context { wantarray(); } print "\@array = map: sub block is evaluated in "; if( defined( $array[0] ) ) { say $array[0]? "list context" : "scalar context"; } else { say "void context"; } print "\@rv = test_context(): sub evaluated in "; if( defined( $rv[0] ) ) { say $rv[0] ? "list context" : "scalar context"; } else { say "void context"; }
And the output is...
@array = map: sub block is evaluated in void context @rv = test_context(): sub evaluated in list context
wantarray returns undef in void context.
What is going on here?
Dave
In reply to Is map's sub block really being evaluated in list context? wantarray() returns undef.. by davido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |