The OPed sort of problem is tricky, but for this
specific iteration:
Win8 Strawberry 5.8.9.5 (32) Wed 11/25/2020 22:12:13
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $pattern = '(testing)';
my $replacement = 'New \U$1';
my $flags = 'i';
my $value = 'My Testing Text';
### $replacement =~ s/\\/\\\\/g;
print "replacement '$replacement' \n";
eval "\$value =~ s/$pattern/$replacement/$flags";
print "$value\n";
^Z
replacement 'New \U$1'
My New TESTING Text
(An escaped backreference \1 is not kosher in a replacement
string anyway; it should be in $1 form.)
Update: Here's a version of the example code that better
illustrates the process of building the evaluation string:
Win8 Strawberry 5.8.9.5 (32) Wed 11/25/2020 22:45:44
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $pattern = '(testing)';
my $replacement = 'New \U$1';
my $flags = 'i';
my $value = 'My Testing Text';
print "replacement '$replacement' \n";
my $eval_string = "\$value =~ s/$pattern/$replacement/$flags";
print "eval_string '$eval_string' \n";
eval $eval_string;
print "$value\n";
^Z
replacement 'New \U$1'
eval_string '$value =~ s/(testing)/New \U$1/i'
My New TESTING Text
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|