On Tue, 23 Aug 2005 14:02:03 +0100, Jurgens du Toit wrote:
> I want to use groupings, (TEST), and replacement
> variables, $1, $2, etc, in a replacement regex, but
> the replacement is built into a variable. Like so:
>
> #!/usr/bin/perl
>
> my $replace = "urgent \$1";
> my $string = "This is a TEST";
> $string = s/(TEST)/$replace/gi;
> print $string;
>
> It outputs
> This is a urgent $1
The right usage of the /e (evaluate) modifier will do this (which is
depressingly nonobvious):
$string = s/(TEST)/qq("$replace")/giee;
The first /e turns the replacement into qq(urgent TEST), the second
one evaluates that expression and interpolates it.
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>