Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

nkuitse (193)

nkuitse
  (email not shown publicly)
http://www.nkuitse.com/

Journal of nkuitse (193)

Wednesday February 19, 2003
09:18 PM

Runtime rigmarole

[ #10676 ]

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??

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.