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

Re^3: Regular expression "replace string interpolation" problem

by pc88mxer (Vicar)
on May 17, 2008 at 06:36 UTC ( #687065=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Regular expression "replace string interpolation" problem
in thread Regular expression "replace string interpolation" problem

To explain what's going on it might be helpful to use underlined bold text to represent strings. So, one can write:

eval 2+24
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 $replaceeval $1 Perl worldsyntax 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 $replaceeval "$1 Perl"
and because of the double quotes this last eval yields $1 concatenated with a space and the string Perl.


Comment on Re^3: Regular expression "replace string interpolation" problem
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-10-20 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls