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.


In reply to Re: Re: Re: Re: difference between \() and [] by xdg
in thread difference between \() and [] by eXile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.