Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Bug or feature? s/// and the g option

by dsheroh (Monsignor)
on Oct 14, 2007 at 15:39 UTC ( #644762=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://644762]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2023-05-31 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?