Yes, another issue with a strange and underestimated operator. Here I thought commas were good for only seperating list elements. Well, I guess in a sense I was right...but I didn't realise how much of an effect the low precedence of the comma operator would have on the outcome of a program. I tried a bunch of different things with assignment operators and print statements to see if I could get the gist of the way the comma operator will behave in different situations.
This all started when I first started using the comma operator in print statements like this:
$foo = "bar"; print $foo, "\n";
At first I didn't know what was going on there, but then I realized that expression was being evaluated as:
print($foo,"\n");
That's when I decided to play around a bit to see just what kind of chaos I could cause with the comma operator and different combos of parentheses.

First up was this:

($a,$b,$c) = (1,2,3); # started off easy # does the expected
Everything is in list context here. $a is assigned 1, $b is assigned 2, $c is assigned 3.
$a,$b,$c = (1,2,3); # left side acts weird
The left side of the = uses the last valid option, $c(as if being evaluated in scalar context) and then the right side is evaluated in scalar context returning the last valid option, 3. So $c is assigned 3, $a,$b are both undefined.

The next one was a puzzler:

$a,$b,$c = 1,2,3;
The right side acts the same as in the last example. The left side though, acts kinda strange. With out the parentheses, the left side is no longer a list, so only 1 is evaluated, so $c is assigned 1. I would've though the left side would still evaluated as a list in scalar context returning 3. I'm not really sure why it didn't.

Things got kinda funky when I started messing with print statements.

print(1),print(2),print("\n"); # output # 12 - followed by newlin
I thought that was strange since in that statement the comma is acting almost as a line terminator. That one acts as if it was coded:
print 1; print 2; print "\n";
perlfunc pretty much says "If it looks like a function, it is a function". So, when print is called with its argument in parentheses, it is treated like a function call. Fine. What I thought was weird was that this pseudo-list was entirely executed. That is, I didn't think the print(2) and print("\n") would execute. Remembering how fishy things got last time I removed parentheses, I tried this:
print 1, print 2, print "\n"; # output [blankline] 2111
Interesting. Best as I can figure, that one evaluated to print(1, (print 2,(print "\n")));. So it executes from inside to outside(sort of like simlyfying a math expression). The newline is printed first, then second-innermost print is execute with its 2 arguments,2, and the return value of print "\n" which is 1(or true. Those two values are printed. Then the print outside all parentheses is called with 1 as its first argument and the return value of print 2,(print "\n")(which evaluates to 1) as its second.

The funny part is, I understand why that one worked out the way it did. I don't understand why:

print(1),print(2),print("\n");
executes like 3 seperate statements. I'm guessing the comma saves the program from a syntax error, but why does it allow the other list items to execute.

Anyway, that's it. I don't know why that damn thing acts like it does sometimes(specifically the examples I gave), so if anyone can shed any light on things that would be good.

Thanks.

Amel - f.k.a. - kel


In reply to The Comma Operator by dsb

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.