in reply to Re^2: When doesn't the flip-flop operator work in all scalar contexts?
in thread Why doesn't the flip-flop operator work in all scalar contexts?

You just demonstrated the difference between a 'constant value' and a 'constant expression'.

UPDATE: Arrrgh, again I fell into the trap of not reading thoroughly. Please ignore.

UPUPDATE: After testing more thoroughly, it seems that when something like $x is used instead of 1, something happens that is not consistent with the docs:

for(1,2,3,4,5) { $.= $_; next if 1..1; print "$_\n"; } #### prints as expected 2 3 4 5 my $x=1; for(1,2,3,4,5) { $.= $_; next if $x..$x; # never returns true! print "$_\n"; } #### prints nothing. Why??? It doesn't test against $. and it doesn't +just test the value $x my $x = 1; my $y = 0; for(1,2,3,4,5) { $.= $_; next if $x..$y; # never returns true! print "$_ "; } #### prints nothing as well. If the flip and the flop happened at the +same round it would have printed something

Tried to run it with -Dx but that showed too much information. Wasn't there a debugging flag that shows just the compiled byte code?

UPUPUPDATE: corrected a copying mistake and explained the wrong output some more
  • Comment on Re^3: When doesn't the flip-flop operator work in all scalar contexts?
  • Download Code

Replies are listed 'Best First'.
Re^4: When doesn't the flip-flop operator work in all scalar contexts?
by ikegami (Patriarch) on Oct 02, 2008 at 19:55 UTC

    #### prints nothing. Why??? It doesn't test against $. and it doesn't just test the value $x

    $x is not a constant expression, so $. doesn't come into play. You're right about that bit.

    But you're wrong about it not just testing $x. The reason it never prints anything is that it flips again as soon as it flops since $x is always true.

    my $x=1; for(1,2,3,4,5) { next if (printf("flip at %s? %s\n",$_,$x?'yes':'no'),$x) .. (printf("flop at %s? %s\n",$_,$x?'yes':'no'),$x); print "$_\n"; }
    flip at 1? yes flop at 1? yes flip at 2? yes flop at 2? yes flip at 3? yes flop at 3? yes flip at 4? yes flop at 4? yes flip at 5? yes flop at 5? yes

      First of all that doesn't fit the man page:

      It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once.

      Secondly if you look at my third example above, even if the second operand is always false, nothing is printed. So it can't be a case of flip and flop on the same round, there never seems to be a flip

        First of all that doesn't fit the man page

        How does it not fit that quoted statement?
        flip at 1? yes flop at 1? yes -> Would return false, but must return true once. flip at 2? yes flop at 2? yes -> Would return false, but must return true once. flip at 2? yes flop at 2? yes -> Would return false, but must return true once. flip at 2? yes flop at 2? yes -> Would return false, but must return true once. flip at 2? yes flop at 2? yes -> Would return false, but must return true once.

        Secondly if you look at my third example above, [...] So it can't be a case of flip and flop on the same round, there never seems to be a flip

        It flips, but it never flops because $y is always false.

        my $x=1; my $y=0; for(1,2,3,4,5) { next if (printf("flip at %s? %s\n",$_,$x?'yes':'no'),$x) .. (printf("flop at %s? %s\n",$_,$y?'yes':'no'),$y); print "$_\n"; }
        flip at 1? yes flop at 1? no -> Returns true. flop at 2? no -> Returns true. flop at 3? no -> Returns true. flop at 4? no -> Returns true. flop at 5? no -> Returns true.

        Update: Truth tables for flip/flop:

        ..
        StateFlipFlopNew StateReturns
        outyesyesouttrue
        outyesnointrue
        outno[not executed]outfalse
        in[not executed]yesoutfalse
        in[not executed]nointrue

        ...
        StateFlipFlopNew StateReturns
        outyes[not executed]intrue
        outno[not executed]outfalse
        in[not executed]yesoutfalse
        in[not executed]nointrue