Modifying a string's value resets pos.
That's not in the doc that I've seen -- "perldoc -f pos" doesn't show it, and it doesn't seem to be anywhere in the ORA Programming Perl book (3rd Ed.). So, how do you know this? Experimentation? Looking at the source? Whatever the answer, I still think there should be a definitive spec as to what progressive matching covers.
I would think that s/// is in the same family as m// simply because matching has to be done "exactly the same way" -- it could even be considered a convenience function. Therefore, I would say that 'progressive matching'for s/// should follow the same rules that m// does in while() loops.
And that brings us back to pos and my question about how you know that pos resets to 0 if the string is modified. If it's *specified* to do that, that's one thing--but if it's just an artifact of implementation (ie., it's not formally documented as a spec), then I'd say it could be changed, and s/// could then become compliant (and consistent) with existing specs.
dan
| [reply] [d/l] |
Common sense. Confirmed by observation.
Actually, I have a module that supports just this type of thing and more that I hope to finish and release to CPAN this year, anyway.
pos doesn't apply to s/// just like it doesn't apply to pack/unpack or anything else. pos only talks about m//g and that is the only thing that it applies to. It doesn't talk about the things that it doesn't apply to and it doesn't apply to s/// nor split. Similar for \G.
The way you run code between steps of s/// is to use s//.../ge. You can't do that with m//g and, similarly, you can't do while( s///g ) { .... }.
| [reply] [d/l] |
pos doesn't apply to s/// just like it doesn't apply to pack/unpack or anything else. pos only talks about m//g and that is the only thing that it applies to.
No question about it. However, the nature of how m// and s/// appears, and how they are documented closely together, makes them appear much more closely related, which would also allow them to share this particular characteristic -- especially in the context of "progressive matching." Remember, the frame of mind that someone is in when they think "progressive" is to "match-operate-match-operate, etc." Since m// lets you do that in a while loop, it seems reasonable to (at least) suspect that maybe s/// would do it too. By contrast, one wouldn't expect to apply to pack/unpack "or anything else" (as you put it) because those functions don't look so similar. Those are conceptually different.
if nothing else, I think it warrants comment in documentation that pos can be reset if used with s///, "or any other operation that may change the string, even if that change occurs later in the string than pos points to."
FWIW, I look forward to learning about your perl module that you described. It does seem to acknowledge the need for what I suspected to be true (or "should have been true." :-)
| [reply] |