Mike wrote:

Given the following code snippet:
---------------------------------

print "$text\n";

my $text="sour red apples";
my $pattern="(sour)";
my $replacement="very \$1";

$text=~s/$pattern/$replacement/;

print "$text\n";
---------------------------------

I was expecting "very sour red apples" to be printed, but instead I got "very $1 red apples". I tried changing:

$text=~s/$pattern/$replacement/;

to

$text=~s/$pattern/$replacement/ee;

but that did not work either. How can I make it work, so that it was as if I had written:


Enable warnings with either the -w flag or use warnings


The reason it does not work is this, the /e modifier changes the replacement string to very $1. The second /e modifier will try to evaluate this as a perl code. As you can see this is not a valid perl code. You would have recieved a warning message if you had enabled warnings.

Change $replacement to
$replacement = "\"very \$1\"";

The first /e modifier will change this to "very $1". The second /e modifier will give you the string you want.


$test=~s/$pattern/very $1/; # With the "very $1" being extracted from the #$replacement variable



Thanks









-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to