in reply to Re: 'and next' question
in thread 'and next' question

"You are post-incrementing your variable. It isn't 'true' until after the and is evaluated."

I am a little bit concerned that this statement could be easily misunderstood (I strongly believe that you mean the right thing, and I don't intend to second guess, but I want make this a little bit more clear and precise. I know that I am a little bit picky here ;-).

If you try this code:

my $a = 1; $a++ and print $a;

You will get 2, which shows that right after "$a++" is evaluated, and before the entire second line finished, ++ already happened.

From a pure language view, that "It" in your sentence could stand for the phrase "your variable", which appeared earlier. In the case of the original post, that variable is already true before that "and" is evaluated, but it is false before the "++" happened. However "++" happens before "and".

Replies are listed 'Best First'.
Re: Re: Re: 'and next' question
by sauoq (Abbot) on Oct 27, 2003 at 17:03 UTC
    my $a = 1; $a++ and print $a;
    You will get 2, which shows that right after "$a++" is evaluated, and before the entire second line finished, ++ already happened.

    Yes, but in your example no short-circuiting occurs. In the original post, the evaluation of the left operand terminates the evaluation of the and so the variable's new value isn't available until "after the and is evaluated".

    I guess I could have been more precise. I might have said something like, "the left operand evaluates to undef before being incremented and, since undef is false, the right operand is never evaluated because and is a short-circuit operator." I don't know if that would really have been easier to understand though, even if it is slightly more correct.

    At least, instead of "after the and is evaluated" I probably should have said "after the and is short-circuited" or something.

    Still, the only reason the and was used in the first place was for its short-circuiting properties. Its return value was ignored. In fact, with your example, if I were being pedantic I wouldn't talk about the and at all...

    perl -MO=Deparse -e '$a++ and print $a' print $a if $a++; -e syntax OK
    See? :-)

    -sauoq
    "My two cents aren't worth a dime.";