Hi
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}",×tamp,"\-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::(grab-images.pl:42): mark_blob(my $pic);
DB<2> n
main::(grab-images.pl:43): $pic = $snapshot->image; #debug
DB<2>
main::(grab-images.pl:45): my $SaveFile = join("","$ImagePath{$ke
+y}",×tamp,"\-snapshot\-$key.jpg");
DB<2>
main::(grab-images.pl:46): print ("Save filename: $SaveFile");
DB<2>
main::(grab-images.pl:48): 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::(grab-images.pl:49): binmode $fh; # for MSDOS derivations.
DB<2>
main::(grab-images.pl:50): print $fh $pic;
DB<2> s
Use of uninitialized value $pic in print at grab-images.pl line 50.
at grab-images.pl line 50.
main::(grab-images.pl:51): close $fh;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.