Think about Loose Coupling | |
PerlMonks |
manipulating capture groups in s///by glwtta (Hermit) |
on Nov 11, 2003 at 19:10 UTC ( [id://306323]=perlquestion: print w/replies, xml ) | Need Help?? |
glwtta has asked for the wisdom of the Perl Monks concerning the following question:
I am sure this has been asked before, but I couldn't find the answer on my own. Very simply, I want to do a substitution where the string being substituted in, is based on what was matched; in other (clearer) words, it would look like: s/(bar)/&foo($1)/g;. The best I've been able to come up with is: s/((bar)(?{$b=foo($2)}))/$b/g; I am a little wary of this, as I am not entirely certain that I understand exactly how it's doing what it's doing (additionaly the docs seem to suggest that (?{}) is liable to open the gates of hell and unleash all sorts of despair on humanity). Additionally, as I understand it, this requires a global variable (the use of $b is quite intentional here). So my question is: (a) am I going to run into trouble with the above? and (b) is there a better way?
Back to
Seekers of Perl Wisdom
|
|