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

I'm working on a CGI script right now, and have come upon a problem with the following code:
my $Absolute_URL = "$gallerycfg{'UPLOAD'}/$imgparams{username}/$im +gparams{PicID}.jpg"; my $image = new Image::Magick; my ($image_x,$image_y) = $image->Ping($Absolute_URL); #Set to a high number for error trapping $imgparams{THREED}=5; #Determine if image is 3D based on width if ($image_x>$gallerycfg{'3DWIDTH'}){$imgparams{THREED}=1; }else{$imgparams{THREED}=0;} #Makes sure we got information on this picture. unless ($imgparams{THREED}<2){ &writelog ("Error blah blah $Absolute_URL.",1); &dieerr("The image blah blah inconvenience."); }
I've rewritten to code to prevent the problem, but what was happening was that if ImageMagick could not ping the image (that is, get its width), the script would cause the window's server memory usage to increase to over 800MB in less than a second. I looked in the task manager and found that the perl.exe process was using only around 20MB! Again, I rewrote the code to check on $image_x, but was wondering why so much memory would be allocated so quickly for an undefined variable problem and where that memory might have gone. Again, looking through the process list showed only a total of maybe 100MB memory useage. Any ideas? Thanks

Replies are listed 'Best First'.
Re: Undefined value in gt (>) -> 800MB memory usage
by Aristotle (Chancellor) on Jan 15, 2003 at 23:47 UTC

    Sounds like a bug in ImageMagick or (not very) possibly IIS. You'll probably have more luck asking on the ImageMagick mailing list. The problem is extremly unlikely to be the greater-than test, most likely it's the Ping method that goes into a tailspin.

    I wanted to comment on the following piece of code though:

    $imgparams{THREED}=5; if ($image_x>$gallerycfg{'3DWIDTH'}){$imgparams{THREED}=1; }else{$imgparams{THREED}=0;} unless ($imgparams{THREED}<2){ &writelog ("Error blah blah $Absolute_URL.",1); &dieerr("The image blah blah inconvenience."); }
    Which, really, is very ugly. For one, that longwinded if can be expressed with a lot more brevity: $imgparams{THREED} = $image_x > $gallerycfg{'3DWIDTH'} ? 1 : 0; See perlop for the ternary operator. Much more important though, is your error trapping practice. Perl has mechanisms to recognize an undefined variables as such, even to distinguish between a hash entry with and undefined value and one that doesn't even exist in the hash. In this case something like the following would be a lot more appropriate:
    unless (exists $imgparams{THREED}){ &writelog ("Error blah blah $Absolute_URL.",1); &dieerr("The image blah blah inconvenience."); }
    Then again, this makes no sense at all in your case, since the previous assignment will always either 0 or 1 to the key, so your error check is never going to trigger. I think what you were really trying to do here was something like this:
    my ($image_x, $image_y); unless(($image_x,$image_y) = $image->Ping($Absolute_URL)) { &writelog ("Error blah blah $Absolute_URL.",1); &dieerr("The image blah blah inconvenience."); }

    Makeshifts last the longest.