use Safe;
$compartment = new Safe;
$compartment->permit(qw(time sort :browse));
$result = $compartment->reval($unsafe_code);
Obviously, it is object oriented. The object is the ``compartment'' in which to run the code.
The permit method opens up access to the specified operators in addition
to any already permitted. Some operators are allowed by default. It also
uses an operator tag to specify a group of operators to allow. Note that
the operators allowed by default are specified by the :default tag. For
more information on operator names, tags, and sets, see the Opcode module's
documentation.
The reval method evaluates the code in $unsafe_code in a new
namespace. Note that since nothing was shared with the compartment that
the only identifiers available to the compartment are the ``underscore''
variables (e.g. $_ and @_).
What happens if the code evaluated tries to access a name or operator that has not been allowed?
$@. Example:
Unable to create sub named "*Safe::Root3::unshared" at
/opt/TKLCelap/bin/rob_cmds line 1, <STDIN> chunk 7.
$@. Example:
unlink trapped by operation mask at /opt/TKLCelap/bin/rob_cmds line 1,
<STDIN> chunk 3.
Filehandles must be shared in order to be accessable. They are shared using the typeglob (e.g. *STDOUT).
Local subroutines used by shared subroutines need not be shared. However, subroutines in other packages must be shared separately.
Note that the operator mask does not apply to shared subroutines because they are not compiled as part of the eval.
Example from documentation:
varglob(VARNAME)Example from documentation:
$cpt = new Safe 'Root';
$Root::foo = "Hello world";
# Equivalent version which doesn't need to know $cpt's package name:
reval(STRING)rdo(FILENAME)