Uri Guttman said:
MP> my $x = 3;
MP> print "$y\n" while my $y = $x--;
AQ> But I thought that the OP's code was symantically identical to the
AQ> following:
AQ> use strict;
AQ> my $x = 3;
AQ> while (my $y = $x--) {
AQ> print "$y\n";
AQ> }
AQ> and this is perfectly fine. Why the difference in behaviour then?
how are those the same? the OP's code was a single statement, yours is a
while with a block. in the former, my $y means $y is not visible until
the next statement. in your code $y is visible to the code in the block.
I never said they were the same, just that I thought they were semantically
(sic) identical. I guess I'm wrong, but I don't really understand why. You
say that in the OP's code, the $y is not visible until the next statement,
but that doesn't seem intuitive to me since the "body" of the while loop
(i.e the print statement) will not execute until after the condition
(setting my $y = $x--) has been tested, at which point $y has been defined
as a lexical variable. And since it's defined in the condition part of the
while loop, then it ought to be available in the body, just like my longer
version of the code. To me (and I have never designed a language, so please
excuse my ignorance), both versions of the code ought to behave the same way
(the OP's version has an "invisible block" if you will). But, I get the
feeling that this might break something.
Btw, is this related to the fact that in statements of the form "EXPR
foreach EXPR" you can only use $_ as your loop variable?
Now, I would immediately see why this is wrong:
do {
print "$y\n";
} while my $y = $x--;
since this will evaluate the body before the condition.
--Ala