in reply to Re: Warning in objImageMagick
in thread Warning in objImageMagick

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 ...

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

    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

      Did this way and it oppressed the warnings:
      my $obj_temp = ""; $obj_temp = $objImageMagick->Read("dir/$filename"); warn "$obj_temp" if $obj_temp =~ /(\d+)/; # print the error message warn "Error Number: ".$1." Number of images read: ".0+$obj_temp if +$obj_temp =~ /(\d+)/;

        Hi Anonymous,

        Actually there's still a bug in there, you'll still get the same "Argument ... isn't numeric in addition" warning when Read fails. warn "message... ".0+$obj_temp is interpreted by Perl as warn "message... 0" + $obj_temp; (i.e. the concatenation happens first) instead of what you want, warn "message... ".(0+$obj_temp). Try it yourself: perl -MO=Deparse,-p -e 'warn "message... ".0+$obj_temp'

        The reason is the operators + and . have the same precedence but are left associative (compare the output of the aforementioned Deparse with this: perl -MO=Deparse,-p -e 'warn 0+$obj_temp."message... "'). You should use parentheses to set the precedence and for clarity.

        Regards,
        -- Hauke D