Rodent of Unusual Size <[EMAIL PROTECTED]> writes:

> Gisle Aas wrote:
> > 
> > If you make the content into a sub then LWP will not override the
> > Content-Length value.
> > 
> >   $req = HTTP::Request->new(PUT => "http://....";);
> >   $req->content_length(33);
> >   $req->content(wrap("hi there"));
> > 
> >   sub wrap {
> >      my $data = shift;
> >      return sub {
> >         my $tmp = $data;
> >         $data = "";
> >         return $data;
                   ^^^^^
                    should have been $tmp

I should have tested my code before posting it :-(

> >      }
> >   }
> > 
> 
> I'm still trying to wrap my understanding around this.  I modified
> wrap() to:
> 
>     sub wrap {
>       my ($data) = @_;
>       return sub {
>           return $data;
>       }
>     }
> 
> and the data string seems to get sent not once, but over and over and..

I would expect so.

An LWP driver module (in this case the HTTP module) will open the
connection to the server, send the request line and the headers and
then call the content code and set what it returns until it returns an
empty string (signaling EOF).  You provided a version of the code that
never returns an empty string (and so did I).

The purpose of the $data = "" statement above was to make sure that an
empty string was returned the next time the function was called.

Try this:

------------------------------------------------
sub wrap {
    my $data = shift;
    return sub {
        my $tmp = $data;
        $data = "";
        return $tmp;
    }
}

my $f = wrap("hi there");

for (1..4) {
    print &$f, "\n";
}
------------------------------------------------

Regards,
Gisle

Reply via email to