in reply to Chomp a numerical expression, return the answer.

You want eval. But... you don't want it!

$ perl -lne "print eval" (7 * 3) + (2 + 1) 24

Now, to convince you that you don't want it, think about

"system 'rm -rf /'"

You may be interested in Safe.pm, but that's been reported not to be bullet-proof either.

Replies are listed 'Best First'.
Re^2: Chomp a numerical expression, return the answer.
by suaveant (Parson) on May 18, 2006 at 16:17 UTC
    or at the very least limit the input...

    Something like

    if($exp =~ /^[-+/*%()\s\d]+$/) { print eval $exp; }
    or something along those lines it would make it much harder to actually do anything underhanded.

    Exactly what taint mode encourages people to do.

                    - Ant
                    - Some of my best work - (1 2 3)

Re^2: Chomp a numerical expression, return the answer.
by sub_chick (Hermit) on May 18, 2006 at 17:50 UTC
    If eval is as big of a No-No as "system 'rm -rf /' ", I think I better seek an alternative.
    I'll do the suggested and put a strict limit on the input via a regex.

    Es gibt mehr im Leben als Bücher, weißt du. Aber nicht viel mehr. - (Die Smiths)"