I have some code:
$a = 'abcdef';
$b = 'f\1d';
$a =~ s/d(e)f/$b/g;
print $a;
I get abcf\1d, but how can I get abcfed?
$a = 'abcdef';
$b = '"f$1d"';
$a =~ s/d(e)f/$b/gee;
print $a;
Notice that there are two e modifiers in /gee; the second one evaluates "f$1d" string to fed.
As a side note you don't need /g as you're replacing only one pattern occurrence.
$a and $b are special variables, used for sort. So you shoulnd't use them at all.
Better solution is to use another variable and then:
$var_a = 'abcdef';
$var_b = '"f${1}d"';
$var_a =~ s/d(e)f/$var_b/gee;
print $var_a;
The trick is to say, that the substitution uses the \1 first found var ( use the /e flag to avoid using \1 :P )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With