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"; }

In reply to Re: Dynamically generating the replacement part of a substitution by Jenda
in thread Dynamically generating the replacement part of a substitution by chaos_cat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.