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 0xd9How 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.
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...
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 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.
perlmonks.org content © perlmonks.org and ady, eff_i_g, Marcello, wazoox
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03