我使用
reval从Perl的
Safe模块,我想阻止它产生警告如果被eval’ed字符串不能被解析(实际上,我想阻止它产生任何警告的话).
例如,以下代码:
use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; my $x = $cft->reval(') 1' ); my $y = $cft->reval('2' ); say "x: $x"; say "y: $y";
结果是:
Number found where operator expected at (eval 5) line 1,near ") 1" (Missing operator before 1?) Use of uninitialized value $x in concatenation (.) or string at ./test line 12. x: y: 2
我想要实现的是$x = undef和$y = 2,没有警告.
我试图提出“没有警告;”在新范围内,但它对reval中产生的警告没有影响(尽管如@DavidO指出的那样,它会使’未初始化值’警告静音):
use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; { no warnings; my $x = $cft->reval(') 1' ); my $y = $cft->reval('2' ); say "x: $x"; say "y: $y"; }
我想不管怎么说’没有警告’必须在安全隔间内,所以我也尝试在“没有警告”前面加上被评估的字符串:
use strict; use warnings; use Safe; use feature qw/say/; my $cft = Safe->new; { my $x = $cft->reval( 'no warnings;' . ') 1' ); my $y = $cft->reval( 'no warnings;' . '2' ); say "x: $x"; say "y: $y"; }
这种方式reval不会发出任何警告,但两个变量都是undef:
Use of uninitialized value $x in concatenation (.) or string at ./test line 10. x: Use of uninitialized value $y in concatenation (.) or string at ./test line 11. y:
我不知道还有什么可以尝试,我希望问题描述足够清楚.