Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Parsing Math Strings

by lhoward (Vicar)
on Dec 06, 2001 at 20:27 UTC ( [id://129964]=note: print w/replies, xml ) Need Help??


in reply to Parsing Math Strings

Several people have recomended using eval to solve this problem. Though eval will work, it can be very dangerous in situations where you do not have control over the data passed in to the eval (such as in the case of a cgi script). Consider if you were using evel and someone were to pass in system("rm -rf *") to your CGI program instead of the math expression you were expecting?

Replies are listed 'Best First'.
Re: Re: Parsing Math Strings
by belg4mit (Prior) on Dec 06, 2001 at 20:48 UTC
    So the solution would be:
    #You might want to do something to look for say sin, cos, tan, etc. unless( $str =~ /[^0-9+-*^/.,_()]/ ){ eval($str); }

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      Or use the Safe module and mask the operators that can be considered potentially harmful. This has the benefit of allowing the "math expression" to be arbitrarily complex (Perl, really), and if you pre-loaded things like Math::Complex, you could potentially have a very powerful "calculator."
        /me always forgets Safe (and Penguin)

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://129964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-03-28 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found