I'm manually interpolating values from a hash ref of internal "environment
variables" into a user-supplied string.
The string's magic interpolation
character is '%', not '$', so "%FOO" in the string should be replaced with
the value of $env->{'FOO'}, and "%{BAR}' should be replaced with
$env->{'BAR'}, etc. Plus, the interpolation is recursive; if
the interpolated value itself contains a '%', it gets re-expanded.
Lastly, '%%' eventually gets replaced with a single '%', although this
happens later, so I actually need to pass through %-pairs
unchanged.
Hope I explained that clearly...
Anyway, the following code snippet does all of this the way I want:
# % expansion. %% gets converted to % later, so expand any
# %keyword construction that doesn't have a % in front of it
# (modulo multiple %% pairs in between).
while (($str =~ s/(^|[^\%](?:\%\%)*)\%([_a-zA-Z]\w*)/"$1".$env->{$2}/g
+e) ||
($str =~ s/(^|[^\%](?:\%\%)*)\%\{([_a-zA-Z]\w*)\}/"$1".$env->{$
+2}/ge)) {}
But I'm doing this with two very similar expressions,
the first for normal
unadorned variable names ("%FOO") and the second for variable
names enclosed in braces ("%{FOO}"). Can anyone suggest how
these two might be combined, hopefully in a way that
speeds up the processing? The obvious technique of sticking
'*' after the braces in the regexes loses because I don't want to match
unbalanced braces like "%FOO}".
I've been leafing through "Mastering
Regular Expressions"
in search of an answer,
especially the section on lookahead,
but that's right about where
my brain starts filling up...
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.