Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果宽字符错误,Perl停止打印_Perl - Fatal编程技术网

如果宽字符错误,Perl停止打印

如果宽字符错误,Perl停止打印,perl,Perl,我有一个简单的打印脚本 my $pdf_data = $agent->content; open my $ofh, '>:raw', "test.pdf" or die "Could not write: $!"; print {$ofh} $pdf_data; close $ofh; 有时我会收到“宽字符警告”,我知道为什么会收到此警告,并希望能够取消打印,而不是打印损坏的错误。差不多 if(wideCharWarning) { delete "test.pdf" } else{

我有一个简单的打印脚本

my $pdf_data = $agent->content;
open my $ofh, '>:raw', "test.pdf"
or die "Could not write: $!";
print {$ofh} $pdf_data;
close $ofh;
有时我会收到“宽字符警告”,我知道为什么会收到此警告,并希望能够取消打印,而不是打印损坏的错误。差不多

if(wideCharWarning)
{
delete "test.pdf"
}
else{
print {$ofh} $pdf_data;
}

您指定要打印字节(:raw),但不是

$ perl -we'
   open(my $fh, ">:raw", "file") or die $!;
   for (0..258) {
      print "$_\n";
      print $fh chr($_);
   }
'
...
249
250
251
252
253
254
255
256
Wide character in print at -e line 5.
257
Wide character in print at -e line 5.
258
Wide character in print at -e line 5.
要“取消打印”,只需检查打印内容是否包含非字节

die if $to_print =~ /[^\x00-\xFF]/;

如果要检测字符串是否包含宽字符,可以使用如下正则表达式:

/[^\x00-\xFF]/;

(正如ikegami在下面所指出的,我的第一个建议是不正确的:
/[^[:ascii:][]/;
将产生误报)

您可以设置一个
\uuuuuu警告\uuuu
信号处理程序,并根据警告消息执行任何操作

my $wideCharWarningsIssued = 0;
$SIG{__WARN__} = sub {
    $wideCharWarningsIssued += "@_" =~ /Wide character in .../;
    CORE::warn(@_);     # call the builtin warn as usual
};

print {$ofh} $data;
if ($wideCharWarningsIssued) {
    print "Never mind\n";
    close $ofh;
    unlink "test.pdf";
}

这消除了警告,但如果没有正确编码,我宁愿(如果可能的话)文档不打印。我知道这意味着该文档不是pdf文件,因此对其进行UTF-8编码将删除警告,但该文件仍然损坏。当我将raw更改为编码(utf8)时,我不再收到宽字符警告,并且输出仍然是损坏的pdf文件。@chrstahl89,我意外地将其设置为2秒。再读一遍:)啊,我的坏><谢谢,现在收到了!