Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
No such thing as a small change
 
PerlMonks  

Re: Evaluate in a substitute

by nashdj (Friar)
on Oct 17, 2003 at 11:12 UTC ( #299972=note: print w/ replies, xml ) Need Help??


in reply to Evaluate in a substitute

IO::String is useful for this kind of task.

use IO::String; ... $data =~ s/<# (\{.*?\}) #>/evalCap($1)/eg; sub evalCap { my $code = shift; my $str; my $str_fh = IO::String->new($str); my $old_fh = select $str_fh; eval $code; select $old_fh; $str; }
Update:
The regular expression can be simplified: s/<# {(.*?)} #>/....
As Abigail implies, the braces aren't needed in the eval. Nor in this case do they need to be escaped.

- nashdj


Comment on Re: Evaluate in a substitute
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-04-20 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls