in reply to Re^3: Regexp substitution using variables
in thread Regexp substitution using variables

Though ... there is one "lighter" version to build your replacement dynamically.

eval the replacement-string into a sub, and apply just one /e at the s///

DB<137> $rep = '<$1>' DB<138> eval qq( sub rep { "$rep" } ) DB<139> p "abc" =~ s/(.)/rep()/rge <a><b><c> DB<140>

This will give you more control about what is happening, since you can use B::Deparse to check the replacement string before executing it.

Like this you have at least a chance to reject dubious code.

DB<140> p B::Deparse->new('-q')->coderef2text(\&rep) { use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say +', 'state', 'switch', 'unicode_strings', 'unico\ de_eval'; '<' . $1 . '>'; } DB<141>

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery