> > I have a perl script that FTPs a file down from another server > [snip] > > I am not sure HOW to do the file size check. > > I wrote the following using the LWP::Simple module: > > print "beginning download of ", ftp_file_size( $ftp_path_gz ), "\n"; > > $status = download_file( $ftp_path_gz, $local_path_gz ); > > sub ftp_file_size > { > my ( $url ) = @_; > > my @info = head( $url ); > > if( scalar @info == 0 ) > { > return 'unknown size'; > } > > return sprintf( "$info[1] bytes (%.2f MB)", $info[1]/(1024*1024) ); > } > > sub download_file > { > my ( $file_to_ftp, $save_filename ) = @_; > > my $status = getstore( $file_to_ftp, $save_filename ); > > if( $status == 200 ) > { > print "Download successful.\n"; > return 1; > } > else > { > print "An error occurred during the download.\n"; > return 0; > } > } > > > Do you want to do the file check locally or remotely? If local, > > > > perldoc -f stat > > (field 7) > > > > If remotely, Net::FTP provides the 'size' method. You definitely want > > to use Net::FTP for the FTP handling. > > Your response seemed pretty adamant about using Net::FTP. Would I be better > off using that module instead of LWP::Simple for this? If so, why? >
Interesting use of LWP, I wouldn't have thought of doing it that way, which certainly doesn't make it wrong! I probably should have stated specifically that I was being adamant *about using a module* for FTP transactions rather than doing it some other kludge of a way. Generally I would think the simplest module is Net::FTP, but since LWP provides functionality as long as it fits what you need to do then by all means... Having said that, personally I don't think it fits very well what you are attempting to do, since I have a hunch LWP was intended to make handling FTP urls found in web pages simple, rather than providing a true interface to FTP, which is Net::FTP's sole and intended purpose. Specifically in your case fetching the file size requires either parsing of a directory listing or HEAD request, and you had to write your own functions to do it, which means they have to be maintained/tested, etc. while Net::FTP provides the 'size' method for you. There are other reasons for preferring Net::FTP over LWP for this specific type of task, more specific return codes, already provided routines such as 'size', continuous connection (aka LWP implements FTP using HTTP like request, aka a single action at a time) while with Net::FTP you could handle several transactions over a single connection, for instance checking the size of the file, downloading it if it is over a certain size, and then moving the remote file to a new location. Depending on the size of your files or the number of actions needed, the time to establish connections could be the longest part of the process. Just some thoughts... 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>