for (1 .. API_ERRORS) { # If a timeout (ie. code 500) occurs, or # some kind of other error, repeat the API call $response = $self->mech->request($request); if ($response->is_success) { # sucessful processing code here... return $response_data; } # all non-success code cause pause, then re-try # elsif ($response->code == 500) { # next; #} sleep(1); }