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

Re: Parsing Math Strings

by lhoward (Vicar)
on Dec 06, 2001 at 20:27 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2019-08-19 23:20 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (141 votes). Check out past polls.