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

Personally, I wouldn't consider it a bug, but rather a constraint on the use of capturing parens and references to captures in the context of the "g" modifier: the "$1,$2,..." can only be used reliably in the replacement side of s///g, and cannot be counted on as defined outside the scope of that operator.

Replies are listed 'Best First'.
Re^2: Bug or feature? s/// and the g option
by dsheroh (Monsignor) on Oct 14, 2007 at 18:28 UTC
    Agreed. I can't readily think of any time that it would be particularly useful to say "do a bunch of replacements, then tell me what the last thing replaced was" - you'd normally want to either see all the replacements (by using $1 inside a loop on the regex) or none of them (by not using $1 at all).

    While the OP's code brings out an interesting quirk, I think I'd call it undefined behaviour rather than a bug or a feature.

A reply falls below the community's threshold of quality. You may see it by logging in.