Hello ikegami,

I had to think carefully about this, but of course you’re right. Without parentheses:

13:54 >perl -MData::Dump -wE "my @c = 2, 3; dd \@c;" Useless use of a constant (3) in void context at -e line 1. [2] 16:15 >

the assignment occurs first, because the precedence of the assignment operator is higher than that of the comma operator. So the fact that assignment to an array puts the right-hand side into list context is irrelevent here. With scalar context imposed instead:

16:35 >perl -wE "my $c = 2, 3; say $c;" Useless use of a constant (3) in void context at -e line 1. 2 16:36 >perl -wE "my $c = (2, 3); say $c;" Useless use of a constant (2) in void context at -e line 1. 3 16:36 >

the parentheses act only to change the order of evaluation, because parenthesized expressions (“terms”1) have the highest precedence. So in a standard array assignment:

16:36 >perl -MData::Dump -wE "my @c = (2, 3); dd \@c;" [2, 3] 16:36 >

the assignment to an array puts the RHS into list context; the parentheses create a term; and within that term, the comma operator is “just the list argument separator, and inserts both its arguments into the list.”2 I think that makes sense. :-)

Thanks for the clarification,

1perlop#Terms-and-List-Operators-(Leftward).
2perlop#Comma-Operator.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Perl assign scalar to array by Athanasius
in thread Perl assign scalar to array by bagyi

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.