in reply to Bug or feature? s/// and the g option

I'd guess that it's undefined because, in the final example, the last partial match (B...) fails and apparently clears $1 before doing so. Reversing the order of those lines works without returning the warning:
$test = <<'END'; ABFG ABCD END
Edit: I poked at it a little more and seem to have confirmed my theory. If the final B in the data string is not followed by C?[DE], $1 ends up undef, regardless of where that B is.

Replies are listed 'Best First'.
Re^2: Bug or feature? s/// and the g option
by Belgarion (Chaplain) on Oct 14, 2007 at 16:17 UTC
    Interesting. So a failed match will clear the $1 variable. I'm still a little stumped by Marco's original example:
    $test = <<'END'; XXWY XXWZ END $count = $test =~ s#(XXW?Y)##gi; print "REMOVED: <<$1>>\nCOUNT: $count\n";
    That fails. But remove the "i" in the substitution and $1 is defined. Why would case-insensitive make any difference?
    --rjk
      AIUI, without the i, before the regex engine proper is entered, there's an optimization that makes it search for an XX followed later by a Y. That optimization rejects a match, and when that happens, it bypasses the bug that's setting $1 to undef. Seems to be fixed for 5.10.0.
        Ah, thanks for the explanation ysth!
        --rjk