Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Just another Perl shrine
 
PerlMonks  

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

by davido (Archbishop)
on Oct 19, 2011 at 16:00 UTC ( #932438=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Is map's sub block really being evaluated in list context? wantarray() returns undef..
Select or Download Code
Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
by BrowserUk (Pope) on Oct 19, 2011 at 16:29 UTC

    My first pass assessment would be that maps block doesn't qualify as a sub in the normal sense:

    print map{ print wantarray(); (1,2,3) } 1;; Use of uninitialized value in print at (eval 19) line 1, <STDIN> line +11. 1 2 3

    It was obviously called in a list context, else the return would be different:

    print scalar map{ print wantarray(); (1,2,3) } 1;; Use of uninitialized value in print at (eval 20) line 1, <STDIN> line +12. 3

    This implies that the block is not a true sub as far as wantarray is concerned. It is just a block as with:

    print do{ print wantarray(); (1,2,3) };; Use of uninitialized value in print at (eval 23) line 1, <STDIN> line +15. 1 2 3

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

        I suspect that as a built-in, map avails itself of more optimisations than it would be safe to apply to a user-written function.

        I know from experience that if you implement a callback interface in XS using the full belts'n'braces approach described in perlcall, then the result is far less efficient than the carefully optimised callback interface used by List::Util::reduce().

        The downside of trying to match that level of optimisation with your own XS code is that it is very easy to create the situation whereby the callback leaks memory if it contains closures. (The early versions of reduce() suffered from the same problems.)

        I think it would be very difficult, if not impossible to compile user-written, runtime-compiled subs taking callbacks, to be as efficient as map. And to 'fix the inconsistency' would essentially require pessimising the implementation of map

        So no, I don't think it is worth a bug report. At most, a documentation change noting the difference might be useful.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
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

    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.

Re: Is map's sub block really being evaluated in list context? wantarray() returns undef..
by GrandFather (Cardinal) on Oct 19, 2011 at 19:59 UTC

    wantarray's documentation says it all really. wantarray is sensitive to the "subroutine or eval" context and is "unspecified in the top level of a file". map isn't a sub or eval so doesn't provide any new context for wantarray.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932438]
Approved by BrowserUk
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-04-19 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls