[Chicago-talk] Net::FTP

Steven Lembark lembark at wrkhors.com
Tue Dec 23 14:21:35 CST 2003



-- "Dooley, Michael" <Dooley.Michael at con-way.com>

> is there a way to pull a unique filename from the Net::FTP package?
>
> something like $ftp->unique_name(); unfortunately this works on the server
> side not the client side or do I have to do something like below?
>
> my @local_files=glob("*"); ## assume you are in the proper directory
> if (grep $ftp_file eq $_, @local_files) {
> $ftp->get("$ftp_file",$ftp_file.$randnum);
> } else {
> $ftp->get("$ftp_file");
> }

File::Temp will give you the ability to load a file into
the local system, validate it, and then overwrite the existing
file if you like.

tempfile returns a file handle and path, and $ftp->get takes a
file handle (nice way to unzip things on the fly also):

	sub ftp_get
	{
		my $ftp = shift or die "Bogus ftp_get: missing ftp";
		my $remote = shift or die "Bogus ftp_get: missing remote";

		my $local = basename $remote;

		my( $fh, $path ) = tempfile( "$local-XXXX" );

		# double-check me that this returns zero...

		if( $ftp->get($remote, $fh) )
		{
			# blew it... assume caller uses exceptions.
			# see attached for boilerplate...
			
			die "Failed $remote: $errorstring";
		}
		else
		{
			rename $path, $remote or die "$path -> $remote: $!";
		}

		# no return, caller is happy enough just to be alive.
	}
	

The attached files are part of wormbase and use Schedule:Depend
to dispatch the operation. Wormbase::Download does the deed.
The $config argument is -- as you might guess -- a hash referent
w/ named parameters. This generates a queue of items to be downloaded
from the remote system and downloads the new files (i.e., ones not
found on the local system).

One cute trick is opening a gzip | cpio pipe in order to extract
tarballs on the fly as they are downloaded.





--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508
-------------- next part --------------
#!/usr/local/bin/perl -w

package Wormbase;

use strict;

use FindBin qw( $Bin );
BEGIN { use lib "$Bin" }

use Wormbase::Execute;

runsched ( 'download :', 'download' );

# this isn't a module

0

__END__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Download.pm
Type: application/x-perl
Size: 20669 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/chicago-talk/attachments/20031223/117adf85/Download.bin


More information about the Chicago-talk mailing list