I’m making a regexp constructor.
But when running:
my $text = 'a a a';
my $replace = '$1/$2-$3';
$text =~ s/(\w) (\w+) (\w+)/$replace/gmi;
$text here = ‘$1/$2-$3’;
So $1,$2,$3 are not changed but placed as they are in $replace. How would i make it use $replace content as manually printed replate pattern?
$replaceis just a string. If you want it to be evaluated as code, you need the/emodifier in your substitution. But you also need to prepare your string for the evaluation to interpolate your variables:We use double evaluation to first turn the variable into a string, then to evaluate that string.
However, whenever you find yourself relying on eval, you’re probably doing something unnecessary. Eval can be rather evil, as OmnipotentEntity rightly points out, so be very careful about using it.