I've just finished testing my initial implementation of a simple module you can use to get and set variables, define and call functions, and evaluate arbitrary Perl code. It simply formalizes calls to eval($src)
and ${"${pkg}::$varname"}
and whatnot: fairly common things that can be tricky to get right. It works by creating a unique, randomly named package and then accessing variables and functions in that package. (Duh.)
Right now it's called JOM::Shell::RunTime::UsingPackages
(I'm using it in a simple shell); it inherits from the abstract class JOM::Shell::RunTime
at some point I'll rename these for general consumption and post them to CPAN.
The following example is a nearly complete picture of what the module provides:
use JOM::Shell::RunTime::UsingPackages; my $runtime = JOM::Shell::RunTime::UsingPackages->new(); $runtime->setvar('@fib' => (0, 1, 1)); $runtime->defsub('fib' => <<'--EOS--'); { my ($n) = @_; push @fib, $fib[-2] + $fib[-1] while $#fib < $n; return $fib[-1]; } --EOS-- $runtime->callsub('fib', (9)) == 34 || die "Not 34"; $runtime->evaluate(<<'--EOS--'); $fib9 = fib(7) + fib(8); die unless $fib9 == fib(9); ); $foo = $runtime->getvar('$fib9');
There are some wrinkles to work out (notably, die
and warn
and their ilk won't produce very helpful error messages) but all in all it's pretty useable as is. Now to get my PAUSE ID and roll it up for release. But what name to use??