I'm reading manuals and learning about context being imposed on variables.
In line 1 below, the parens around $a1 force a scalar context. The next line prints out 55 so the variable was assigned the FIRST value in the list. This makes sense because someone might write ($a1,@b) = (55,77,99) intending that the first value be assigned to $a1 and the remaining values go to @b as an array.
my ($a1) = (55,77,99); print $a1."\n";
So then I read that in the case of a hash, you force scalar by putting the keyword "scalar" in front of the list. So in the following example, I EXPECTED that $a2{'y'} would get the FIRST value in the list (as it did in the first example above) but it instead gets "9".
my %a2 = ( x=>4, y=> scalar (5,7,9) ); print $a2{'y'} . "\n";
Why would the assignment of a list in scalar context behave differently in these two scenarios?
Thanks for any help or links to an explanation.
Eric
In reply to Newbie question - imposing context by erictully
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |