in reply to Using a regex capture as part of a varible

Never used it but looking at the source I'm not sure that it'll do what you want. Your second argument is getting passed in as $replace into this bit of code:

else { @content = split( /\n/, cat($file) ); for (@content) { s/$search/$replace/; } }

You've tried to quote things correctly, but for that to work there'd need to be an /ee modifier on the substitution to get it evaluate the contents of $replace and then replace with what that expanded text contains. Unless there's some new dark magic I'm not aware of for expansions that lets you turn on /ee at a distance I don't think it's capable of what you're trying to do.

Edit: Aah you've got the multiline option set; the substitution would be the similar s///mgs a few lines earlier but the problem remains the same (it'd need to be s///mgsee to expand $replace and then evaluate the expanded value).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Using a regex capture as part of a varible
by nysus (Parson) on May 07, 2020 at 14:49 UTC

    OK, good to know. My brain seems to have lost all knowledge of the 'e' modifier. Tried to get it to work with this simple example:

    my $new = 'blah'; my $string = 'asdfjkjl'; my $find = '(asd)'; my $replace = '$1' . $new; replace ($string, $find, $replace); sub replace { my $string = shift; my $left = shift; my $replace = shift; $string =~ s/$left/$replace/e; print $string; }

    Still prints $1 in the output though. Adding 'ee' throws an error: "Bareword found where operator expected at (eval 1) line 1, near "$1blah"

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      You need to set up your replacement string correctly. Once that's done it runs fine with the /ee modifier:

      use strict; use warnings; use Test::More tests => 1; my $new = 'blah'; my $string = 'asdfjkjl'; my $find = '(asd)'; my $replace = qq#\${1} . '$new'#; my $result = replace ($string, $find, $replace); is $result, 'asdblahfjkjl'; sub replace { my $string = shift; my $left = shift; my $replace = shift; $string =~ s/$left/$replace/ee; return $string; }