#!/usr/bin/perl -w use strict; use constant MAX_RETRY => 2; my $ua = LWP::UserAgent->new; my $raw_html; my $someParam; RETRY: while (my $n_attempt=0, $someParam=) { #...some clean up of $someParam my $req = POST 'http://www.someURL/db/', [ action => '/db/', dbparam => "$someParam", type => 'submit', value => 'Search', ]; my $res = $ua->request($req); unless ($res->is_success) ## or perhaps: if ( !$res->is_success ) { $n_attempt++; print STDERR "$someParam ERROR: Try# $n_attempt" . $res->status_line . "\n"; sleep(1); redo RETRY if $n_attempt <= MAX_RETRY; print "$someParam,ERROR: Try# $n_attempt" . $res->status_line . "\n"; next; #Maybe you something different here???? } $raw_html = $res->as_string; #...do some normal processing... #then loops to next value of $someParam... }