in reply to Re: Image::Magick resize question
in thread Image::Magick resize question

Yeah, I usually always try to check return values in my code. I actually do catch the return code in my open(image) code like you suggested.

My actual code is:
sub convert_images { my ($array, $start_directory) = ($_[0], $_[1]); foreach my $picture (@$array) { # read the image via a filehandle my $image = Image::Magick->new; open(IMAGE, "<$start_directory\\$picture") || die "Unable to o +pen image: $!"; $image->Read(file=>\*IMAGE) || die "Unable to read image: $!"; close(IMAGE); # resize the image to the correct width and height $image->Resize(geometry=>'450X300') || die "Unable to resize i +mage: $!"; # write the image back to disk $image->Write(filename=>"$picture", compression=>'None') || di +e "Unable to write image: $!"; } }
Even when I run this, the program does not die at the open command. If it did, I'd see my error message "Unable to open image: $!" somewhere in the script error message. That means the file is being opened correctly, right? The line that it complains about is the $image->Resize(geometry='450X300'); line. Am I doing something wrong there? Thanks for the response.

Replies are listed 'Best First'.
•Re: Re: Re: Image::Magick resize question
by merlyn (Sage) on Jun 08, 2002 at 14:17 UTC
    $image->Read(file=>\*IMAGE) || die "Unable to read image: $!";
    According to my docs, that should be:
    { my $result = $image->Read(file => \*IMAGE); die $result if $result; }
    Perhaps another pass over the manual would be useful, since your code is quite different from anything I've ever seen in the docs. No results are ever returned in $!.

    -- Randal L. Schwartz, Perl hacker

Re: Re: Re: Image::Magick resize question
by mt2k (Hermit) on Jun 08, 2002 at 22:32 UTC
    Just a question on the notation used for the return value for Image::Magick::Read. I understand from merlyn's post that the Read function does not return a result in the error variable ($!).

    Let's just pretend that it did return a value (even though it doesn't). Even then, wouldn't the line

    $image->Read(file=>\*IMAGE) || die "Unable to read image: $!";

    be better written as

    $image->Read(file=>\*IMAGE) or die "Unable to read image: $!";

    The difference being using or instead of ||. Not that it will do anything in this case, but when checking for return values in $!, is one not always suppose to use the "or" operator rather than "||"? I believe I was once told that using "or" (since it has a higher precedence) is always preferred when dealing with this type of situation. Did I hear wrong or am I right? Or does it depend upon the type of function called?

      my $y = int rand 10 or die; This will work correctly since or binds losely. 10 is regarded as a parameter to rand which is regarded as a parameter to int; the result will be tested with or and die 1 out of 10 times. my $y = int rand 10 || die; This will never die since || binds tightly and causes 10 || die to be evaluated first. The result of that expression is then passed to rand as a parameter, and so on.

      Sometimes, very rarely, you actually want the tight binding rather than the lose, however.

      Makeshifts last the longest.

        Cool! I was quite unaware of the difference between || and or. If it makes that much of a difference, I'll start using or in place of || when checking return errors. Thanks for the reply, the information was very useful.