Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 wandering the Monastery: (7)
As of 2016-02-14 18:04 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (470 votes), past polls