kiboro has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to rewrite part of our system to use WWW:Curl instead of a home-grown socket server.

I've got the basics working and can get a response with both get and post vars. However, I get the response in a variable and the rest of the code on the system is expecting a file/socket handle to be returned to read line by line. I'm not really a perl programmer so I can't see a way to do this. Can anyone help?

The relevant code is:
my $response_body; open (my $fileb, ">", \$response_body); $curl->setopt(CURLOPT_WRITEDATA,$fileb);
Which let's me return the response with return $response_body, but I'd rather return a filehandle.

Replies are listed 'Best First'.
Re: Can I return a filehandle from Curl?
by derby (Abbot) on Apr 21, 2009 at 11:07 UTC

    As the AM pointed out, $fileb is a filehandle ... one that's only opened for writing but still, a filehandle. Maybe you need the filehandle to be open for reading and writing? In that case, just do so. I always find it useful to take the code in the docs and modify them to meet my conditions *before* I try to jam the new code into a legacy system:

    ... my $response_body; # # open the file for reading and writing # open( my $fileb, "+>", \$response_body ); $curl->setopt( CURLOPT_WRITEDATA, $fileb ); ... # rewind the file handle seek $fileb, 0, 0; while( <$fileb> ) { print $_; }

    -derby
Re: Can I return a filehandle from Curl?
by Anonymous Monk on Apr 21, 2009 at 10:36 UTC
    but I'd rather return a filehandle.
    Do you know what is filehandle? It is $fileb. 'perldoc -f open'