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

Fellow Monks:
I am wondering if there is anyone out there amongst my brethern that have had dealings with the Perl module: Image::TestJPG.
This module was recommended to me in a previous post as having the primary function of verify the integrity of a JPEG image file, (which is precisely what I want to do). With a slight little bit of tweaking to see what precisely (with the variables), what things were doing, I ran the test code provided with the documentation on a JPEG image file that was known to be good.
#!/usr/bin/perl -w use strict; use Image::TestJPG; my $file="/main/images/temp/image.jpg"; # read data from a file open(JPEG, "$file") or die "Can't open $file : $!\n"; my $jpgData = <JPEG>; close(JPEG); my $length=length($jpgData); print "LENGTH: $length\n"; # test the data my $rv = Image::TestJPG::testJPG($jpgData,$length); print "RV: $rv\n"; # do something based on the return value if($rv) { print " ... jpeg data is valid ...\n"; } else { print "... jpeg data contains errors ...\n"; }
According to the documentation for this module, the subroutine testJPG should return a value of 0 if the JPEG is bad, and 1 if it is good; thus in this case setting the variable $rv to 0 or 1 accordingly.
My initial results on the test of the "good JPEG" returned the results of "... jpeg data contains errors ..." . A new this was not correct because a knew for a fact that my JPEG was good. So... I suspected that something was fishy, so I added a diagnostic line to see what was actually being returned by the subroutine:
my $rv = Image::TestJPG::testJPG($jpgData, $length); print "RV: $rv\n";
If things were working properly according to the documentation, I should have gotten a 1 or a 0. Instead all I got was:
Use of uninitialized value in concatenation (.) or string at ./CCIMGVE +Rtest.pl line 20. RV:
This implied to me that something inside the subroutine was going wrong, or perhaps I was missing some subtlety not known to me. At one point I added:
binmode JPEG;
just after the open statement for reading in the image file, with the line of thought of forcing perhaps the file read into binary; however, this made no difference.

It would be really grat if I could get this module to work like it is supposed to according to the documentation.
FYI: The OS that I am running this is Red Hat Linux 9 and the Perl Version is 5.8.0 .
Thank you in advance for you help, insight and wisdom!

Replies are listed 'Best First'.
Re: Insight Into Image::TestJPG Module needed
by Joost (Canon) on May 09, 2005 at 13:40 UTC
Re: Insight Into Image::TestJPG Module needed
by davidrw (Prior) on May 09, 2005 at 13:37 UTC
    Hopefully Anonymous Monk's advice fixes your problem, but to explain your error message a litte, when you printed $RV and got the "Use of uninitialized value ..." warning, that was because $RV was undef after the testJPG() call, and the perl -w switch is on. This is kind of a trivial case, but note that if you wanted a pretty, warning-less debug statement, you could do something like:
    use Data::Dumper; print Dumper $RV; # or printf "RV: %s\n", defined $RV ? $RV : 'UNDEF'; # or even (to make sure $RV is always defined: my $rv = Image::TestJPG::testJPG($jpgData, $length) || 0;
Re: Insight Into Image::TestJPG Module needed
by Anonymous Monk on May 09, 2005 at 13:12 UTC
    Add binmode JPEG; after open.