in reply to nested variable resolution
There is a vast difference between eval with a string and eval with a block. String-eval will evaluate the string, while block eval will execute the block and catch any exceptions raised in there. What you (seem to) want is the string form of eval:
use strict; my $x = 3; my $y = "2 * $x"; # interpolation here sets y to '2 * 3' already! my $z = eval $y; # $z is 6 now print "\$x is $x"; print "\$y is $y"; print "\$z is $z";
More likely though, in the long run, you will want to ditch eval and build your own parser for mathematical expressions. There are two or three approaches to such parsers, the best documented way is the way Dominus describes in his book Higher Order Perl. The second approach would be to use Parse::RecDescent, which allows a "natural" approach to parsing but has horribly bad error messages whenever you mess up your input. The third approach is to use Parse::YAPP, which is mostly like the yacc compiler builder, but for Perl. I found yacc (and P:YAPP) to be unapproachable first, but the speed made up for the steeper learning curve.
|
|---|