[Edinburgh-pm] a Perl surprise

Jeff Parsons bynari at gmail.com
Wed Jul 18 10:11:09 PDT 2012


On 18/07/2012 16:05, Aaron Crane wrote:
>
> There isn't really any such thing as a list in scalar context, at
> least in one reasonable analysis of how all this works.  Instead, this
> is about the behaviour of the comma operator: in list context, the
> comma operator constructs a list, while in scalar context, it discards
> the operand on its left-hand side and returns the operand on its
> right-hand side.
>
> qw is defined to give you the same behaviour: it yields a list in list
> context, or the last element in scalar context).
>
> My "no list in scalar context" claim may sound like nitpicking, but it
> matters for code like this:
>
>    my @x = (10 .. 12);
>    my $y = (6, 7, @x);
>
> Given that lists always flatten, treating the assignment to $y as a
> "list in scalar context" suggests that it would be the same as
>
>    my $y = (6, 7, 10, 11, 12);
>
> which would set $y to 12, as you've discovered.  But in fact, the
> sequence of events is:
>
>



Yeah, I also used the phrase 'list in scalar context', but I thought it 
was possibly a bit iffy as yes it's actually the *comma operator* in 
list context or the comma operator in scalar context. Thanks for the 
example with the array, that really shows clearly why there's definitely 
no such thing as a 'list in scalar context'!





More information about the Edinburgh-pm mailing list