dsb has asked for the wisdom of the Perl Monks concerning the following question:
At first I didn't know what was going on there, but then I realized that expression was being evaluated as:$foo = "bar"; 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.print($foo,"\n");
First up was this:
Everything is in list context here. $a is assigned 1, $b is assigned 2, $c is assigned 3.($a,$b,$c) = (1,2,3); # started off easy # does the expected
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.$a,$b,$c = (1,2,3); # left side acts weird
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.$a,$b,$c = 1,2,3;
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:print(1),print(2),print("\n"); # output # 12 - followed by newlin
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";
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.print 1, print 2, print "\n"; # output [blankline] 2111
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.print(1),print(2),print("\n");
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: The Comma Operator
by japhy (Canon) on Jul 20, 2001 at 03:21 UTC | |
Re: The Comma Operator
by lestrrat (Deacon) on Jul 20, 2001 at 03:05 UTC | |
Re: The Comma Operator
by petral (Curate) on Jul 20, 2001 at 11:48 UTC | |
Re: The Comma Operator
by da (Friar) on Jul 20, 2001 at 19:24 UTC | |
Re: The Comma Operator
by PetaMem (Priest) on Jul 20, 2001 at 19:00 UTC | |
Re: The Comma Operator
by kael (Monk) on Jul 21, 2001 at 10:10 UTC |