Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Use of uninitialized value $pic

by dazz (Sexton)
on Apr 24, 2017 at 08:54 UTC ( #1188743=perlquestion: print w/replies, xml ) Need Help??

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

I am stuck trying to debug a small piece of code.
This code grabs an image from an IP camera on my home network, timestamps the filename and then should save it to a file.
No matter what I do, I can't get past the uninitialised value error reported in the debug output below.

The most obvious bug would be that $snapshot->grab; is empty.
I have cut/pasted the URL from the code into my browser to make sure that the URL is correct and that I get an image. So I am reasonably confident that Grab is getting a valid image. As far as I can tell, $snapshot should hold a valid image but I don't know how to test the contents of $snapshot->grab; without first saving it to a file.
This code is running on a headless PC that I ssh into.
Google hasn't helped solve this problem.
my $snapshot = new Image::Grab; $snapshot->ua->timeout(5); # define the timeout for grabbing + an image. Default is 180 sec$ $snapshot-> url( "IPCamera{$key}"); #define the source of the image $snapshot->grab; #grab an image mark_blob(my $pic); $pic = $snapshot->image; #debug my $SaveFile = join("","$ImagePath{$key}",&timestamp,"\-snapshot\-$key +.jpg"); print ("Save filename: $SaveFile"); open(my $fh, ">$SaveFile") || die "$SaveFile: $!"; binmode $fh; # for MSDOS derivations. print $fh $pic; close $fh;
The debug output is as follows:
DB<1> c 42 main::( mark_blob(my $pic); DB<2> n main::( $pic = $snapshot->image; #debug DB<2> main::( my $SaveFile = join("","$ImagePath{$ke +y}",&timestamp,"\-snapshot\-$key.jpg"); DB<2> main::( print ("Save filename: $SaveFile"); DB<2> main::( open(my $fh, ">$SaveFile") || die "$Sa +veFile: $!"; DB<2> Save filename: /var/images/trackcam1/2017-04-23_20:40:01-snaps +hot-1.jpg main::( binmode $fh; # for MSDOS derivations. DB<2> main::( print $fh $pic; DB<2> s Use of uninitialized value $pic in print at line 50. at line 50. main::( close $fh;

Replies are listed 'Best First'.
Re: Use of uninitialized value $pic
by Eily (Monsignor) on Apr 24, 2017 at 09:29 UTC

    Well, the Image::Grab doesn't seem to provide any way to retrieve the error message. That's inconvenient. The documentation does indicate that you may use LWP::Simple if you're only using the base features.

    $snapshot-> url( "IPCamera{$key}"); looks very suspicious. If you want to be sure of what the argument to a call is, put it in a temporary variable and use that variable twice (for printing and in the call). The reason for using a temporary variable is that if the value is a more complex expression, you might not type twice the same thing, or you may change one and forget the other. With a temp variable and strict, you're sure that what you see is what you get.

    my $url = "IPCamera{$key}"; print "Trying to read from $url\n"; $snapshot-> url($url);

Re: Use of uninitialized value $pic
by dazz (Sexton) on Apr 24, 2017 at 09:30 UTC
    Hello I have added the statements
    my $info= $snapshot->type; print ("image info : $info"); #debug
    This fails as uninitialised as well. So it appears the problem is with the Image::Grab code.
    Knowing that doesn't help me solve the problem.
    I have pinged the camera to make sure it is reachable from my headless box.

    If Image::Grab is failing, I don't know why.
      SOLVED I found a typing error in the reference to the hash with the URL. The debugger/compiler didn't pick it up.
        SOLVED I found a typing error in the reference to the hash with the URL.

        And the typo was? I bet on

        - $snapshot-> url( "IPCamera{$key}"); + $snapshot-> url( "$IPCamera{$key}");

        BTW there's no need to put that into double quotes, or is there? if you had written

        $snapshot-> url( IPCamera{$key});

        the compiler would have catched that error.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        Yes, because it was in double quotes, so it was a valid string. You don't need the quote though, $snapshot->url( $IPCamera{$key} ); does pretty much the same thing. One difference is that if you forget the $ outside of a string, it is not valid code anymore. The double quotes also mean that perl turns $IPCamera{$key} into a string before using it as a parameter for url(). But I'm pretty sure that your url already is a string to begin with, so stringifying it is not necessary.

        Your question garnered very few replies which is often an indication that it wasn't well constructed. In this case using a literal string instead of the hash value would not only have made it clearer for us, but you'd actually have discovered the error an hour ago before you even submitted the question. A virtue of Perl Monks that has nothing to do with the community is that well asked questions often provide their own answer. :-)

        Premature optimization is the root of all job security

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1188743]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-10-21 08:06 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (212 votes). Check out past polls.