Syntactic Confectionery Delight | |
PerlMonks |
No \G for s///g ?by tye (Sage) |
on Mar 21, 2003 at 20:45 UTC ( [id://245000]=perlquestion: print w/replies, xml ) | Need Help?? |
tye has asked for the wisdom of the Perl Monks concerning the following question: In Re: Wrap while ignoring certain sequences (from CB) I ended up with this code: But it has two bugs (described later). What I think I should use is: You see, the first substitution should only take place immediately after a newline. The ^ (and m option) take care of starting right after a newline that was already in the string. The \G should take care of starting right after a newline that was just inserted by a substitution (wait for it). Note that you can't use ^ nor look-behind assertions to detect stuff that was inserted after the s///g started. This isn't documented (that I've seen), but I've tested it (and it makes sense). \G should say "start where I left off last time" -- except double checking the documentation, it appears that this is only supported for m//g, not s///g. Is there a good reason for this that I'm missing? Update: Appears to be just a documentation issue (perhaps even an issue just with my reading of docs). See my reply below. So the first substitution in the first block of code above could be better because it wastes time trying to split each short line starting at each whitespace in the line. The (?<=\s) works by matching the space that we just turned into a newline. The second substitution in the first block of code is broken because a single word that spans more than two lines will only be wrapped once. I want to allow substitutions that start either right after I've wrapped a long word, or at the start of the next word. But without \G, I don't see any way to allow starting right after a substitution that doesn't also allow starting anywhere (which creates another bug as discussed on my previous node). Anyone see a way I can work around this missing feature? Should I report this as a "bug"? BTW, playing with this code is made easier by setting $len much lower and making $esc a regular character. - tye
Back to
Seekers of Perl Wisdom
|
|