storing failes: binmode() on closed filehandle $out at … print() on closed filehandle $out

StackOverflow https://stackoverflow.com/questions/9952054

  •  28-05-2021
  •  | 
  •  

Question

see the update at the end of the intail posting

  • well i am a bit confued i have to admit...;-) 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.

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 "$_\n";
    $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!?

update

hello my dear

thx for the reply - guess that i have a permission issue here....

well i have 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 "$_\n";
    $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")or die $!;
  binmode $out;
    print $out $png;
    close $out;
    sleep 5;
  }

well this works - but all i can get is the storing into the directory where the test_8.pl resides

guess that this is a permission issue.

what can i do.

can i put the image-directory somewhere outside the folder of perl..? perhaps i have created either the

perl-directory or the images-directory with some special root permission.

what i did to solve the issues so far was

a- checkint the permissions on the folders - perl . perl/images

b.- running the script in command line as root user.

well allll i can get are the results that are stored in the folder,...

linux-wyee:/home/martin/perl_dev/perl # ls
.directory                    images                    module_test         pfh-gr.ch.png  phsg.ch.png          phtg.ch.png  schwyz.phz.ch.png  test_4.pl  test_8.pl        urls.txt
heilpaedagogik.phbern.ch.png  luzern.phz.ch.png         module_test.pl      phbern.ch.png  phsh.ch.png          phvs.ch.png  test_2.pl          test_6.pl  test_8.pl~       zug.phz.ch.png
hepfr.ch.png                  ma-shp.luzern.phz.ch.png  open-local-file.pl  phr.ch.png     ph-solothurn.ch.png  .png         test_3.pl          test_7.pl  unifr.chsfm.png
linux-wyee:/home/martin/perl_dev/perl # 

the images folder is empty

what can i do

sould i create a images folder outside the perl-directory

how to name the string-path to it..?!

dear buddy - we re allmost there - i am pretty sure - i guess that this is only a permission issue. But how to solve it!?

perhaps i have to create all the testfiles in a whole new directory again . not as root but as normal user!? What do you say!?

Was it helpful?

Solution

You weren't able to open the file for writing. Your path is /images, and you probably don't have permissions on that directory (if it even exists). Always check the return value of your calls to open, like you did in the first open.

If I were you, I wouldn't use /images. I'd download everything into directory I control and isn't cluttering the standard directory layouts. You should almost never create new directories under / if you aren't doing system administration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top