Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Callback during FTP

15 views
Skip to first unread message

Yehuda Berlinger

unread,
Feb 3, 2004, 2:35:54 PM2/3/04
to
I would like to do a callback while transferring via FTP, so as to
print % finished, etc... (and to activate my Tk::Progressbar).
Net::FTP doesn't seem to support this. Is that correct? Is there any
other tool that can do this? Is there anyway to do this using the
'hash' mechanism?

Thanks in advance.

Yehuda

P.S. I would like to avoid using fork()

Ben Morrow

unread,
Feb 3, 2004, 6:12:49 PM2/3/04
to

j...@actcom.co.il (Yehuda Berlinger) wrote:
> I would like to do a callback while transferring via FTP, so as to
> print % finished, etc... (and to activate my Tk::Progressbar).
> Net::FTP doesn't seem to support this. Is that correct? Is there any
> other tool that can do this? Is there anyway to do this using the
> 'hash' mechanism?

If you're downloading you could try using LW,P which I think does
support this kind of callback. Otherwise... I think the only way to do
it is with a tied filehandle. Something like (completely untested):

package My::App::FTPCallback;

use Symbol;

sub new {
my ($c, $cback) = @_;
my $H = *{Symbol::gensym};
tie $H, $c, $cback;
return $H;
}

sub TIEHANDLE {
my ($c, $cback) = @_;
return bless {
count => 0,
cback => $cback,
}, $c;
}

sub PRINT {
my $s = shift;
my $hashes = join '', @_;
$s->{count} += length $hashes;
$s->{cback}->($s->{count});
return 1;
}

package main;

my $HASH = My::App::FTPCallback->new(sub {
my $count = shift;
# do stuff here
});

$FTP->hash($HASH, 1024);

# the callback will now be called for every KB transferred, with its
# first argument the number of KB transferred so far.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp. [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live. b...@morrow.me.uk

Yehuda Berlinger

unread,
Feb 6, 2004, 2:57:16 AM2/6/04
to
Ben Morrow <use...@morrow.me.uk> wrote in message news:<bvp9th$g71$1...@wisteria.csv.warwick.ac.uk>...

...


Cool.

Actually, I decided to subclass Net::FTP, copying over the get and put
functions, and adding one parameter $cb, and one line in the
appropriate place:

&{$cb}($buf,$len) if ref $cb eq 'CODE';

For the life of me, I can't figure out why this is not in Net::FTP, as
it is trivial.

Yehuda

Steve Lidie

unread,
Feb 6, 2004, 9:14:20 AM2/6/04
to

I think Ben's trick is really cool. And, generally, your method, which
I've used in the past, is great most of the time. Still, when doing
network I/O with a Tk GUI I prefer a mulit-process approach.
The child handles network activities and talks to the Tk parent via
fileselect, using pipes or sockets (or even memory mapped arrays!).

Ala Qumsieh

unread,
Feb 7, 2004, 12:11:17 PM2/7/04
to
Yehuda Berlinger wrote:

> &{$cb}($buf,$len) if ref $cb eq 'CODE';

The following syntax is clearer, IMHO:

$cb->($buf, $len) if ref $cb eq 'CODE';

--Ala

0 new messages