Is it possible to make a "search and replace" work where the replacement is a variable containing $1
, $2
, ... special variables?
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
my $row = 'ab';
my $pattern = '^(.)(.)';
my $replacement = '$2-$1';
$row =~ s/$pattern/$replacement/;
say $row; # $2-$1
Use String::Substitution's
sub_modify
.Notes:
gsub
instead ofsub
if you want the behaviour of theg
modifier.copy
instead ofmodify
if you want the behaviour of ther
modifier.