in reply to Re^2: How to use Safe to compile anonymous subs
in thread How to use Safe to compile anonymous subs

You can by sharing variables. Somehow I didn't manage to do with simply sharing a variable and localizing it, but I'm quite sure it can be done.

This is my workaround for now:

#!/usr/bin/perl use Safe; use IO::File; our @args; my $code =<<'END'; my @args = pass_options(); return "Arguments: @args\n"; END my $compartment = Safe->new; sub pass_options { @args }; $compartment->share('&pass_options'); $safe = sub { local @args = @_; $compartment->reval($code) || die $@}; print $safe->(1, 2, 3); __END__ Arguments: 123

This takes the sideway of sharing a sub that returns the arguments. Not ideal, but at least it works.