irah has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have to execute a math operation, but the operation itself will be dynamic. For example, I have three different variables a,b,c with values 4, 2 and "-" (operator) respectively. Please let me know how can I evaluate the statement and produce the result 2 i.e (4-2 = 2)

  • Comment on Execute a string which has math operation

Replies are listed 'Best First'.
Re: Execute a string which has math operation
by Athanasius (Archbishop) on Jan 05, 2014 at 04:40 UTC
      That should be sanitized somehow. IDK exactly how but that looks like perl injection/rooting.

        It is - using eval to execute user supplied 'stuff' is dangerous. However it's _most_ dangerous when the program runs as a privileged user (e.g. web server, database instance). If I write a script, and then 'break' it, then I don't elevate my privileges, so at best it's a cute trick, on a part with using perl to 'process' STDIN.

        A case in point

        If however, you do have potential privilege escalation, then it's very important to sanitise your inputs. Normally, you'd do this by 'whitelisting' certain characters (e.g. numbers + arithmetic operators) and removing anything that isn't. URI::Escape may be useful for that - if you do it right, a regular expression will do the trick, but I can't elaborate off the top of my head.

      Thanks Athanasius

      I was tried to execute directly without string into the variable. Thank you so much.

Re: Execute a string which has math operation
by Anonymous Monk on Jan 05, 2014 at 07:29 UTC