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

In reply to Re: Re: Re: Help with simple calculator script. by davido
in thread Help with simple calculator script. 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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.