gboole has asked for the wisdom of the Perl Monks concerning the following question:
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: eval something using private copy of variables
by snopal (Pilgrim) on Aug 19, 2007 at 16:06 UTC | |
|
Re: eval something using private copy of variables
by educated_foo (Vicar) on Aug 19, 2007 at 16:36 UTC | |
by gboole (Novice) on Aug 19, 2007 at 19:42 UTC | |
by b4swine (Pilgrim) on Aug 19, 2007 at 23:13 UTC | |
by gboole (Novice) on Aug 20, 2007 at 06:44 UTC |