I think it's a bug. It has nothing to do with the flip-flop operator and it seems to be caused by jumping out of a block. Consider this example that emulates a flip flop and uses goto instead of next.

#!/usr/bin/perl use v5.24; use warnings; my ($first, $last); while (<DATA>) { chomp; $first ||= /d/; undef($first) if $last ||= /h/; if ($first || $last) { undef $last; #goto ewhile unless //; goto eif unless //; say; eif: } ewhile: } __DATA__ c d e f g h i
goto eif: d h
goto ewhile: d f g h

Jumping to the end of the current block produces the expected result, while jumping to the end of the while loop reproduces choroba's strange results. The jump out of the block seems to clear the "last successful match" causing // to be taken as an always matching empty pattern.

However, I'd prefer to check the flip-flop's return value as this works in all circumstances, even for if(foo($_) .. bar($_)) {...}.

Update: 26.10.2023

Here is a much simpler example demonstrating the behaviour without any flip-flop behaviour. A jump out of a block transforms the empty pattern // from the last successful matching pattern to a true empty pattern.

#!/usr/bin/perl use v5.24; use warnings; for my $label ('inner', 'outer') { say "goto $label"; for ('c' .. 'g') { say "loop: $_"; say "/d/ matched" if /d/; { goto $label unless //; say "// matched"; inner: } outer: } say ''; } __DATA__ goto inner loop: c // matched loop: d /d/ matched // matched loop: e loop: f loop: g goto outer loop: c // matched loop: d /d/ matched // matched loop: e loop: f // matched loop: g // matched

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

In reply to Re: Empty pattern in regex [updated] by jo37
in thread Empty pattern in regex by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.