eval with parameters

Eric Wilhelm scratchcomputing at gmail.com
Sat May 20 18:41:25 BST 2006

# from Fred L Youhanaie
# on Saturday 20 May 2006 05:16 am:

>Code string may be something like:
>> $codeString = "if ($param1->someMethods($param2)) {return
>> $param1->{someData};} else {return $param1->{someData};}";
>Try ' instead of " above.

Not all strings are untrusted code.  (Think "all users can be fired 
and/or spanked.")  While dynamic, code-executing configuration can 
certainly bite you, it might not.

Peter, it sounds like you want to compile a closure.  Best to do it 
under strict and warnings.  Yes, you had better be able to trust the 
incoming code.  If this is not possible, you'll need to look into 
templating or something completely different.

short answer:
  my $sub = eval("sub {$codeString}");

long answer:

"If you only know how to use a hammer, every problem begins to look like
a nail."
--Richard B. Johnson

More information about the london.pm mailing list