my $i = 0; EVAL("++$i; Do_Something()"); print "\$i equals $i\n";
would print "i equals 0", assuming Do_Something() does not change $i. We do not know in advance what variables may be used in the EVAL parameter.
Update: solution using threads given in Re^3, thanks to b4swine!
Keyboard entry is eval'ed (after some calculator-friendly preprocessing) as a Perl expression, and updates Answer only when special keys such as Return are encountered. However GUI buttons should update Answer more frequently, leading to difficulties. For instance, if $i has value zero, the line
(++$i)+1;
should of course give Answer "2". However if the ")+1" substring were entered via GUI buttons, then the following should happen:
")": evaluate first part, give Answer "1";
"*": do nothing for now;
"1": re-evaluate the whole string, and give Answer "2" (not "3" even though ++$i was evaluated again).
The whole string must be re-evaluated at this last step, since the user might have changed the contents of Command via the keyboard.
My aim is to make an app that perfectly mimics a typical calculator. Full compatibility with Perl is not desired, but I want scalar purely-numeric Perl expressions to evaluate correctly (except for some fairly useless expressions that are pre-interpreted in a calculator friendly way).
In reply to eval something using private copy of variables by gboole
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |