On Sat, Dec 13, 2008 at 13:26, hotkitty <stpra...@gmail.com> wrote:
> HI,
>
> I have a bunch of news websites that are stored in my mysql db and
> each morning I have a script to go to each site and download the top
> stories (this is for personal use, not commercial). My problem is that
> sometimes www::mechanize will fail to get the website because the
> server is busy, or for whatever reason. HOw do I get it so that
> instead of dying right then and there it will just ignore it and move
> onto the next page?
snip

You can use a block eval* to catch the die the module is throwing.
This is similar to some languages try/catch syntax:


while ( $ref = $st->fetchrow_arrayref() ) {
    print "Retrieving news stories from $page\n";
    eval {
        my $mech=WWW::Mechanize->new(autocheck => 1);
        #this line dies if it cannot get to the web server
        $mech->get($page);
        #this line dies if it gets to the web server, but it
        #doesn't get a good http status code back (e.g. a 404 or 403)
        die $mech->status, " ", $mech->content unless $mech->status == 200;
        print $mech->content, "\n\n\n";
        1;
    } or {
        print "Could not get page: $@";
    }
}

* http://perldoc.perl.org/functions/eval.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to