in reply to 'and next' question

Consider these examples:

$ perl -le 'print $h++' 0 $ perl -le '0 and print "this will not be printed"' $
You are post-incrementing your variable. It isn't 'true' until after the and is evaluated. Since the lefthand side of the and is false, the righthand side is never evaluated. This is called "short-circuiting" and it is often very useful.

As you discovered, you can use a comma to get what you want; you could also change your post-increment to a pre-increment. But I don't think either of those options is the best way to do it. There's really no value in being overly idiomatic. The straight-forward approach is probably best:

while ( <DATA>) { chomp; if ($_ > 5) { $hash{ $_ }++; next; } print; }

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

Replies are listed 'Best First'.
Re: Re: 'and next' question
by pg (Canon) on Oct 27, 2003 at 01:03 UTC
    "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".

      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.";