davido has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
by BrowserUk (Patriarch) on Oct 19, 2011 at 16:29 UTC | |
by moritz (Cardinal) on Oct 20, 2011 at 10:35 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2011 at 15:07 UTC | |
|
Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
by ikegami (Patriarch) on Oct 19, 2011 at 19:04 UTC | |
|
Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
by GrandFather (Saint) on Oct 19, 2011 at 19:59 UTC |