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

I have a perl mechanize script that works great if the script is able to submit the form. However, if it is unable to to submit for any reason, I get an internal server error. The script gives me this error when I am checking the 'success' value. It never reaches the 'else' statement below:
my $m = WWW::Mechanize->new( autocheck=>1); $m->get($url); if ($m->success) { $m->submit_form( form_id => 'comment-form', fields => { author => $author, email => $email, url => '', text => $comment, }, ); my $response = $m->response(); print "Your comment has been posted and will appear shortly."; } else { print "Was not able to connect to website."; }
Any tips on what I can do so that it doesn't 'die' if there is an error. I had though that the code above would be enough but apparently it's not. Thank you in advance.

Replies are listed 'Best First'.
Re: Perl Mechanize script dies during success check
by philipbailey (Curate) on Mar 26, 2010 at 19:14 UTC
    I believe the autocheck=>1 in your first line is your problem. This causes Mechanize to die on an I/O error. In older versions you could simply omit mention of autocheck, but it is now the default unless you are subclassing WWW:Mechanize, and you should explicitly use autocheck => 0
Re: Perl Mechanize script dies during success check
by crashtest (Curate) on Mar 26, 2010 at 19:57 UTC

    From the documentation:

    autocheck => [0|1]
    Checks each request made to see if it was successful. This saves you the trouble of manually checking yourself. Any errors found are errors, not warnings.

    philipbailey touched on a recent meditation about how defaulting autocheck to on broke many things for one particular monk. But in your case, you turned it on explicitly, so that monk's (mostly valid) complaints aren't applicable. You opted for autocheck yourself - and WWW::Mechanize is doing what you asked it to.