Created
December 11, 2013 16:40
-
-
Save icydee/7913892 to your computer and use it in GitHub Desktop.
Running Perl in Safe Mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use lib "lib"; | |
use Safe; | |
use Data::Dumper; | |
use Ar; | |
my $compartment = new Safe; | |
$compartment->permit(":load",qw(pack sort print)); | |
our $state = {}; # Maintain state between calls. | |
our $debug = ""; # Debug information to print at the end. | |
our $ar = Ar->new({id => 1}); | |
sub w_print { | |
my ($val) = @_; | |
print $val; | |
} | |
$compartment->share('w_print'); | |
$compartment->share('$state'); | |
$compartment->share('$ar'); | |
$compartment->share('$debug'); | |
my $code = <<'EOT'; | |
use strict; | |
use warnings; | |
my $s = $state; | |
if (not $s->{init}) { | |
$s->{init} = 1; | |
$s->{counter} = 1; | |
} | |
else { | |
$s->{counter}++; | |
} | |
$s->{ar} = $ar->triple_id; | |
$s->{double} = $ar->double($s->{counter}); | |
$debug = "state: { init: ".$state->{init}.", counter: ".$state->{counter}.", double: ".$state->{double}.", }"; | |
EOT | |
for (my $i = 0; $i < 10; $i++) { | |
my $result = $compartment->reval($code); | |
if ($@) { | |
print "ERROR: $@"; | |
exit; | |
} | |
} | |
print 'result: '.Dumper($state); | |
print 'debug: '.$debug."\n"; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment