Archive :: Zip membersMatching无法在Perl中找到方法?

时间:2015-01-28 03:20:36

标签: perl methods archive perl-module

我有一个使用Archive::Zip的脚本,我想使用方法membersMatching,但我无法弄清楚我缺少的是什么。

我在脚本的开头调用了模块:

use Archive::Zip qw( :ERROR_CODES :CONSTANTS :MISC_CONSTANTS );

这是使用模块的代码块:

while (my $file = readdir(TRIMMED_CELL_DIR)) {

    #Only if file ends in _1.fastqc.zip (only 1 instance per "trimmed" subdirectory.)
    if($file =~ /.*\_1\_fastqc\.zip/){  

        #Extract the file summary.txt and assign it to filehandle SUMMARY_R1.
        $file = "${trimmedDirectory}/${file}";
        print "Loading ZIP file: $file. \n";
        my $zip = Archive::Zip->new($file);
        my @txtFileMembers = $zip->membersMatching( '.*\.txt' );
        foreach my $txtFile (@txtFileMembers){
            extractMember($txtFile);
            open(SUMMARY_R1,"< $txtFile");
        }

    }

我一直收到错误Can't locate object method "membersMatching". ...我知道这与memberMatching方法没有导出有关,但我不知道如何在脚本中调用它。 Archive :: Zip的Te CPAN页面除了像这样使用它之外没有说什么:

  

membersMatching($ regex)   membersMatching({regex =&gt; $ regex})   返回其文件名匹配的成员数组,在列表上下文中给出正则表达式。返回匹配成员的数量   标量上下文。

    my @textFileMembers = $zip->membersMatching( '.*\.txt' );
    # or
    my $numberOfTextFiles = $zip->membersMatching( '.*\.txt' );

使用Archive::Zip->new($file)函数加载的ZIP文件有效,因此正在导出模块,而不是方法memebersMatching ...

1 个答案:

答案 0 :(得分:1)

检查zip文件的路径($file)。我认为它失败了。将您的代码更新为以下内容:

my $zip = Archive::Zip->new();
unless ( $zip->read( 'someZip.zip' ) == AZ_OK ) {
    die 'read error';
}

print "zip contains the following files:\n";
print "$_\n" for $zip->memberNames();