DESCRIPTION

This module define a subroutine that let you evaluate Perl code in a specific context. The code can be passed directly as a string or as a file name to read from. It also provides some subroutines to let you define and optionally share variables and subroutines between your code and the code you wish to evaluate. Finally there is some support for running your code in a safe compartment.

Don't play with fire!

Don't start using this module, or any other module, thinking it will let you take code from anywhere and be safe. Read perlsec, Safe, Opcode, Taint and other security related documents. Control your input.

SUBROUTINES/METHODS

Subroutines that are not part of the public interface are marked with [p].

new(@named_arguments)

Create an Eval::Context object. The object is used as a repository of "default" values for your code evaluations. The context can be used many times. The values can be temporarily overridden during the eval call.

The default is to use strict. Note that "Safe" in perldoc default is to NOT use strict (undocumented).

PRE_CODE - safe code you want to evaluate in the same context as the unsafe code

This let you, for example, use certain modules which provide subroutines to be used in the evaluated code. The default compartment is quite restrictive and you can't even use strict in it without tuning the safe compartment.

- Pass the same package name to your safe compartment and to Eval::Context.

- If you really want to be on the safe side, control your input. When you use a module, are you sure the module hasn't been fiddle with?

- Leave strict on. Even for trivial code.

INSTALL_VARIABLES - "Give me sugar baby" Ash.

Eval::Context has mechanisms you can use to set and share variables with the code you will evaluate. There are two sides in an Eval::Context. The caller-side, the side where the calls to eval are made and the eval-side, the side where the code to be evaluated is run.

How should you get values back from the eval-side

Although you can use the mechanisms below to get values from the eval-side, the cleanest way is to get the results directly from the eval call.

You can declare variables of any of the base types supported by perl. The initialization data , on the caller-side, is serialized and deserialized to make the values available on the eval-side. Modifying the variables on the eval-side does not modify the variables on the caller-side. The initialization data can be scalars or references and even my variables.

Persistent variables

When evaluating code many times in the same context, you may wish to have variables persist between evaluations. Eval::Context allows you to declare, define and control such state variables.

This mechanism lets you control which variables are persistent. Access to the persistent variables is controlled per eval run. Persistent variables are my variables on the eval-side. Modifying the variables on the eval-side does not modify the variables on the caller-side.

Define persistent variables:

# note: creating persistent variables in 'new' makes little sense as
# it will force those values in the persistent variables for every run.
# This may or may not be what you want.
my $context = new Eval::Context() ;
$context->eval
(
INSTALL_VARIABLES =>
[
[ '$scalar' => 42 => $Eval::Context::PERSISTENT ] ,
# make %hash and $hash available on the eval-side. both are
# initialized from the same caller-side hash
[ '%hash' => \%hash_caller_side => $Eval::Context::PERSISTENT ] ,
[ '$hash' => \%hash_caller_side => $Eval::Context::PERSISTENT ] ,
],
CODE => '$scalar++',
) ;

Although the first intent of persistent variables is to be used as state variables on the eval-side, you can get persistent variables values on the caller-side. To change the value of an eval-side persistent variable, simply reinitialize it with INSTALL_VARIABLES next time you call eval.

The mechanism above gave you fine control over persistent variables on the eval-side. The negative side is that only the variables you made persistent exist on the eval-side. Eval::Context has another mechanism that allows the eval-side to store variables between evaluations without the caller-side declaration of the variables.

To allow the eval-side to store any variable, add this to you new call.

By fine tuning EVAL_SIDE_PERSISTENT_VARIABLES you can control what variables are stored by the eval-side. This should seldom be used and only to help those storing data from the eval-side.

You may have notices in the code above that a package name was passed as argument to new. This is very important as the package names that are automatically generated differ for each eval call. If you want to run all you eval-side code in different packages (Eval::Context default behavior), you must tell Eval::Context where to store the eval-side values. This is done by setting CATEGORY

The validator sub can verify if the value to be stored are valid, E.G.: variable name, variable value is within range, ...

Here is an example of code run in different packages but can share variables. Only variables which names start with A are valid.