Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Empty pattern in regex [updated]

by jo37 (Deacon)
on Oct 28, 2023 at 10:40 UTC ( [id://11155240] : note . print w/replies, xml ) Need Help??


in reply to Re^2: Empty pattern in regex [updated]
in thread Empty pattern in regex

Hello perlboy_emeritus,

to be more explicit in this issue, I do not only think it's a bug, I am absolutely convinced it is. Some remarks:

  • Having a workaround for a bug does in no way mean it is not a bug.
  • Using $& in this scenario is dangerous, as it is affected by the very same bug. See extended example below.
  • I cannot find anything in your code that would trigger the bug. This is fine and TIMTOWTDI
  • perlop is very precise in The empty pattern "//:
    If the *PATTERN* evaluates to the empty string, the last *successfully* matched regular expression is used instead. (...) If no match has previously succeeded, this will (silently) act instead as a genuine empty pattern (which will always match). (...)
    As you can see from my example, // does not behave as described if there was a successful match and there happens a jump out of an inner block where // was applied. This clears $& and resets // to the genuine 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/; say "\$&: '$&'" if defined $&; { goto $label unless //; say "// matched"; inner: } outer: } say ''; } __DATA__ goto inner loop: c // matched loop: d /d/ matched $&: 'd' // matched loop: e $&: 'd' loop: f $&: 'd' loop: g $&: 'd' goto outer loop: c // matched loop: d /d/ matched $&: 'd' // matched loop: e $&: 'd' loop: f // matched loop: g // matched

Greetings,
-jo

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