in reply to How to apply modifiers to a substitution via variable?

For the second issue generally /e (evaluate) is used, but you need it twice. Remember to quote the $ in the string for $replace, or use single quotes so Perl doesn't try to interpolate $1:

my $replace = '$1'; $string =~ s/a+(b+)a+/$replace/eeg;

DWIM is Perl's answer to Gödel