Ing. Branislav Gerzo wrote:
> Hi Perlers,
> 
> I have one nice question for you, I was run into common problem:
> need to repeat certain number of times something, until is succesful.
> For example - download webpage, FTP upload, connect to some host and
> so on.
> I ask you for some general (or it can't be possible - FTP upload)
> function, for example I will call:
> ftpupload($cfg, 10);
> will call ftpupload() with $cfg ($cfg->{user}, $cfg->{pass},...)
> and 10 is number of retries.
> 
> Could be anyone so nice and write it ? I have some snippets here,
> using eval {} and catch errors with calling recursive sub, but I don't
> think thats the best option.
> 
> Thanks in advance.
> 
> /brano
> 
> 

You haven't shown us what you have tried, or where it failed, only
suggested something about eval and recursive subs, which neither of
which should be pertinent here. Show us your attempts...

In any case...

I would write this such that the number of retries is part of your
configuration, assuming you really do want the ftpupload sub to handle
keeping track of that, for now I will assume you do.

In pseudo code this might looking something like...

use Net::FTP;
my $cfg = { 'user' => 'username',
            'pass' => 'password',
            'host' => 'host.com',
            'retries' => 10,
            'filename' => '/path/to/file',
            'dir'      => '/remote_dir',
          };

ftp_upload($cfg);

sub ftp_upload {
  my ($config) = @_;

  # error handling on argument list

  $config->{'retries'} ||= 3;

  my $try = 0;
  while ($try < $config->{'retries'}) {
     # connect
     # change directory
     # upload file
     # disconnect

     if (success) {
        return;
     }
  }
  return "Failed after $config->{'retries'} attempt(s)";
}

I assume you can read the docs for Net::FTP to handle the actual FTP
code, they are fairly complete and straight forward. It is also a core
module, and the *best* way to handle FTP transfers.

Good luck,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to