Thanks in advance.
Yehuda
P.S. I would like to avoid using fork()
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
...
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!).
> &{$cb}($buf,$len) if ref $cb eq 'CODE';
The following syntax is clearer, IMHO:
$cb->($buf, $len) if ref $cb eq 'CODE';
--Ala