erictully has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie question - imposing context
by ikegami (Patriarch) on May 23, 2012 at 17:57 UTC | |
|
Re: Newbie question - imposing context
by kcott (Archbishop) on May 23, 2012 at 18:06 UTC | |
by erictully (Initiate) on May 23, 2012 at 18:55 UTC | |
by Anonymous Monk on May 24, 2012 at 00:32 UTC |