Basically I want to do this:
#!/usr/bin/perl
$search = qr/(?<X>abc)/;
$_ = "123 abc 456";
s/$search/$+{X} $+{X}/;
print;
something like this:
#!/usr/bin/perl
$search = qr/(?<X>abc)/;
$replace = q($+{X} $+{X});
$_ = "123 abc 456";
s/$search/$replace/;
print;
Result should be 123 abc abc 456
.
Is it possible?
$replace
needs to be maintained as an external var. So, I don't want the contents just moved to another location. I'm reading this info from a file.
Figured it out. I need to do a double evaluate on the expression (Thanks to @Birei for pointing me at the regex evaluate command. Still can't find it in the perl docs though... had to google. :( )
So it becomes:
#!/usr/bin/perl
$search = qr/(?<X>abc)/;
$replace = q(qq($+{X} $+{X}));
$_ = "123 abc 456";
s/$search/$replace/ee;
print;
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