in reply to Re^8: Progressive matching w/substitutions (pos)
in thread Progressive matching w/substitutions

Indeed, it took experience to realize that \G doesn't work within split nor with s///. And, a quick test shows that \G and even pos (now) works within s///g:

> echo xxxfooxxx | perl -pe's/\Gx/pos($_)/ge' 012fooxxx

I can understand there being a reluctance to update documentation to proclaim "pos doesn't work with s///", for example (the potential authors of such a documentation patch share your reservations about whether this is an accident of implementation, an intentional design that might still change, or something that is that way "for a very good reason"). But I agree that some improved clarity is called for with somewhat vague additions similar to "Currently \G is not supported with split and using it there can produce surprising results."

With experience, I also learned to appreciate the drawbacks of implied, global state as provided by pos and each. Just last week I "lost" several hours at work to a pair of bugs, one that left each unreset and one that used each without reseting it first.

Part of the point of my module is that it requires you to declare where you want to start using this state and doesn't store the state globally. Thus it removes some of the common gotchas of pos and scalar m//g.

So I certainly would be reluctant to change s/// to be sensitive to pos. It would be less troubling if s/// only obeyed pos when \G is used in the regex, but that isn't ideal. Maybe s///p?

- tye