Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Best way to replace 'die' with 'warn'

by Marshall (Canon)
on Dec 06, 2009 at 04:50 UTC ( #811304=note: print w/replies, xml ) Need Help??


in reply to Best way to replace 'die' with 'warn'

I haven't used the Weather module. Perl has a "redo" which allows re-starting of a while loop() without re-evaluating the stuff within the while(...). This is very useful for doing "retries". For example, this POST will fail about one out of every 5,000 times on my machine. I've never seen this happen 2x in a row unless the web server is "hard down".

I think what you need is similar to this. Here I get a clear return value that says "It didn't work". Maybe you get something more indefinate like "I wanna die". If that is true, then use eval{} and plug the success or fail value into the "redo" code.

my $max_retry =3; RETRY: while (my $n_attempt=0, $parameter=<>) { my $req = POST 'http:/...blah..../', [ action => '/db/', param => "$parameter", type => 'submit', value => 'Search', ]; my $res = $ua->request($req); # idea is to concentrate the "it didn't work" code to # right here unless ($res->is_success) ##1 of 5,000 case## { $n_attempt++; print STDERR "attempt # $n_attempt for $parameter\n"; print STDERR "$parameter ERROR: Try# $n_attempt of $max_retry: " +. $res->status_line . "\n"; sleep(1); redo RETRY if $n_attempt <= $max_retry; print STDERR "$parameter ERROR: Try# $n_attempt of ". "$max_retry FAILED: ". $res->status_line . "\n"; next; #we "Give up" at this point! And read next #parameter from <> } ### this is the "good machine case..." print $res->as_string if DEBUG_RAW; ......or whatever.... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-10-20 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (210 votes). Check out past polls.

    Notices?