in reply to Dynamically generating the replacement part of a substitution

The attempt number three is still invalid. Try it with my $repl = '$1-$1-'; The attempt four fails if the replacement contains a doublequote.

#my $repl = '$1-$1-'; use strict; use warnings; #my $repl = '$1'; my $repl = '$1"{$1-'; { print "attempt 1 "; my $str = "a,A,c,d"; $str =~ s/([a-z]),/$repl/ig; print "yields: $str\n"; } { no strict 'refs'; print "attempt 2 "; my $str = "a,A,c,d"; $str =~ s/([a-z]),/$$repl/ig; print "yields: $str\n"; } { print "attempt 3 "; my $str = "a,A,c,d"; $str =~ s/([a-z]),/eval "$repl"/ieg; print "yields: $str\n"; } my $code = 'sub {$_[0] =~ s/([a-z]),/'. $repl . '/ig}'; my $re = eval ($code); if ($@) { print "attempt 4 failed: $@" } else { print "attempt 4 "; my $str = "a,A,c,d"; $re->($str); print "yields: $str\n"; } { print "attempt 5 "; (my $repl = $repl) =~ s/"/\\"/g; my $str = "a,A,c,d"; $str =~ s/([a-z]),/qq{"$repl"}/igee; print "yields: $str\n"; }