In line 1 below, the parens around $a1 force a scalar context.
Actually, that's list context.
What you're seeing here is how the comma operator behaves in list and scalar context. See perlop - Comma Operator for details.
As a further example, compare scalar context (as in your example):
$ perl -Mstrict -Mwarnings -E 'my %a2 = ( x=>4, y=> scalar (5,7,9) ); +say $a2{y}' Useless use of a constant (5) in void context at -e line 1. Useless use of a constant (7) in void context at -e line 1. 9
with list context (removing scalar from your code):
$ perl -Mstrict -Mwarnings -E 'my %a2 = ( x=>4, y=> (5,7,9) ); say $a2 +{y}' 5
Update: Actually, a better example (for the list context) would be:
$ perl -Mstrict -Mwarnings -E 'my %a2 = ( x=>4, y=> (5,7,9) ); say $a2 +{y}; say $a2{7}' 5 9
-- Ken
In reply to Re: Newbie question - imposing context
by kcott
in thread Newbie question - imposing context
by erictully
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |