in reply to Re^2: eval something using private copy of variables
in thread eval something using private copy of variables

Seems like an overkill, but if you really want ALL your variables available for the user, that essentially is equivalent to making a copy of the entire symbol table, and handing it over the the user to mess with. The only way to reconstruct it is to save everything. That is essentially a fork or a thread, so this works
use threads; my $x=1; my $threads = threads->new(sub {eval '(++$x)+1'}); print "The value of \$x before eval is $x\n"; my $val = $threads->join(); print "The eval returned $val\n"; print "The value of \$x after eval is $x\n";
giving the result
The value of $x before eval is 1 The eval returned 3 The value of $x after eval is 1
It seems like overkill, but then, you asked for it. Less expensive might be to copy that part of the symbol table that you want shared with the user, and restore it after they are done.

Replies are listed 'Best First'.
Re^4: eval something using private copy of variables
by gboole (Novice) on Aug 20, 2007 at 06:44 UTC
    This particular swine thanks you for your Perl wisdom! I didn't know of the concept of Perl threads but they seem perfect for my application.

    Of course copying the whole symbol table seems like overkill, but this is for a resource-light calculator program, so the extra time and memory involved seems fine e.g. it took 8K-15K microsec for repeat runs of one simple threaded test vs 70-150 microsec for unthreaded. Waiting .015 sec or so for an answer from a calculator seems acceptable. Since Perl cleans up threads after use, all should be well even if the calculator is left running for a long time.