Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 romping around the Monastery: (3)
As of 2014-09-21 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls