Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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/$replace/e
to
s/$match/eval qq{"$replace"}/e

The latter can also be written as
s/$match/qq{"$replace"}/ee
(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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-10-23 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls