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

I'm using Mech to do some remote PHP work on a server. Some times the link is down and the program dies. It's part of a group of tasks that I'd rather just keep running. So don't die on fatal error, maybe just print message, or retry but don't drop kick the script. Suggestions

sub zero_status_file {
my $url = 'http://www.foobar.com/stuff/weed14.php?';
my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->get($url);
my $status = $mech->status();
$mech->submit_form(form_name => 'zerofile', fields => { longstat => "@reverse_remote_coins" }, button => "zcc" );
my $mech_stat = $mech->status();


ends with 'uncaught exception from user program....
www::mech :: die....
thanks monk

Replies are listed 'Best First'.
Re: WWW::Mechanize goes bang
by Corion (Patriarch) on Mar 14, 2008 at 17:45 UTC
Re: WWW::Mechanize goes bang
by Fletch (Bishop) on Mar 14, 2008 at 17:40 UTC

    It's die'ing because of an uncaught exception so, erm, perhaps catch it?

    Update: Additionally, perhaps check the frelling status of your get before blithely and unconditionally proceeding on as if everything were hunky-dory?

    If you've got a site that intermittently has hiccups something like this get wrapper might be useful:

    use constant MAX_FETCH_ATTEMPTS => 5; use constant RETRY_FETCH_DELAY => 2; sub _get_with_retry { my ( $m, $url ) = @_; my $tries = 0; my $result; until ( $tries == MAX_FETCH_ATTEMPTS() ) { $result = $m->get($url); last if $result->is_success; $tries++; sleep( RETRY_FETCH_DELAY() ); } return $result; }

    Update the second: Of course _get_with_retry would be kinda useless with autocheck enabled. Then again since you didn't bother to wrap your code in the correct tags I missed that it was set.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.