【发布时间】:2013-06-12 10:29:47
【问题描述】:
我正在使用 Safe 模块以包含在 Mastering Perl 中。 v5.16 之前的版本(支持的最早版本)似乎不理解新关键字。我错过了什么吗?
说适用于 v5.16 及更高版本
use v5.10;
use Safe;
say "Running $0 under $^V with Safe ", Safe->VERSION;
my $compartment = Safe->new;
$compartment->permit( ':base_io', ':load' );
my $code =<<"CODE";
use v5.10;
say "Hello Safe!";
CODE
$compartment->reval( $code ) or do {
my $error = $@;
warn "Safe compartment error! $@";
};
这段代码在v5.18和v5.16下运行如我所料,这两个officially supported versions of Perl:
% perl5.18.0 safe.pl
Running safe.pl under v5.18.0 with Safe 2.35
Hello Safe!
% perl5.16.3 safe.pl
Running safe.pl under v5.16.3 with Safe 2.35
Hello Safe!
它在 v5.16 之前不起作用,因为它认为 say 关键字无效:
% perl5.14.4 safe.pl
Running safe.pl under v5.14.4 with Safe 2.35
String found where operator expected at (eval 5) line 2, near "say "Hello Safe!""
(Do you need to predeclare say?)
Safe compartment error! syntax error at (eval 5) line 2, near "say "Hello Safe!""
% perl5.12.3 safe.pl
Running safe.pl under v5.12.3 with Safe 2.35
String found where operator expected at (eval 5) line 2, near "say "Hello Safe!""
(Do you need to predeclare say?)
Safe compartment error! syntax error at (eval 5) line 2, near "say "Hello Safe!""
% perl5.10.1 safe.pl
Running safe.pl under v5.10.1 with Safe 2.35
String found where operator expected at (eval 5) line 2, near "say "Hello Safe!""
(Do you need to predeclare say?)
Safe compartment error! syntax error at (eval 5) line 2, near "say "Hello Safe!""
状态不起作用,但中断方式不同
state 不同。
use v5.10;
use Safe;
say "Running $0 under $^V with Safe ", Safe->VERSION;
my $compartment = Safe->new;
$compartment->permit( ':base_io', ':load' );
my $code =<<'CODE';
use v5.10;
print "Hello Safe!\n";
foo();
sub foo {
state $n = 0;
print "n is $n\n";
}
CODE
$compartment->reval( $code ) or do {
my $error = $@;
warn "Safe compartment error! $@";
};
v5.18 和 v5.16 认为 state 是语法错误:
% perl5.18.0 safe.pl
Running safe.pl under v5.18.0 with Safe 2.35
Hello Safe!
n is 0
% perl5.16.3 safe.pl
Running safe.pl under v5.16.3 with Safe 2.35
Hello Safe!
n is 0
在那些版本之前,我认为它将state 视为一种间接方法:
% perl5.14.4 safe.pl
Running safe.pl under v5.14.4 with Safe 2.35
Hello Safe!
Safe compartment error! Can't call method "state" on an undefined value at (eval 5) line 5.
给定
given也有同样的问题:
use v5.10;
use Safe;
say "Running $0 under $^V with Safe ", Safe->VERSION;
my $compartment = Safe->new;
$compartment->permit( ':base_io', ':load' );
my $code =<<'CODE';
use v5.10;
print "Hello Safe!\n";
my $foo = 'Buster Bean';
given( $foo ) {
when( /Buster/ ) { print "Buster\n" }
}
CODE
$compartment->reval( $code ) or do {
my $error = $@;
warn "Safe compartment error! $@";
};
在 v5.16 和 v5.18 中运行良好:
% perl5.18.0 safe.pl
Running safe.pl under v5.18.0 with Safe 2.35
given is experimental at (eval 5) line 4.
when is experimental at (eval 5) line 5.
Hello Safe!
Buster
但在早期版本中中断:
% perl5.14.4 safe.pl
Running safe.pl under v5.14.4 with Safe 2.35
Safe compartment error! syntax error at (eval 5) line 4, near ") {"
【问题讨论】:
-
我认为问题实际上出在 Opcode 中,但那是一个迷宫。