So I ask the user for a regexp and a substitution string as parameters
perl -s -e "$data='abcdef'; $data=~s#$r#$s#; print $data" -- -r=(abc) -s=$1hello
produces
$1hellodef
I'd rather it produce
abchellodef
Adding an eval
perl -s -e "$data='abcdef'; eval qq{\$data=~s#$r#$s#}; print $data" -- -r=(abc) -s=$1hello
does the trick, but uses eval. Seems like it would be nice to have an option to "reinterpolate" a string. Am I missing another way to do this? I suppose, with block interpolation "${\func()}" that interpolation isn't any safer than eval, but it would be easier to use... especially if it were a substitution operation modifier s///R like e, possibly usable more than once s///RRR And yes, I could force the user to write it as code, and use s///e, but that is cumbersome for the user:
perl -s -e "$data='abcdef'; $data=~s#$r#$s#ee; print $data" -- -r=(abc) -s=$1.'hello'
Might be nice to have qR{$foo} that would do the interpolation of qq{$foo} and then reinterpolate. And maybe qRRR{$foo} for 3 reinterpolations...

The solution

Anonymous Monk's suggestion of String::Interpolate seems to suffice:

perl -s -MString::Interpolate -e "$data='abcdef'; $data=~s#$r#$s#; $data = String::Interpolate->interpolate( $data ); print $data" -- -r=(abc) -s=$1hello

In reply to reinterpolation of regexp (and strings?) by AlwaysLearning

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.