Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Using a regex capture as part of a varible

by nysus (Parson)
on May 07, 2020 at 14:49 UTC ( #11116546=note: print w/replies, xml ) Need Help??


in reply to Re: Using a regex capture as part of a varible
in thread Using a regex capture as part of a varible

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

Replies are listed 'Best First'.
Re^3: Using a regex capture as part of a variable
by hippo (Bishop) on May 07, 2020 at 15:05 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2022-05-18 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?