Thanks for the quick response, which seems very promising especially as I hadn't delved into namespaces before now. Unfortunately, while it solves one half of the problem (protecting outer copies of the variables), it does not solve the other (copying the outer variables to the inner namespace).
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:
for (;$line=<>;) {
$v = EVAL($line);
print "answer is $v\n";
}
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.
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?
|