我使用的是Perl的Safe模塊中的reval,我想阻止它在生成警告時無法解析被評估的字符串(實際上,我想阻止它生成任何警告) 。「沒有警告;」在安全隔間
例如,下面的代碼:在
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 =民主基金和$ Y = 2,且無警告。 我試圖提出「沒有警告;」一個新的範圍內,但它從重估中產生的警告沒有效果(雖然,正如@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";
}
我想,不知何故'沒有警告'必須放在安全隔間內,所以我也試圖在「無警告」之前加上警告。琴絃被eval'ed:
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";
}
這樣重估不發出任何警告,但兩個變量都是民主基金:
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:
我不知道什麼嘗試,我希望問題描述足夠清楚。
您的第二次嘗試實際上不會產生與第一次相同的輸出。它仍然會產生「編譯時」錯誤(實際上是reval編譯時錯誤),但不會產生與在'say'語句中插入未初始化值有關的運行時警告。所以你已經在上面的第二個片段中解決了你的一半問題(壓制了警告)。另一半(壓制編譯時錯誤)更成問題。 – DavidO 2012-07-26 21:20:46
是的,你說得對。我甚至沒有注意到,因爲我在這裏主要關心的是重新評估 - 未初始化的變量警告是我試圖保持示例簡短的結果。無論如何,我已更新了該帖子以使其更清晰。謝謝! – andrefs 2012-07-26 21:33:01