Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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 如何使存档中的警告静音::Zip_Perl - Fatal编程技术网

Perl 如何使存档中的警告静音::Zip

Perl 如何使存档中的警告静音::Zip,perl,Perl,在提取存档文件时,我不希望看到以下消息 format error: file is too short at /usr/share/perl5/Archive/Zip/Archive.pm line 667. Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Archive=HASH(0x8acf47c)', 'IO::File=GLOB(0x8ac9d70)') called at /usr/share/p

在提取存档文件时,我不希望看到以下消息

format error: file is too short at /usr/share/perl5/Archive/Zip/Archive.pm line 667.    
Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Archive=HASH(0x8acf47c)', 'IO::File=GLOB(0x8ac9d70)') called at /usr/share/perl5/Archive/Zip/Archive.pm line 581
Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archive=HASH(0x8acf47c)', 'IO::File=GLOB(0x8ac9d70)', 'some zip file') called at /usr/share/perl5/Archive/Zip/Archive.pm line 548

我想跳过一些损坏的存档。

根据发出此错误的函数的调用位置,可以在本地重写警告处理程序以忽略警告

sub f {
   ...
   local $SIG{__WARN__} = sub { # do nothing };
} 
现在,函数f中发出的所有警告都被路由到本地警告处理程序,而本地警告处理程序不执行任何操作

您应该注意到,Archive::Zip有一个错误标志
AZ_OK
,这是大多数操作返回的。例如:

# Read a Zip file
my $somezip = Archive::Zip->new();
if ( $somezip->read( 'someZip.zip' ) != AZ_OK ) {
    die 'read error';
}
管道stderr到/dev/null


这适用于linux。对windows不确定???

搞乱SIGWARN处理程序或将STDERR输出重定向到空设备是非常不整洁的

Archive::Zip
具有设置错误处理程序的功能
Archive::Zip::setErrorHandler
。默认设置是使用
Carp::Carp
,它生成您看到的输出

您可以编写自己的替换,例如,将错误消息保存在数组中以供以后检查,如下所示

my @errors;
Archive::Zip::setErrorHandler(sub { push @errors, $_[0] });

另外,不要忘记检查函数的返回代码,否则将无法知道是否存在问题。

添加产生此错误的代码。
my @errors;
Archive::Zip::setErrorHandler(sub { push @errors, $_[0] });