misterperl has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: How can I force a regex to interpolate a substituted block?
by LanX (Saint) on Sep 17, 2024 at 14:28 UTC
      Rolf ++ vote for you as always- I'm on it! I'm starting to mistrust Gemini?
      Rolf /ee works TY!I had to add " inside the 'block' but works great!

      google gemini AI ? She's supposed to be smart but not nearly as smart as you!

      Rolf I realize there are regex resources. But just citing them all doesn't mean any, or all describe "ee", for example:
      Metacharacters Modifiers Overview Details on some modifiers /x and /xx Character set modifiers /l /u /d /a (and /aa) Which character set modifier is in effect? Character set modifier behavior prior to Perl 5.14
      Not knowing that "ee" even exists , and it not being in this doc's modifier list, well, is less than useful? I agree if I studied maybe 12 regex guides I might eventually get to "ee" but that would be like Christmas by then. I tossed my Camel book having read it cover to cover in the 2000s but I don't recall ee in there either.. But it was a long time ago..

      Suggesting a bot that answers every question on every Perl forum with "read the docs"? But some questions are so obscure (like this) that if answered at all, its buried. I've successfully programmed in Perl since 1995 never knowing about or needing "ee" for example..

      Sometimes YOU are a much better resource than a mountain of docs.

Re: How can I force a regex to interpolate a substituted block?
by hippo (Archbishop) on Sep 17, 2024 at 15:08 UTC

    Are you reinventing templates?


    🦛

      Are you reinventing templates?

      no I don't think so? Please explain how one would be simpler than interpolating a scalar with ee?

        Oh, it is by no means simpler than interpolating a scalar with /ee. But it is safer. And it really becomes an advantage when you are performing hundreds of replacements in a single doc. But again, if that's not where this is going to end up, then there's no need. Hence the original question.

        Too often we see folks grab a powerful feature, deploy it all through their code and then it grows and eventually becomes a nightmare from one or more of efficiency, security or maintainability perspectives. Where relevant, it is best to head those off early on.


        🦛