Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Bug in script, regex help req extreme urgentby jaredor (Priest) |
on Mar 09, 2013 at 16:22 UTC ( [id://1022589]=note: print w/replies, xml ) | Need Help?? |
I like the test harness of 2teez, it is easily adapted for a perl -p command line. So I'll reuse it for an old sed-der comment/observation: For mass transformations of text, trade off flexible regex for static strings when possible.
Of course, this means that whatever whitespace separation you used to have between the keyword "module" and the module name is gone, but whitespace niceties usually aren't a big deal with mechanically gunkulated code. On the other hand, you don't have to ignore any lines that have commenting on them. This is a line oriented solution, so slurping in a file and transforming that long string will require using the "g" flag and listening to sages such as choroba. For line-oriented text, I believe line oriented processing gives the fewest surprises (e.g., a surprise such as having a comment change affect code manipulation). However if the two adjacent words you are keying off of can be separated by a newline, then you have to use something like the fancier regex solutions suggested.
One thing left to worry about is whether "module OLD" in a comment should be transformed to "module NEW". But if you keep in mind the old maxim, "All comments lie," then you shouldn't worry too much ;-)
In Section
Seekers of Perl Wisdom
|
|