in reply to Re: Re: Reducing LWP Buffering
in thread Reducing LWP Buffering

Having never tried that, I'm wondering what the code might look like. Would you need to do something like:

open (READER, "$file") or die "Can't read: $!\n"; my $req = HTTP::Request->new('PUT', "$url", undef, \&read_file_in_chun +ks(\*READER)); sub read_file_in_chunks { my ($fh_ref) = @_; my $content; read ($fh_ref, $content, 51200); return $content; }

Hmmm..actually, I just tried that and it seems only the first 51200 bytes get uploaded. What did I miss?

Replies are listed 'Best First'.
Re: Re: Re: Re: Reducing LWP Buffering
by Corion (Patriarch) on Dec 05, 2003 at 14:08 UTC

    You can't pass parameters to a callback, so you have to either pass a closure or do some other magic. I haven't tried anything like this recently, and thus I write simply untested code from the top of my head:

    use strict; use LWP::UserAgent; my $url = 'http://www.example.com/'; my $filename = 'test.file'; open CONTENT, "<", $filename or die "Couldn't open $filename : $!"; binmode CONTENT; my $callback = sub { my $content; my $size = read( CONTENT, $content, 51200 ); $content = "" unless $size; $content; }; my $ua = LWP::UserAgent->new(); $ua->post($url,$callback);

    For anything fancier, I strongly suggest you learn about anonymous code references and closures.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      Ahh...I see. Very helpful, thanks Corion. Hope this helps Anonymous too.
Re: Re: Re: Re: Reducing LWP Buffering
by Anonymous Monk on Dec 05, 2003 at 14:29 UTC
    Did you try the :content_file option?