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

I am trying to reset certain pixels of an image which I have read in from a .gif file. This line at best just alters one pixel even though it is called many times with different values for the col and row.
$img_gry->Set('pixel[$col,$row]' => 'rgb($sub_red, $sub_green, $sub_bl +ue )');
However, if I store all the lines in an array using
$con_str = "\$img_gry->Set('pixel[$col,$row]' => 'rgb($sub_red, $sub_g +reen, $sub_blue)')"; push(@pix_con_str, $con_str);
and then process these using the following loop which uses eval, all the required changes are made.
for $jc (@pix_con_str) { eval($jc); }
I just wondered if anyone knew why this was the case. I can send more of the Perl script if that is of any use).

Replies are listed 'Best First'.
Re: Pixel setting oddity - Image:Magick
by Eily (Monsignor) on May 11, 2015 at 12:18 UTC

    In  $img_gry->Set('pixel[$col,$row]' => 'rgb($sub_red, $sub_green, $sub_blue )'); the single quotes prevent interpolation which means Image::Magick is given the strings '$col', and '$row' instead of the values you expect. I didn't read the documentation to see if anything else is wrong, but this does look highly suspicious. Aren't you getting any warnings for that? I'm surprised Image::Magick doesn't complain about the invalid input.

      Aren't you getting any warnings for that? I'm surprised Image::Magick doesn't complain about the invalid input.

      The posted code has a bare string eval without any error checks ($@), so most errors will be silent. Also, Image::Magick requires a lot of manual error checks because it does not use perl's exception mechanism (i.e. die), but instead requires checking return values (as in C). This is documented in http://www.imagemagick.org/script/perl-magick.php#exceptions. The posted code lacks those checks, too.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Thanks for the info on exception handling with Image::Magick!

        And yes, merrymonk fails to check $@ after the eval, but there wouldn't have been a warning there since this actually works (as in this case the variables are actually in a double quote context). And on the topic of safety and error checking, it does look like the code is not run under strict and warnings ; which would have done nothing for this particular case, but are still very helpful safety nets.