I created a block of text with $n variable and other variables inside that I want to interpolate on the RHS of a regex. I asked Gemini, and she created the same code I had also tried , which does not work:
my $myVar='old yeller'; my $b = 'cat $1 dog $2 mouse $myVar'; $_ 'hello sir, I am wally, the greeter'; # Use a substitution to replace the matched parts with $b s/(hello).+(wally).+/$b/;
I WANT cat hello dog wally mouse old yeller

I GET 'cat $1 dog $2 mouse $myVar'

the RHS didn't replace $1 , $2 or $myVar. How can I force this? Is there a regex flag? an "eval" somehow?

I told Gemini that our mutual approach failed, and then she suggested:

s/(hello).+(wally).+/sub { $b =~ s/\$\d+/$$1/g; }/;
I have a paragraph with lots of substitutions of both $1 $2 etc as well as other vars like $myVar. I want everything in the block interpolated without specifics- I might even have things in the block like $h{test} or $a(indexed to element 3). (I cant seem to enter brackets here). Her answer is way too specific.

as always, it is greatly appreciated that you took time from your busy day to assist me!


In reply to How can I force a regex to interpolate a substituted block? by misterperl

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.