perl - 使用perl评估归档的解码文件

标签 perl error-handling try-catch eval

在这里,我正在读取目录中的文件列表,但我注意到某些文件无法解码,这行$tap3->decode($tap_file) or die $tap3->error;引发错误

每次我读取新文件时,如果该文件有效以进行解码,则我将对其进行更改列表,然后对该文件再次进行编码,否则,如果无法解码损坏的文件,我将执行另一项任务,但脚本拒绝

printDir(".");
sub printDir{
opendir(DIR, $_[0]);
my @files;
my @dirs;
(@files) = readdir(DIR);
foreach my $file (@files) {
    if (-f $file and substr($file,0,2) eq "CD")
    {
        my $tap3 = TAP3::Tap3edit->new;
        my $tap_file = $file;
        $result = $tap3->decode("tap_file"); ## decode key

        if ( defined  $result ) ## here if the file can be decoded then i do changes on it - then encode the file
        {
            ..
            ..
            tap3->encode($tap_file)  or  die $tap3->error;

        }else { ## here if the file courrpted "connot be  decoded" then i do another job ...
            ...
            ...
            $tap3->file_type("TAP");
            $tap3->version(3);
            $tap3->release(11);

            $tap3->structure($notific_struct);

            $tap3->encode($file) || die $tap3->error();

        }

    }

}
closedir(DIR);
}

但它看起来不适用于( defined $result )

最佳答案

考虑使用点击错误报告来警告错误。

       if ( defined  $result ) ## here if the file can not be decoded ...
        {
            ..
            ..
            tap3->encode($tap_file)  or  die $tap3->error;

        }else { ## here if the file courrpted "connot be  decoded" ...

            /// ADD THIS LINE
            warn "File: '$file_name' $tap3->error" ;
            /// CURRENT CODE FOLLOW
            $tap3->file_type("TAP");
            $tap3->version(3);
            ...
            .
        }

关于perl - 使用perl评估归档的解码文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59106514/

相关文章:

c# - 使用阻止在 VB.NET 中编译的自定义文本创建错误(C# 中的#error)

postgresql - 将具有108列的csv文件复制到postgresql

java - 开发 Android 应用程序时的最佳实践

typescript - 如何在TypeScript中尝试catch和finally语句?

java - 寻找一种更快的方法来执行字符串搜索

perl - 找不到错误 "Global symbol @xx requires explicit package name"

excel - 取消时Application.InputBox错误424

javascript - 如果 URL 不正确,fetch API 不会在 catch 中显示确切的错误

regex - 有人能指出我在这个 perl 程序中犯的错误吗?

arrays - 深度克隆 Moose 对象,其属性为 ArrayRef[Object] 和 Set::Object