Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

gunzip exception

by luxs (Beadle)
on Feb 08, 2021 at 08:41 UTC ( [id://11128056]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to gunzip some data with Gzip::Faster; If the compressed data valid - everything works fine, but when the input data is bad or absent - i have error message and crash. EVAL do not help to stop it
use Gzip::Faster; my $gz = ''; my $data; eval { $data = gunzip( $gz ); }; if ($@) { say "can't unzip file"; exit; };
and the output is "Attempt to uncompress empty string at ....." with crashing of script without EVAL or continuing with EVAL How to handle this error and do not output any default messages? BUT! if the file is not empty but not proper format - in this case this script hanle error correctly!

Replies are listed 'Best First'.
Re: gunzip exception
by choroba (Cardinal) on Feb 08, 2021 at 09:41 UTC
    Probably not related to your problem, but checking $@ is unreliable. Switch to
    eval { $data = gunzip( $gz ); 1 } or { say "Can't unzip $file: $@"; exit 1 };

    See Bug in eval in pre-5.14 for more details.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      my $gz =''; my $data; eval { $data = gunzip( $gz ); 1; } or do { print "Error: $@"; exit; }; print "Done";
      Give me Attempt to uncompress empty string at ./aaa.pl line 10. Done; Which is prevent script from die, but do not catch a fatal error.... Thank you for link to the bug
Re: gunzip exception
by Corion (Patriarch) on Feb 08, 2021 at 08:52 UTC

    Maybe check if $gz is an empty string and don't try to call gunzip() in that case?

    I also can't reproduce your problem. eval catches the error message correctly for me with your code (amended below to show that):

    use 5.012; use warnings; use Gzip::Faster; my $gz=""; my $data; eval {$data=gunzip($gz)}; if($@) { say qq(error: $@) }; say "Done"

    This gives me the output:

    Attempt to uncompress empty string at ... Done

    Most likely the code you post is not the code you run.

      my $gz = 'f'; my $data; eval { $data = gunzip( $gz );}; if ($@) { say "Something wrong"; exit; }; say "done";
      Produce Attempt to uncompress empty string at ./aa.pl line 11. done which mean - generate fatal error but not recognize it YES, i will check that the file is not empty before using gzip - it will be easier for now
Re: gunzip exception
by choroba (Cardinal) on Feb 08, 2021 at 10:52 UTC
    Warnings don't stop running programs and aren't caught by eval.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Any idea how to catch it and process by my scripts?

        The warning is there to tell you (the programmer) that you're doing something wrong. The easiest step would be to stop doing the wrong thing.

Re: gunzip exception
by tybalt89 (Monsignor) on Feb 08, 2021 at 15:33 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11128056 use warnings; use Gzip::Faster; my $gz = ''; my $data; eval { local *STDERR; open STDERR, '>', '/dev/null'; $data = gunzip( $gz ); } or die "can't unzip file";

    Outputs just:

    can't unzip file at ./pm11128056.pl line 10.
Re: gunzip exception
by 1nickt (Canon) on Feb 09, 2021 at 01:56 UTC

    Hi,

    $@ is not set because an exception is not thrown. The module is only emitting a warning to STDERR. This is as documented.

    Hope this helps!


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11128056]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found