Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Regular expression "replace string interpolation" problem

by ikegami (Pope)
on May 16, 2008 at 22:27 UTC ( #687039=note: print w/ replies, xml ) Need Help??

in reply to Regular expression "replace string interpolation" problem

/e treats the contents of the replace operand of the s/// operator as Perl source code. Therefore, $replace (not its content) is treated as source code. An expression consisting of a variable name returns its contents, so s/.../$replace/e replaces the match with the contents of $replace.

To execute Perl code in a variable, you need eval EXPR. Change
s/$match/eval qq{"$replace"}/e

The latter can also be written as
(note the double "e") but I find that to be rather obfuscated. eval EXPR is dangerous so it shouldn't be hidden.

Update: Added missing code to add quotes to the code to execute.

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2015-11-30 03:25 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls