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        


In reply to Re^9: Progressive matching w/substitutions (implied) by tye
in thread Progressive matching w/substitutions by argv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.