For instance if I initialize $i to 3 via "$i=3;" or "local $i=3;" early in main, and then feed line input to EVAL via:
then $i is initially undefined in the local package, so the input "++$i" gives "answer is 1" every time, rather than the desired "answer is 4" every time.for (;$line=<>;) { $v = EVAL($line); print "answer is $v\n"; }
I would like to import from main all variables that are used in this particular call to EVAL(). More precisely, when EVAL($line) encounters a variable ($i, say) while evaluating $line:
(b) if $i is already defined in the local namespace, use that value (could happen if $i were assigned earlier in the evaluation of the same $line).
(a) otherwise, import $i from main;
If the only solution is to explicitly import named variables we would need to parse $line to discover what variables it uses. This seems like an ugly non-ideal solution but it is feasible as a last resort because I only want a solution that works right for most scalar purely numeric expressions, rather than one that works when $line includes strings, function calls, etc. But is there a more beautiful solution?
In reply to Re^2: eval something using private copy of variables
by gboole
in thread eval something using private copy of variables
by gboole
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |