in reply to Catching GD warning

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...

Replies are listed 'Best First'.
Re^2: Catching GD warning
by Marcello (Hermit) on May 03, 2006 at 14:59 UTC
    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.