Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^4: Use of uninitialized value $pic

by dazz (Sexton)
on Apr 25, 2017 at 05:40 UTC ( #1188818=note: print w/replies, xml ) Need Help??

in reply to Re^3: Use of uninitialized value $pic
in thread Use of uninitialized value $pic

Hello Yes, it wasn't a good night for troubleshooting code. I have incorporated your suggestions and fixed the code.
I used a hash because I have a couple of switches and args that determine which camera is being used and where the files should go.

Moving onto my next problem. I want to call a subroutine ImageNotDark. I want to pass an Image::Magick object ($pic).
By design, the object is not changed inside the subroutine. My problem is that I am not passing the image object correctly.
# Convert to Image::Maggick image my $pic = Image::Magick->new(magick=>'jpg'); # create new image obj +ect $pic->BlobToImage($img); #convert the grabbed blob image to Image::Ma +gick object ## test point A #pass the $pic object to the subroutine and measure image darkness. my $test = ImageNotDark ($pic); #
The code snippet at the start of the subroutine is as follows:
sub ImageNotDark { # Argument is one reference to an image object. # ImageNotDark compares the image with a blank black canvas. If the c +omparison is # above the defined threshold, the image is bright enough to send (not + dark). # the return value is true if the images is bright enough. my $iutImage = Image::Magick->new; #the image tested for darkness $iutImage->Read(@_); # get the image under test (iut)
So the debugger shows that $iutImage is not initialised. At test point A, I temporarily added code to save the $pic image to a file.
I was able to confirm that $pic holds a valid image. So I am reasonably certain the problem is the way I call the subroutine and attempt to pass the image. Any tips would be much appreciated.

Replies are listed 'Best First'.
Re^5: Use of uninitialized value $pic
by haukex (Bishop) on Apr 25, 2017 at 06:28 UTC
    Moving onto my next problem.

    As this question is unrelated to the rest of the thread, it'd have been better to post it in a new thread.

    You don't need to construct a new Image::Magick over the argument passed to the subroutine. The normal way to handle arguments is (see perlsub):

    sub ImageNotDark { my ($iutImage,$arg2,$arg3,...) = @_; # --OR-- my $iutImage = shift; my $arg2 = shift; ...

    Or, if your intention were to work with a copy of the image in the subroutine, you could simply do my $imgCopy = $_[0]->Clone(); inside the subroutine.

      That is diamond advice.
      My Google search results had not found the perlsub doc and I found conflicting or/and confusing advice around handling objects passed to subs.
      I am sure this will be helpful to others. I will raise this as a separate question and include your answer for the benefit of others who hit the same problem.
Re^5: Use of uninitialized value $pic
by zentara (Archbishop) on Apr 25, 2017 at 13:51 UTC
    Hi, just a thought. Shouldn't
    $iutImage->Read(@_); # get the image under test (iut)
    my $x = $iutImage->ReadImage($_[0]); warn "$x" if "$x";
    Also, you can try passing \$pic to pass the object by reference.

    I'm not really a human, but I play one on earth. ..... an animated JAPH
      I don't know. What does this do:
      warn "$x" if "$x";
      Google tells me it's a warning. Undef??


        It's a very ugly hack for error checking. See "Handling Exceptions" in the documentation of Image::Magick. Sane error handling would use die or the XS equivalent of die inside the Image::Magick code, but now it's too late for an incompatible change.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1188818]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2020-10-26 21:50 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (254 votes). Check out past polls.