Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Others have suggested using Parse::RecDescent to create a parser for arithmetic expressions. That's all good advice, but you may want something less involved that writing your own parser. Maybe you should have a look at the Math::Symbolic module on CPAN. (Caveat: I'm the author.)

You could do this: (untested)

use Math::Symbolic qw/parse_from_string/; my $ms; eval { $ms = parse_from_string($expression) }; if ($@) {...} # catch parse errors my $value; eval { $value = $ms->value() }; if ($@) {...} # catch execution errors

The above code might break because Math::Symbolic supports variables - which have no value by default. So you can check that the user didn't use any variables in his expression before calling value():

# parse... unless ( $ms->is_constant() ) { # complain about variables in the tree } my $value; eval { $value = $ms->value() }; if ($@) {...} # catch execution errors

If you have to evaluate the expressions very often, you will find that calling ->value() is slow. (It walks the expression tree.) In that case, you can compile the tree to Perl code. You'd do that with ->to_sub():

# parse... # note the parens for list context my ($coderef) = $ms->to_sub(); my $value = $coderef->();

The module can do much more than that, of course. For something fancy, you can check out an experiment of mine, which is unfortunately rather undocumented: Math::SymbolicX::Calculator::Interface::Web. It's a feature-starved AJAX-enabled symbolic calculator with a worksheet-ish appearance. That's just a reference for your entertainment, though. Don't ever think of using that.

Admittedly, if I was sticking to my promise of this being less complex, I should have stopped after the second code snipped. :)

Cheers,
Steffen


In reply to Re: eval question by tsee
in thread eval question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-23 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found