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
In reply to Re: Empty pattern in regex [updated]
by jo37
in thread Empty pattern in regex
by choroba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |