Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 examining the Monastery: (12)
As of 2015-07-07 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (91 votes), past polls