develooper Front page | perl.perl5.porters | Postings from January 2009

Re: Context of early elements of list in scalar context

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
January 11, 2009 15:10
Subject:
Re: Context of early elements of list in scalar context
Message ID:
40877.97.113.87.89.1231715441.squirrel@webmail.efn.org
On Sun, January 11, 2009 1:58 pm, Eric Brine wrote:
> Is there a reason for the following difference in behaviour?
>
>
> use strict; use warnings;
>
> sub f { if ( wantarray() ) { print "list\n"; }
> elsif ( defined wantarray() ) { print "scalar\n"; }
> else { print "void\n"; }
> }
>
>
> my $x =     ( f(), f(), f() );      print("\n");  # v,v,s
> my $y = sub { f(), f(), f() }->();  print("\n");  # s,s,s
>
> (Perl 5.6.0, 5.6.1, 5.8.0, 5.8.8, 5.10.0)

Yes, though only due to the implementation.

Operations can have context known at compile time, or they can inherit
context from the call stack.

The last f call can correctly inherit context, but the first two f calls
can't correctly be classified into one of the two above categories, since
they must have list context when the anon sub is called in list context,
but should ideally have void context when the anon sub call is in either
void or scalar context.  Having them inherit scalar context is as minimally
wrong as perl can get.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About