in reply to Dynamically generating the replacement part of a substitution

Looks like all of your attempts are broken. I'd expect the resulting string to be aA,cd, and not to lose all its commas.

I'd go for a variant of attempt 3:

my $repl = '$1'; my $str = "a,A,c,d"; s/([a-z]),/$repl/eeg; # Note the double 'e' modifier say $str; __END__ aA,cd
No need to turn off strictness.

Now, if you don't want string evals in the loop, I presume you want that because of performance. But you replace that with a subroutine call - I'd have to see a benchmark to be certain that is faster.

But what you could do is (string) eval your entire loop:

my $repl = '$1'; eval <<"EOT"; while (1) { \$str =~ s/([a-z]),/$repl/g; } EOT