http://qs321.pair.com?node_id=11116788


in reply to Re^3: Evaluating user-entered captured groups during Perl substitution
in thread [SOLVED] Evaluating user-entered captured groups during Perl substitution

Hello Polyglot!

My approach with eval'ing the complete substitution statement was kind of "brute force".

I thought about this every now and then and felt dissatisfied with my solution. Now I may round this off with the following:

#!/usr/bin/perl use Test2::V0; #FROM DATABASE TEXT my $line = "Her house is on 34th Mt. Whitney St. near St. Mt. Helens S +t."; #FROM INCOMING FORM INPUTS my $query = qr'(St\.\s)(Mt\.\s)(?=Helens)'; my $substitution = 'Mount ${1}'; # USER MAY HAVE ENTERED "$1" #FOR RETURNED HTML HIGHLIGHT OF CHANGES my $start = '<span class="highlight">'; my $end = '</span>'; my $replace = $start . $substitution . $end; # twofold quoting and double eval'ing does the trick: $line =~ s{$query}{"qq{$replace}"}gee; is $line, 'Her house is on 34th Mt. Whitney St. near <span class="high +light">Mount St. </span>Helens St.', 'pattern substitution'; done_testing;

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$