storing issues - perl script runs nicely but does not write the intended files to "out"

well i am a bit confued i have to admit…:wink: being a novice perl-friend the perl-code allway looks a bit abracadaba…

i need to have some thumbnails from websites but i tried to use wget - but that does not work for me, since i need some rendering functions what is needet: i have a list of 2,500 URLs, one on each line, saved in a file. Then i want a script - see it below - to open the file, read a line, then retrieve the website and save the image as a small thumbnail. well since i have a bunch of web-sites (2500) i have to make up my mind about the naming of the results.

this is the urls.txt


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


So far so
good, well i think i try something like this




  #!/usr/bin/perl
  use strict;
  use warnings;

  use WWW::Mechanize::Firefox;

  my $mech = new WWW::Mechanize::Firefox();

  open my $urls, '<', 'urls.txt' or die $!;

  while (<$urls>) {
    chomp;
    next unless /^http/i;
    print "$_
";
    $mech->get($_);
    my $png = $mech->content_as_png;
    my $name = $_;
    $name =~ s#^http://##i;
    $name =~ s#/##g;
    $name =~ s/\s+\z//;
    $name =~ s/\A\s+//;
    $name =~ s/^www\.//;
    $name .= ".png";
  open(my $out, '>', "/images/$name");
  binmode $out;
    print $out $png;
    close $out;
    sleep 5;
  }


i get the following results now…
see what comes out…
and as far as i can see - there are no images stored in the folder “images”

why not!?



rtin@linux-wyee:~> cd perl
martin@linux-wyee:~/perl> perl test_8.pl
http://www.unifr.ch/sfm
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 2.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 2.
http://www.zug.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 3.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 3.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 4.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 4.
http://www.luzern.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 5.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 5.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 6.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 6.
http://www.phvs.ch

binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 14.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 14.                                                                                                                        
http://www.pfh-gr.ch                                                                                                                                                                            
Got status code 500 at test_8.pl line 15                                                                                                                                                        
martin@linux-wyee:~/perl>                                                                                  


what does the output want to say to me…
what can i do now!?

Looks like $out does not get opened in the first place. And what’s not opened cannot be worked with.

Shouldn’t it be:


open my $out, '<', "/images/$name" 

Again, just using my general programming knowledge and comparing to a line which causes no complaints, on the contrary, works.

Weird thing: the forums software inserts “http://forums.opensuse.org” before the “images” part if I leave out the “/” before “images”…???

Knurpht wrote:
> Looks like $out does not get opened in the first place. And what’s not
> opened cannot be worked with.
>
> Shouldn’t it be:
>
> Code:
> --------------------
>
> open my $out, ‘<’, “/images/$name”
>
> --------------------

Well, no, that opens it for input.

The OP needs to check thes results of functions for errors.

> Weird thing: the forums software inserts “http://forums.opensuse.org
> before the “images” part if I leave out the “/” before
> “images”…???

Yeah, yet another area where it’s broken. And a hint to what’s wrong in
the program, BTW.