> > > 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!

Thanks, I think.  :)  I'm sure my code is full of 'interesting' uses!

> 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.

Absolutely.

>  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.

In truth, I used LWP because I was already using it in another part of the
program, and because I thought the 'head' and 'getstore' functions would do
what I needed (so it was out of laziness more than anything).  Now that I
look at it, I agree Net::FTP looks like a more appropriate module than LWP.
It appears to be purely OO, though, and I prefer a more procedural interface
- do you know if Net::FTP supports that?

> 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.

All very good points.  They never occurred to me, so it's good to hear the
reasoning behind why one module might be better than another (given a certain
set of circumstances).  I find it easier to learn when I understand WHY I
should use A instead of B, instead of just reading something that says to use
A but fails to give an example or the logic behind it.

That said, would Net::FTP still be the preferred module if the file you're
downloading is NOT on an ftp server?  For example, I am downloading flat text
data files.  Some of them are on an ftp site (cue Net::FTP, stage right), but
some of them aren't.  Specifically, they have a http address and, when the
link to them is clicked, they are displayed as text instead of downloaded.
So would LWP be appropriate for those files, or would Net::FTP still be the
way to go?

Thanks!!

Bob



--
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