Catching GD warning
Marcello
created: 2006-05-03 06:59:47
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
Re: Catching GD warning
created: 2006-05-03 09:50:00
Look at error trapping here: eval
Re: Catching GD warning
ady
created: 2006-05-03 10:42:48
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...
Re^2: Catching GD warning
created: 2006-05-03 10:59:18
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?
Re^3: Catching GD warning
created: 2006-05-03 11:19:21

Looks like a bug to me. If CPAN://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.

perlmonks.org content © perlmonks.org and ady, eff_i_g, Marcello, wazoox

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03