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

Dear Monks, I want to gain information from several websites using WWW::Mechanize and the following code:
use WWW:Mechanize $website=WWW::Mechanize->new(); $website->get('http://www.website.com'); $link_id=0; while(1) { $link_id++; if($website->follow_link(n=>$link_id)) { do_something(); $website->back(); } else { last; } }
The problem is, that the program crashes if the link is not found or a link cannot be followed (e.g. an email address returns the error 'can't find sendmail' and exits). How can I prevent the program from crashing?

Replies are listed 'Best First'.
Re: WWW::Mechanize - Check if link can be followed
by Corion (Patriarch) on Jun 25, 2013 at 07:23 UTC

    Did you read the WWW::Mechanize documentation about the autocheck attribute?

      Thanks. I didn't know that. $website=WWW::Mechanize->new(autocheck=>0); did the job.

        You might want to add error checking to your code now, because now WWW::Mechanize will not automatically stop the program when it encounters an error.