Although the issue at hand might be to correct the above example code, forgive me if I mention the obvious - i.e. that for evaluating formulas read in as data, the
eval function does seem rather suitable. Because it is functionally overkill, a filter function is also supplied in this quickie example:
#!/usr/bin/perl
do { print "CALC> ";
printf "%f\n", eval( Filter(<>) );
} until 0;
sub Filter {
my $exp = shift;
( $exp =~ /^\s*exit/ ) and return $exp;
( $exp =~ /^\s*(\w+)/ )
and return 'print STDERR "$1 denied\n"';
return $exp;
}
example session:
CALC> 0.7*0.9
0.630000
CALC> 0.6-0.8
-0.200000
CALC> mkdir freddy;
mkdir denied
CALC> exit
$
-S
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.