Your suggestion to turn on -w is absolutely right, but we should right the way apply it in this particular case.
Just a small correction ;-) For $a--, you said, "undef prints as the empty string", that's not quite right. The fact is Perl didn't print anything, and simply ignored that print $a--, because it attempted to print an uninitialized value.
This would become clear, if you try this:
Step 1:
Put this in a file, say test.pl:
print $a--;
Step 2:
Run it by typing perl -w test.pl, and you will get the warning, so perl never printed it as empty string, instead issued a waring. However when you run it as one-liner, the warning is not printed.
So the bug (inconsistancy) is not that, in the -- case, perl see $a as string; in the ++ case, perl see $a as number.
The bug (inconsistancy) is that, in the -- case, perl straightly realized $a is uninitialized; in the ++ case, perl initialized it to 0. | [reply] [d/l] |
No, that is not correct. Perl still "printed" the undef value as an empty string, it did not abort the print function just because a value was undefined. You could see this with e.g.
print $a--, "foo";
to see that you get both the warning and that "foo" is printed. But since printing an empty string is the same as not printing anything, it doesn't make much sense to argue this too long. :) | [reply] [d/l] |
Okay this is my last post in this thread. ;-)
From a pure logic point of view, answer yourself this questions (if you don't oppose, only whisper the answer to yourself, so we come to an end of this thread ;-), but any way, this is my last post, as I don't believe any more value can be yielded, I expressed whatever I think right, and so did you ;-)
My question:
When your example looks agree with your conclusion that $a is printed as empty string, WHAT MAKES YOU THINK IT DOES NOT AGREE WITH MY CONCLUSION THAT $a IS SIMPLY IGNORED, AND A WARNING IS PRINTED?
| [reply] |
Okay, so why does the first one print 0? | [reply] |
What you are looking at is the difference between post and pre decrement/increment. Note predecrement and preincrement will first do the increment/decrement to the variable and then used. Whereas, postincrement and postdecrement will use the variable first and on the way out increment or decrement. That is:
print $a++; #prints $a and then increments $a (hence prints 0)
print ++$a; #increment $a and THEN print $a (hence prints 1)
The same can be said of the -- unary operator.update:hv++ for answering what was meant (and well, I answered the wrong question) -enlil | [reply] [d/l] |