Perl using $1 in a substitution replacement with variable interpolation

I am trying to use variable interpolation in a replacement string including $1, $2,... However, I can't get it to expand $1 into the replacement. I eventually will have the $pattern and $replacement variables be read from a configuration file, but even setting them manually doesn't work.

In the example script, you can see that the $1 (which should be 'DEF') is not expanded in $new_name, but it is in $new_name2 (without variables).

Adding an 'e' flag to the substitution doesn't help.

How do I fix this?

Matt

EXAMPLE CODE:

#!/usr/local/bin/perl
use strict;

my $old_name = 'ABC_DEF_GHI';

my $pattern = 'ABC_(...)_GHI';
my $replacement = 'CBA_${1}_IHG';

# using variables - doesn't work
my $new_name = $old_name;
$new_name =~ s|$pattern|$replacement|;

printf("%s --> %s\n", $old_name, $new_name);


# not using variables - does work
my $new_name2 = $old_name;
$new_name2 =~ s|ABC_(...)_GHI|CBA_${1}_IHG|;

printf("%s --> %s\n", $old_name, $new_name2);

OUTPUT:

ABC_DEF_GHI --> CBA_${1}_IHG
ABC_DEF_GHI --> CBA_DEF_IHG

Answers


You need to do this changes in your code:

my $replacement = '"CBA_$1_IHG"';  #note the single and double quotes
# ...
$new_name =~ s|$pattern|$replacement|ee;  #note the double "ee", double evaluation

See this SO answer for more information


/e treat $replacement as Perl code. The Perl code $replacement simply returns the value it contains.

If you want to evaluate the contents of $replacement as Perl code, you need

s/$search/ my $s = eval $replacement; die $@ if $@; $s /e

which can be written as

s/$search/$replacement/ee

Note that since $replacement is expected to contain Perl code, it means that this can be used to execute arbitrary Perl code.

A better solution is to realise you are writing your own subpar templating system, and use an existing one instead. String::Interpolate understands the templating syntax you are currently using:

use String::Interpolate qw( interpolate );
s/$search/interpolate $replace/e

Need Your Help

Spring form tag and model binding escape

java spring-mvc

I have a Spring form tag in my JSP and the model bound to it. I want some inputs inside the form to be received as single @RequestParam values, but whether I put Spring input or plain HTML input the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.