in reply to Re: Re: Re: difference between \() and []
in thread difference between \() and []
Checking my intuition/understanding here:
@a = (1, 2, 3); # works as expected @a = 1, 2, 3; # Equivalent to (@a = 1), 2, 3;
The assignment operator (equals) binds tighter than the comma operator, so in the second case, the assignment is done, then the rest of the commas are processsed. Thus
$d = ( @a = 1, 2, 3 ); print "$d\n"; print "@a\n";
prints "3" and "1". $d gets the last value in the comma list, which is being evaluated in scalar context because of the scalar assignment.
Thus in the first example, () doesn't create a list, it just changes the precedence so that all the comma operators evaluate together (and in list context because of the context of the assignment, thus returning a list).
Wow. Never really thought about that before. Thanks, chromatic. I'm not entirely sure why parentheses create list context for lvalues, but I guess that's further homework and grepping through perldoc.
-xdg
Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.
|
|---|