[Israel.pm] Re: callbacks during FTP

Mikhael Goikhman migo at homemail.com
Thu Feb 5 04:55:47 PST 2004


On 05 Feb 2004 13:49:24 +0200, Yosef Meller wrote:
> 
> Yehuda Berlinger wrote:
> | For the life of me, I can't figure out why there is no call back in
> | Net::FTP's get and put functions.
> |
> | I created a subclass of Net::FTP, copied over the get and put
> | functions and added a callback argument and a single line:
> |
> | &{$cb}($buf,$len) if ref $cb eq 'CODE';
> |
> | to both functions and it works great.
> 
> I didn'r see the code, but instead of copying the entire sub, can't you
> call SUPER::sub with the relevant parameters and then execute your code?

I didn't see the code either, but it is obvious that additional parameter
(progress callback) should be called somewhere in the middle of some
inner loop, so overriding can't work here. You should owerwrite the whole
method.

Actually, thinking a bit more about this, you may try to do overriding.
I needed to actually take a look at `locate Net/FTP.pm`. It goes like this:

  * Create Net::FTP_WithProgress inheriting from Net::FTP, and define:

	sub get ($$$$$) {
		my ($ftp, $remote, $local, $where, $callback) = @_;

		$ftp->type('Binary_WithProgress');
		Net::FTP::Binary_WithProgress->setCallback($callback);

		$ftp->SUPER::get($ftp, $remote, $local, $where);
		# probably restore original $ftp->type here if needed
	}

  * Create Net::FTP::Binary_WithProgress inheriting from Net::FTP::I,
    it should implement 2 small methods, setCallback() to set the global
    callback and read(), somthing like:

	our $globalCallback = undef;

	sub setCallback ($$) { $globalCallback = $_[1]; }

	sub read ($@) {
		my $self = shift;
		my $len = $self->SUPER::read(@_);
		&$globalCallback($len, @_);
		return $len;
	}

Please note that I only looked at FTP::Net for several minutes and didn't
test this, but this seems to be all the code needed.

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list