snaporaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I am running a script (under Windows XP) that crawls websites using the Mechanize module. Sometimes I get request errors that "eval" doesn't catch, so the script breaks and I have to restart it manually, which is extremely inefficient. The code in question is this:
my $browser = WWW::Mechanize->new( autocheck=>1, agent=>"Firefox/1.0 ( +compatible; MSIE 6.0)"); my $url=... some url ...; my $filename=... local path name ...; my $response; eval { $response=$browser->get( $url, ":content_file" => "C:/$filename +" ) }; if ($@) { print "An error occurred ($@), continuing\n"; }
My question is: is there any way I can make sure that "eval" (or something else) catches every error that the "get" method returns, so that the script can go on? Thanks for your help, this is driving me crazy

Replies are listed 'Best First'.
Re: catch errors using Mechanize
by chanakya (Friar) on May 17, 2005 at 05:26 UTC
    snaporaz, here's the code I've used with WWW::Mechanize, without using eval
    #!/usr/bin/env perl5.8.6 use strict; use warnings; use WWW::Mechanize; use WWW::Mechanize::FormFiller; my $mech = WWW::Mechanize->new(); my $formfiller = WWW::Mechanize::FormFiller->new(); ## Grab the home page, $mech->get("http://someurl"); my $msg= <<EOF; Couldn't get the xxxx portal. EOF #If not success, send mail and print the response output $mech->success or die send_mail("Subject: xxxx Portal down", $msg_b +os) , $mech->response->status_line; ## Store new HTML content in a temporary file and open it for viewing +. open (OUT,">/tmp/login.results"); print OUT $mech->content(); close (OUT); exit(0);
    !!@!!
    May the force be with you
Re: catch errors using Mechanize
by thcsoft (Monk) on May 17, 2005 at 02:00 UTC
    hmm... funny that eval doesn't catch the errors. but maybe if you override $SIG{__DIE___}, e.g.
    local $SIG{__DIE__} = sub { print +shift, "\n" };


    language is a virus from outer space.
Re: catch errors using Mechanize
by cbrandtbuffalo (Deacon) on May 17, 2005 at 16:13 UTC
    You can also check $mech->status to get the status code. Anything other than a 200 is an error of some sort.