Callback during FTP

  • Thread starter Yehuda Berlinger
  • Start date
Y

Yehuda Berlinger

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()
 
B

Ben Morrow

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
 
Y

Yehuda Berlinger

....


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
 
S

Steve Lidie

In comp.lang.perl.tk Yehuda Berlinger said:
...


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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top