roperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi how can I ignore the following errors. I'm already handling the error in the code
my $unzip = Archive::Zip->new($zipfile); if ( ( defined($unzip) ) && ( $unzip eq "AZ_OK" ) ) { process zip ... } else { dont process.. $logger->info("error\n"); }
So I don't need to see the error below in my logs
IO error: reading end of central directory : at /opt/perl-5.26.0/lib/site_perl/5.26.0/Archive/Zip/Archive.pm line +656 thread 87. Archive::Zip::Archive::_readEndOfCentralDirectory(Archive::Zip +::Archive=HASH(0x18632b38), IO::File=GLOB(0x1b575298)) called at /opt +/perl-5.26.0/lib/site_perl/5.26.0/Archive/Zip/Archive.pm line 612 thr +ead 87 Archive::Zip::Archive::readFromFileHandle(Archive::Zip::Archiv +e=HASH(0x18632b38), IO::File=GLOB(0x1b575298), "bad.testfile.zip") ca +lled at /opt/perl-5.26.0/lib/site_perl/5.26.0/Archive/Zip/Archive.pm +line 574 thread 87
How can I handle this?

Replies are listed 'Best First'.
Re: Ignoring errors
by haukex (Archbishop) on Aug 28, 2017 at 15:30 UTC
    Archive::Zip::setErrorHandler( sub { } );

    should suppress the messages, although it will suppress all of them, which might be too much - you may want to write a custom handler, e.g. one that calls your $logger.

      that worked. Thanks!
Re: Ignoring errors
by Corion (Patriarch) on Aug 28, 2017 at 15:29 UTC

    Likely by wrapping the offending call in an eval block:

    my $unzip; my $ok = eval { $unzip = Archive::Zip->new($zipfile); 1; }; if( ! $ok ) { $logger->info(":-( $@"); };

    Also see Try::Tiny.

      Tried that with eval, this logging the error
Re: Ignoring errors
by LanX (Saint) on Aug 28, 2017 at 15:27 UTC
    I don't know what you are doing here and if Archive::Zip is used properly and if it provides an interface to deal with your requirement*. ( like a dedicated warnings class for no warnings )

    But in general you can override the error handlers in $SIG{"__WARN__"} (and even $SIG{"__DIE__"} ) and catch these cases before continuing to normal error handling.

    see warn for details ...

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    *) or an own errorHandler like Haukex++ showed