Callback during FTP

Discussion in 'Perl Misc' started by Yehuda Berlinger, Feb 3, 2004.

  1. 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()
    Yehuda Berlinger, Feb 3, 2004
    #1
    1. Advertising

  2. Yehuda Berlinger

    Ben Morrow Guest

    (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.
    Ben Morrow, Feb 3, 2004
    #2
    1. Advertising

  3. Ben Morrow <> wrote in message news:<bvp9th$g71$>...

    ....


    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
    Yehuda Berlinger, Feb 6, 2004
    #3
  4. Yehuda Berlinger

    Steve Lidie Guest

    In comp.lang.perl.tk Yehuda Berlinger <> wrote:
    > Ben Morrow <> wrote in message news:<bvp9th$g71$>...
    >
    > ...
    >
    >
    > 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!).
    Steve Lidie, Feb 6, 2004
    #4
  5. Yehuda Berlinger

    Ala Qumsieh Guest

    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
    Ala Qumsieh, Feb 7, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sosh
    Replies:
    4
    Views:
    2,850
  2. Replies:
    2
    Views:
    4,790
  3. William Gill

    prevent callback during initialization

    William Gill, Aug 25, 2005, in forum: Python
    Replies:
    0
    Views:
    244
    William Gill
    Aug 25, 2005
  4. =?Utf-8?B?U3JpZGhhcg==?=

    Hide/Show Controls during a callback

    =?Utf-8?B?U3JpZGhhcg==?=, Apr 11, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    484
    =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=
    Apr 24, 2007
  5. D. Buck
    Replies:
    2
    Views:
    458
    D. Buck
    Jun 29, 2004
Loading...

Share This Page