Next | Higher-Order Parsing | 7 |
my $function = eval $code; my $y = $function->($x);
I don't need to explain all the things that can go wrong here, do I?
Even if it could be made safe, it has some problems:
(x^2 + 3*x)* sin(x * 2) + 14
In Perl, ^ means bitwise exclusive or
Not exponentiation
Alternative: implement an evaluator for expressions
Input: string
Output:
Compiled code
Or abstract syntax tree
Or specialized data structure
Or Expression object
...
Next | Copyright © 2007 M. J. Dominus |