Hi, I was just refactoring some code and saw a possible opportunity to use this advice. But. Instead of having multiple strings in
@strings to process, I leave all the lines from my file joined together as one giant string with embedded
\n chars. From this angle, since I only have to use each regex once across all the strings via them being 'joined' into one string, I won't benefit from
qr.
my ( $crummy, $good );
foreach my $crummy_good_ar ( @corrections_to_make ) {
( $crummy, $good ) = @$crummy_good_ar;
$file_in_string_form =~ s/\b(\Q$crummy\E)\b/$good/ig;
}
However, as you can see (?) from the example above, I have lots of
crummy/good switchouts to do, and is my plodding approach above the best that can be expected?
P.S. Can you clarify/update what you meant by:
the benefit of qr// objects is lost if there is additional text in the pattern match
I think you are saying that a precompiled/qr regex used in a follow-on regex will have to be recompiled if you snap additional text on to the qr'd variable, because the overall text of the new regex will be different. Although at least one would still have the benefit of 'concentrated regex logic' within the qr'd variable?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.