Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:19:55+00:00 2026-05-31T03:19:55+00:00

hello dear developers. first of all – sorry for being the newbie.. i am

  • 0

hello dear developers.

first of all – sorry for being the newbie.. i am pretty new to Perl.

i am trying to learn something about perl while playin around with code – and snippets.
Today i have a little script that runs a mechanize job.. but somewhat does not run to the end. Waht is aimed: i want to get some thumbnails of wesite-sceenshots.

well i run this script , which is written to do some screenshots of websites i have also up and running mozrepl. whats strange is the output – see below… question: should i do change the script why do i ge the output?

#!/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);
}

what the code gives badck is following

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

what i have doen so fare to get rid of the issues:
well i can use the diagnostics-pragma to get more insights into what is happening…
Alternatively, print() on closed filehandle OUTPUT also gives us lots of answers that
will tell us that we did not use autodie and also did not check the return value of open.

hmmm – well i just mused on the filehandle

well: the open call failed and since you assumed it was successful and proceeded to attempt to use the filehandle (which was not opened), you received that error.

The lesson here to learn is that we should ALWAYS check the return code of an open call to verify that it was successful and take proper action if it wasn’t.

well – i guess that i have to learn here some perl-issues… I guess that i have to correct the code accordingly.

we should also take care and should use the 3 arg form of open and a lexical var for the filehandle.

hmm what about this one here.
Code:

open my $out_fh, '>', $name or die "failed to create/open '$name' <$!>";

I just could build this part into the original code.. whatcha think?

#!/usr/bin/perl




use strict;
use warnings;
use WWW::Mechanize::Firefox;

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

open my $out_fh, '>', $name or die "failed to create/open '$name' <$!>";


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);
}

well
what do you think?

how would you change the code – and make sure that the script will run successfully…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:20:17+00:00Added an answer on May 31, 2026 at 3:20 am

    For one thing, your input contains slashes and then you are trying to use that input to create a filename. Since your input begins with “http://www” and not “www“, your substitution operation doesn’t do anything, either.

    my $name = "$_";            # e.g. $name <= "http://www.zug.phz.ch"
    $name =~s/^www\.//;         # $name still is "http://www.zug.phz.ch"
    $name .= ".png";            # $name is ""http://www.zug.phz.ch.png"
    open(OUTPUT, ">$name");     # error: no directory named "./http:"
    print OUTPUT $png;
    sleep (5);
    

    You’ll want to do a better job of sanitizing your filename. Maybe something like

    $name =~ s![:/]+!-!g; #http://foo.com/bar.html  becomes  http-foo.com-bar.html
    

    And if anything, you return value you want to check is in the open call inside your while loop. If you had said

    open(OUTPUT,">$name") or warn "Failed to open '$name': $!";
    

    you probably would have figured this out on your own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello dear programmers, I am very new to android application development and for practice
Hello all i got a mail code like this body = Dear & cName
Hello There, I am new to phonegap.I am trying to record a audio clip
Hello there I am new to php and want to learn to write reusable
hello dear developers! what are benefits of using rest api + oAuth in Rails
Hello Dear StackOverflowers, I am new to web programming and finding the server-client mixture
hello and good day dear xml-friends, i am new to xml so do not
hello and good day dear xml-friends, i am new to xml so do not
Hello dear Stackoverflowers ! I work on a CardDAV sync source for Android 2.3.3,
Hello all I am working on javascript jquery and svg.I want to ask a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.