laziness, impatience, and hubris | |
PerlMonks |
The Comma Operatorby dsb (Chaplain) |
on Jul 20, 2001 at 02:24 UTC ( [id://98264]=perlquestion: print w/replies, xml ) | Need Help?? |
dsb has asked for the wisdom of the Perl Monks concerning the following question:
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:
At first I didn't know what was going on there, but then I realized that expression was being evaluated as: 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: Everything is in list context here. $a is assigned 1, $b is assigned 2, $c is assigned 3. 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: 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. 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: 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: 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: 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
Back to
Seekers of Perl Wisdom
|
|