Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Catching GD warning

by Marcello (Hermit)
on May 03, 2006 at 10:59 UTC ( #547119=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm having problems catching a certain GD warning when using a (partially) corrupt JPEG file.
use strict; use warnings; use GD; my $image = GD::Image->new("test.jpg");
Running this script produces the following warning on the command-line:
Corrupt JPEG data: 104 extraneous bytes before marker 0xd9
How can I catch this error in Perl? $SIG{__WARN__} is not working here. I would like to catch this error so I can remove the extraneous bytes from the original JPEG file.

Thanks

Replies are listed 'Best First'.
Re: Catching GD warning
by eff_i_g (Curate) on May 03, 2006 at 13:50 UTC
    Look at error trapping here: eval
Re: Catching GD warning
by ady (Deacon) on May 03, 2006 at 14:42 UTC
    use strict; use warnings; use GD; my $image; # $image = GD::Image->new($data) # If something goes wrong, this call will return undef. # so one could do : ($image = GD::Image->new("test.jpg")) or warn "Error: $!"; # or if (! ($image = GD::Image->new("test.jpg")) ) { print "my error handling\n"; } # eval trapping would work too...
      Well actually no, I already tried that:
      use strict; use warnings; use GD; $SIG{__WARN__} = sub { print "Caught warning: ".$_[0] }; my $image; eval { ($image = GD::Image->new("test.jpg")) or warn "Error: ".$!; }; if ($@) { print "Caught ".$@; } if (defined($image)) { print "Image is defined: ".$image."\n"; }
      produces:
      Corrupt JPEG data: 104 extraneous bytes before marker 0xd9 Image is defined: GD::Image=SCALAR(0x817eba4)
      What am I missing?

        Looks like a bug to me. If GD::Image is sending something to STDERR (like it seems to) then it should be manageable thru $SIG{__WARN__}. Try redirecting the STDERR and see what's happenning.

        Update: Looks like actually XS Module stderr can't be easily catched, there's even a module for that: IO::CaptureOutput, hence that should provide you with the solution to your problem.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-06-04 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (30 votes). Check out past polls.

    Notices?