in reply to Re: Re: Help with simple calculator script.
in thread Help with simple calculator script.

This is what I have now what is wrong

For one thing I've never known a calculator to need to know my name. That's kind of "2001: A Space Odyssey"-esque. Especially since I'm Dave. I can just see it now if I attempt to divide by zero: "I'm sorry Dave, I can't do that." It also reminds me of the fascenating little computer exhibits at the Exploratorium in San Fransisco that I used to love to play with back in 1978 when I was 10.

Beyond that, the point that people are making with regards to using eval versus a logic tree, is that perl already knows how to do math. And Perl is an interpreted language, so dynamic code is perfectly alright. eval can be used to execute dynamic code. So by using eval, the Perl interpreter (perl) becomes the math engine for you. You don't have to go to all the trouble of teaching your Perl script how to add and subtract, and parse mathematical expressions, if perl already knows how to do that for you. The Perlish solution therefore, is to let perl parse the expression. (And this reminds me again of the common phrase, "Only perl can parse Perl." ...but in this case it can be rewritten as "Let perl parse it for you as dynamic Perl."

eval does have its pitfalls. Because it would be perfectly happy to do whatever you ask of it as long as it is legal Perl. A mathematical expression is legal Perl. But so is something like " `rm -r /`". So you don't want to just give any old user access to a free eval from a CGI script or something. :)


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein