To explain what's going on it might be helpful to use underlined bold
text to represent strings. So, one can write:
eval 2+2 → 4
and this means if you eval
the string 2+2
, you'll get the string 4
. In perl this is just:
my $x = '2+2';
my $y = eval $x; # $y is the string '4'
In your original example, what is happening during the substitution s/$match/$replace/e is:
eval $replace → $1 Perl
I.e., the match is replaced with the string $1 Perl
, and that is why $text2
contains $1 Perl world
How about just adding another /e modifier to evaluate the substitution again? Unfortunately this doesn't work because $1 Perl world is not valid perl syntax:
eval eval $replace →
eval $1 Perl world → syntax error
When $replace and the substitution is written as:
my $replace = '"$1 Perl"';
$text2 =~ s/$match/$replace/ee;
the evaluation of the replacement proceeds as follows:
eval eval $replace → eval "$1 Perl"
and because of the double quotes this last eval
concatenated with a space and the string Perl
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||