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

Hi Monks!

I am trying to get rid of a warning in this line of code, but can understand why its happening, I even tried to initialize the variable but it is still happening . Has anyone encounter similar problem like this before?
That's the Warning:
Warning: something's wrong at ...
The code line:
... my $obj_temp = ""; $obj_temp = $objImageMagick->Read("dir/$file_name") or warn ($obj_t +emp); ...

Thanks for looking!

Replies are listed 'Best First'.
Re: Warning in objImageMagick
by haukex (Archbishop) on Apr 02, 2016 at 16:56 UTC

    Hi Anonymous,

    The documentation says:

    All PerlMagick methods return an undefined string context upon success. If any problems occur, the error is returned as a string with an embedded numeric status code.

    Which means you've got your condition reversed - with or, your warning is displayed when Read returns a false value, but actually, a false value ("an undefined string") means success. There's also this piece of example code:

    $x = $image->Read(...); warn "$x" if "$x"; # print the error message $x =~ /(\d+)/; print $1; # print the error number print 0+$x; # print the number of images read

    Update: It's been quite a while since I used PerlMagick, but as the documentation and a quick test of the sample code makes clear, the value returned by Read is special and somewhat unusual, so it warrants further explanation: It actually has two different values, one in string context (the error message with an embedded numeric status code, possibly making things even more confusing) and one in numeric context (the number of images read). This means that "$x" and 0+$x, as in the above example, will return two different values. Because it's easy to mix up string and numeric contexts, you should be careful with the return values and follow the code examples from the documentation, i.e. checking "$x" for errors instead of just plain $x.

    Hope this helps,
    -- Hauke D

      It did help, tried to apply this code:
      ... my $obj_temp = ""; $obj_temp = $objImageMagick->Read("dir/$filename"); "$obj_temp" if "$obj_temp";# print the error message $obj_temp =~ /(\d+)/; warn "Error Number: ".$1." Number of images read: ".0+$obj_temp;

      Warning messages now:
      Use of uninitialized value $1 in concatenation (.) or string at ... Argument "Error Number: Number of images read: 0" isn't numeric in ad +dition (+) at 1 at ... Warning: something's wrong at ...

        Hi Anonymous,

        It looks like you missed the warn statement in front of "$obj_temp" if "$obj_temp", and your precedence on the .0+$obj_temp in the last warn isn't quite right. Also, you'll probably want to further inspect $obj_temp only if it actually was an error, something like:

        my $obj_temp = $objImageMagick->Read("foo.jpg","bar.jpg"); if ("$obj_temp") { $obj_temp =~ /(\d+)/; warn "Error Number $1, number of images read: ".(0+$obj_temp); # handle the error further, like maybe "die" }

        When I run this with the file foo.jpg existing and the file bar.jpg not existing, I get the output:

        Error Number 435, number of images read: 1 at ....

        Hope this helps,
        -- Hauke D