padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE:
Since English is my second language I think I did not express myself correctly, as webfiend put it what I am looking is that how to use Net::FTP::AutoReconnectinside my program, what structure like IF, WHILE, DO WHILE, I need to use to make the program reconnect, and how to catch the result when trying to connect to the server and fails, for example :
use Net::FTP; use strict; { my $pollingInterval = 1; my $directoryToMonitor = "C:/testdir"; my %oldFileList = (); my %currentFileList = (); # we start by getting the file list, considered to be the "old" list # getFileList($directoryToMonitor, \%oldFileList); for (;;) { sleep($pollingInterval); # get the current file list and compare it to the old file list # if the lists differ, the current file list becomes the old file +list # and we FTP the files # getFileList($directoryToMonitor, \%currentFileList); if ( fileListsDiffer(\%oldFileList, \%currentFileList) ) { copyFileList(\%oldFileList, \%currentFileList); #dumpFileList(\%oldFileList); # the FTP commands should go here instead of the dumpFileList() +function above my @transferFileList = glob "m*.*"; my @fileList = glob "m*.*"; foreach my $file (@fileList) { chomp($file); if ( $file =~ /.+\.\d{1,}/ ) { # one or more characters, a '.' foll +owed by one or more digits print "Iniciando transmision de datos...\n"; print STDOUT "<Nombre valido del Archivo>" . $file . "\n"; my $filesize = -s $file; print "Size: $filesize\n"; my $server = "124.32.54.59"; my $username = "saai"; my $pass = "saaisi0"; my $ftp; print "Connecting to $server..\n"; # Set up connection $ftp = Net::FTP->new( $server, Passive => 1, Debug => 0 ) o +r die $@; print "..authenticating..\n"; # Log in... $ftp->login( $username, $pass ) or die $ftp->message; print "..done!\n"; $ftp->cwd('Envio') or die $ftp->message; print "Directorio actual es :\n"; print $ftp->pwd (), "\n"; $ftp->ascii(); $ftp->put("$file") or die $ftp->message; my $tamano = $ftp->size($file); print "el tamano del archivo es : $tamano\n"; print "Comprobando el envio de los archivos\n"; if ($filesize = $tamano) { print "\nSON DEL MISMO TAMANO\n"; } else { print "ooooopppssss!!!! intentando enviarlo de nuevo"; } print "Logging out.."; #or die $ftp->message; $ftp->quit; print "..done!\n\n\n"; } else { print STDOUT "<Nombre Invalido del Archivo> " . $file . "\n\n\n"; } } for (@transferFileList) { #print "$_\n"; system ("del",$_); } } } } # Basically prints the contents of a hash. # sub dumpFileList { my $list = shift; my @k = sort(keys(%$list)); for (my $i=0; $i<=$#k; $i++) { print $k[$i] . " " . $list->{$k[$i]} . "\n"; } print "\n"; } # Get a list of the files along with there modification times # from the specified directory. The results are place in the # specified hash where the key is the file name and the value # is that file's modification time. # sub getFileList { my $directory = shift; my $hash = shift; %$hash = (); opendir(DIR,$directory) || die "Can not open $directory"; my @files = grep(!/^\.\.?$/, readdir(DIR)); closedir(DIR); for (my $i=0; $i<=$#files; $i++) { $hash->{$files[$i]} = (stat("$directory/$files[$i]"))[9]; } } # See if two hashes of files differ. The hashes are considered differ +ent if one of the # following occurrs: # # 1. The hashes have different number of elements. # 2. The new hash is missing a key that is in the old hash. # 3. For a given key, the value in the old hash differs from the val +ue in the new hash. # sub fileListsDiffer { my $oldList = shift; my $newList = shift; my @oldKeys = (keys(%$oldList)); my @newKeys = (keys(%$newList)); if ($#newKeys != $#oldKeys) { return 1; } foreach my $key (@oldKeys) { if ( ! defined($newList->{$key}) || $newList->{$key} ne $oldList-> +{$key} ) { return 1; } } return 0; } # Copies one hash to another. The destination has ($to below) is empt +ied first. # sub copyFileList { my $to = shift; my $from = shift; %$to = (); foreach my $key (keys(%$from)) { $to->{$key} = $from->{$key}; } }
Where in this program I can use reconnect or how to use it? thanks!
_____________________________________________________________ Dear monks!!!
I come to you dear monks in seek of knowledge, I need in my program to have a reconnect feature in case of the FTP fails it try to reconnect again but I dont know how to implement it on my program can someone please help me ?
thank you dear monks

Replies are listed 'Best First'.
Re: how to implement Net::FTP::AutoReconnect
by webfiend (Vicar) on Apr 21, 2008 at 20:03 UTC

    Maybe it's a silly question, but wouldn't you just use Net::FTP::AutoReconnect to accomplish that? If for some strange reason you want to implement the functionality yourself rather than using the library, then I guess your best bet would be to look at the library source for ideas.

Re: how to implement Net::FTP::AutoReconnect
by samtregar (Abbot) on Apr 21, 2008 at 20:06 UTC
    Shouldn't be too hard. Off the top of my head:

    1. Trap and record state info as the client code uses Net::FTP. Stuff you'll definitely need: login info, path changes.
    2. Trap calls that could fail due a lost connection, which is probably just about all of them. Catch the error, then...
    3. Reconnect and replay the stored state info to restart the transaction.

    One obvious problem is that some things are not re-playable. You can't re-delete a file successfully, or re-create a directory. But for the common use-case of uploading/downloading a file it should work.

    UPDATE: Haha, as webfiend pointed out the module already exists! In that case the answer is easier - you shouldn't implement it since it's already implemented! You should just use it.

    -sam

Re: how to implement Net::FTP::AutoReconnect
by apl (Monsignor) on Apr 21, 2008 at 20:03 UTC
    Test the return statuses of your new, login and get/put. If any fail, do a quit, and repeat your FTP attempt.

    This lends itself nicely to you imnplementing a routine to do your FTP, returning an indication of success or failure.

Re: how to implement Net::FTP::AutoReconnect
by sgifford (Prior) on Apr 22, 2008 at 04:56 UTC
    padawan_linuxero -

    You simply need to change use Net::FTP to use Net::FTP::AutoReconnect and $ftp = Net::FTP->new to $ftp = Net::FTP::AutoReconnect->new. At least, that's how I intended the module to work; if you have trouble post here and I'll see if I can help.

    Adios, y buena suerte!