well i run this script , which is written to do some screenshots of websites i have also up and running mozrepl
here we have the file with some of the requested urls ... note this is only a short snippet of the real list - the real list is much much longer. it contains more than 3500 lines and URLs
http://www.unifr.ch/sfm
http://www.zug.phz.ch
http://www.schwyz.phz.ch
http://www.luzern.phz.ch
http://www.schwyz.phz.ch
http://www.phvs.ch
http://www.phtg.ch
http://www.phsg.ch
http://www.phsh.ch
http://www.phr.ch
http://www.hepfr.ch/
http://www.phbern.ch
http://www.ph-solothurn.ch
http://www.pfh-gr.ch
http://www.ma-shp.luzern.phz.ch
http://www.heilpaedagogik.phbern.ch/
whats strange is the output - see below... question: should i do change the script
why do i ge the output with the following little script:
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;
my $mech = new WWW::Mechanize::Firefox();
open(INPUT, "<urls.txt") or die $!;
while (<INPUT>) {
chomp;
print "$_\n";
$mech->get($_);
my $png = $mech->content_as_png();
my $name = "$_";
$name =~s/^www\.//;
$name .= ".png";
open(OUTPUT, ">$name");
print OUTPUT $png;
sleep (5);
}
see here the well overwhelming output - to be frank i never have thught to get such a funny output .. i have to debug the whole code.... see below,
http://www.unifr.ch/sfm
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 2.
http://www.zug.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 3.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 4.
http://www.luzern.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 5.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 6.
http://www.phvs.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 7.
http://www.phtg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 8.
http://www.phsg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 9.
http://www.phsh.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 10.
http://www.phr.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 11.
http://www.hepfr.ch/
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 12.
http://www.phbern.ch
Some musings: well -firstly, i think this is not a very serious error - i think i have to debug it and then it will work better. Second, i firstly thought that the script seemed "to overload the machine"? Now i am not very sure about that: the symptoms do look strange but i guess that it is not neecessary to conclude an "overloading of the machine"
Third, well i think of certain steps that have to be taken to ensure that the problem is at all related to WWW::Mechanize::Firefox at all? This leads me to the point to what Perl warning means and to the idea to use the diagnostics pragma to get more explanation: what do you think?
print() on unopened filehandle FH at -e line 1 (#2) (W unopened) An I/O operation was attempted on a filehandle that w +as never initialized.
firstly - we need to do an open(), a sysopen(), or a so +cket() call, or call a constructor from the FileHandle package besides that - alternatively, print() on closed filehandle OUTPUT also gives lots of answers that will tell us that we did not use autodie and also did not check the return value of open. Above all i have to debug it and make sure to find where the error comes into play[/QUOTE]
But after some musings i think that it is worth to have a closer look at all test things-, what do you think about the idea always test to make sure the file is open before using it.That means that we should also get in the habit of using the three
arg open():
open my $fh, '>', $name or die "Can't open file $name : $!";
print $fh $stuff;
well - i guess that we can or should work around this without using die()
,
but we d have to manually have some method to let us know which files couldn't be created. In our case, it looks like all of them....that are shown above...
update in choosing a good file name you mean that i need to have a file name to store the images.. Note: i want to store all of them locally. But if i have a huge list of urls then i get a huge list of output files. Therefore i need to have good file names. Can we reflect those things and needs in the programme!?
the very latest update; there seem to be some errors with mechanize.... i guess so!!!
I you would like to print out binary data (jpg file), you have to set it explicitly. Second, close a filehandler if you does not need it anymore and you 'or die' on open. Third choose a good file name.
Regards,
http://perldoc.perl.org/functions/binmode.html
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;
my $mech = new WWW::Mechanize::Firefox();
open(INPUT, "<urls.txt") or die $!;
while (<INPUT>) {
chomp;
next if $_ =~ m/http/i;
print "$_\n";
$mech->get($_);
my $png = $mech->content_as_png();
my $name = "$_";
$name =~s#http://##is;
$name =~s#/##gis;$name =~s#\s+\z##is;$name =~s#\A\s+##is;
$name =~s/^www\.//;
$name .= ".png";
open(my $out, ">",$name) or die $!;
binmode($out);
print $out $png;
close($out);
sleep (5);
}