Search code examples
perlerror-handlingtry-catcheval

eval filed decoded files with perl


here I'm reading a list of files in directory But I noticed that some files can't be decoded it throws an error with this line $tap3->decode($tap_file) or die $tap3->error;

each time im reading the new file if the file valid to decode then i will do list of changes on it and after that encode the file again, else if the file corrupted cannot be decoded i will do another task but the script refused that

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);
}

but its looks like it does not work at ( defined $result )


Solution

  • Consider using the tap error reporting to warn on the error.

           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);
                ...
                .
            }