Well, you can do this, but it is a sort of silly thing to do. A better solution would be to put the time in to actually evaluate the user's input and act on that. If you really want to build a calculator, I'd suggest using reverse polish notation on your first go (see dc(1)) because it is easier to parse (less ambiguous) than standard notation. Now for the silly way (which is worth seeing since evaluating strings as code is sometimes useful):
#!/usr/bin/perl -w use strict; my $eq = <STDIN>; print eval $eq, "\n";
This will do what you want, but it is very DANGEROUS because any arbitrary code can be embedded in the expression (although this isn't exactly a production project anyway). If you wanted to go to the trouble to catch non-calc-related code, you might as well code up a real calculator. If not, call perl -e and do a calulation on the command line.

In reply to Re: Inputing an Operator is it possible? by dbp
in thread Inputing an Operator is it possible? by $Variable_B

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.