http://www.perlmonks.org?node_id=687039


in reply to Regular expression "replace string interpolation" problem

/e treats the contents of the replace operand of the s/// operator as Perl source code. Therefore, $replace (not its content) is treated as source code. An expression consisting of a variable name returns its contents, so s/.../$replace/e replaces the match with the contents of $replace.

To execute Perl code in a variable, you need eval EXPR. Change
s/$match/$replace/e
to
s/$match/eval qq{"$replace"}/e

The latter can also be written as
s/$match/qq{"$replace"}/ee
(note the double "e") but I find that to be rather obfuscated. eval EXPR is dangerous so it shouldn't be hidden.

Update: Added missing code to add quotes to the code to execute.