mdc76 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use warnings; use Net::FTP; use Config; $Config{useithreads} or die('Recompile Perl with threads to run this p +rogram.'); use threads; my $file = 'c:\sdwork\dla495.txt'; my $ftp = Net::FTP->new("ftpserver") or die "can't get ftp object\n"; $ftp->login("fakeusr", "secret") or die "can't login to ftp server\n"; $ftp->binary(); $ftp->cwd('/home/fakeusr/tmp'); open (FH, "<$file") or die "Could not open source file. $!"; binmode(FH); my $i = 0; my $start_byte = 0; my @THRD_LIST; my $thrd; while (1) { my $chunk; print "process part $i\n"; $i ++; if (!eof(FH)) { my $bytes_read = read(FH, $chunk, 10000); #bytes print "bytes read: $bytes_read\n"; #test with threads $thrd = threads->create (\&transfer_chunks, $start_byte, $chunk) + or die "Failed to start the thread: $@\n"; push (@THRD_LIST, $thrd); #test without threads #transfer_chunks($start_byte, $chunk); $start_byte += $bytes_read + 1; print "start byte: $start_byte\n"; } last if eof(FH); } # start the threads for $thrd (@THRD_LIST) { $thrd->join(); } $ftp->quit; sub transfer_chunks { my ($start_byte, $chunk) = @_; # convert perl string into a filehandle open(TEST, '<', \$chunk); $ftp->restart($start_byte); $ftp->put(*TEST, 'testfile.txt'); ##$ftp->append(*TEST, 'testfile.txt'); close(TEST); }
|
|---|