http://www.perlmonks.org?node_id=1160981


in reply to execute a string

If you want to implement a calculator, write a parser for the expressions. Marpa::R2 contains a simple calculator as an example, I modified it to handle variables, too, see Marpa Enhanced Calculator at GitHub.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,