Problem #1: You have an "or" statement, not a subroutine block therefore a return(-1) is nonsense...return -1 to where and to whom?

I snipped out some code from one of my LWP programs. This will show you how to do a retry. In my application, a retry is required about 1/2,500 or so requests. Ok let's say that just one of 1,000 attempts fail...that's still a small number but if I am gonna run 10,000 requests, that is gonna happen on most of the runs and in this case a retry almost always works, so I don't want to "die". First some code, then some discussion:

#!/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=<IN>) { #...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... }
First set your loop up for the "good machine case" and get that working. Then think about the 1/1000 case that doesn't work. If the loop is gonna fail say 1/10th the time, then I would put the fail conditions in the loop iteration decision making. But if we are talking about a rare occurrence, I would put the processing of that in the "gut's"..not in the major loop condition! In your case ftp is gonna work! Well almost all of the time, given that you are authenticated, etc. But it can fail like 1/5000 times.

Perl has a cool thing called "redo". Read about it in the manual, but basically this causes the loop to go again without evaluating the, in this case while() statement again. Here I used a labeled redo, called RETRY although here this RETRY label is not strictly necessary. I just thought it "looked better" and was easier to understand.

Of course "next" operates just like what you would think..it loops back to and evaluates the loop condition again ("hey, I'm done with this one, I want a another one").

Anyway when considering things to do other than "give up" and die, think about what you would do...is a retry likely to succeed? If something really "blows up" are you willing to deal with the aftermath? If some guy comes along and cuts my internet cable, this program will spew 30-100K lines of BS into a log file. I thought about that and I'm ok with it.

In general your options are limited when "it didn't work". (a) try again, (b)skip this request and go the next request, (c) completely give up (die).

I hope I've helped you with some ideas.


In reply to Re: How do I use a block as an ‘or’ clause instead of a simple die? by Marshall
in thread How do I use a block as an ‘or’ clause instead of a simple die? by boom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.