glwtta has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: manipulating capture groups in s///
by diotalevi (Canon) on Nov 11, 2003 at 19:19 UTC | |
by Aristotle (Chancellor) on Nov 11, 2003 at 19:57 UTC | |
|
Re: manipulating capture groups in s///
by Anonymous Monk on Nov 11, 2003 at 19:26 UTC | |
|
Re: manipulating capture groups in s///
by ptkdb (Monk) on Nov 11, 2003 at 20:17 UTC |