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

I read this post for capturing cURL's output: Cannot suppress Curl headers and I created a filehandle that was really just a scalar/variable. Whenever I call curl->perform, it adds to the variable instead of rewriting. What I really want is just a scalar to hold the response from cURL, but the post said to create a filehandle. Anyway, I want to store the response in a variable, not add it to a variable, because that messes my program up.
  • Comment on How can I open a filehandle so it is cleared and rewritten for every request?

Replies are listed 'Best First'.
Re: How can I open a filehandle so it is cleared and rewritten for every request?
by pc88mxer (Vicar) on Jun 27, 2008 at 18:51 UTC
    Just use a new file handle (or re-open your existing one.) You'll also have to re-configure your curl object every time you do this, so you might want to make it into a subroutine:
    use strict; use warnings; use WWW::Curl::Easy; my $curl = new WWW::Curl::Easy; $curl->setopt(CURLOPT_URL, 'http://cnn.com'); my ($r, $b, $h) = get_response($curl); print "retcode = $r\n"; print "body = $b\n"; sub get_response { my ($curl) = @_; my ($body, $head); open(my $body_fh, ">", \$body); open(my $head_fh, ">", \$head); $curl->setopt(CURLOPT_WRITEDATA, $body_fh); $curl->setopt(CURLOPT_HEADERDATA, $head_fh); my $retcode = $curl->perform; return ($retcode, $body, $head); }
      Yeah, that makes sense, but I didn't think of it. BTW I really don't think my questions was unclear and you answered it perfectly.

        It was "unclear" to the extent that you didn't show the code. Perl monks are generally Perl programmers and Perl programmers are encouraged to be "lazy". One way that manifests itself is expecting the OP to provide all required information to frame their question in a clear an concise way. Following a link is often too much work and may be dangerous - it could lead to Wikipedia and waste hours of time for example.

        More seriously: including a link is frowned on because links can be transient. It is much better to provide your sample code in the node because all the context for the question has the same life time - the life time of the node.


        Perl is environmentally friendly - it saves trees
Re: How can I open a filehandle so it is cleared and rewritten for every request?
by kyle (Abbot) on Jun 27, 2008 at 18:15 UTC
      I didn't post my code because it's exactly the same as the link I provided, but here it is:
      my $response_data; my $response_header; open(my $RD, ">", \$response_data); open(my $RH, ">", \$response_header); $curl->setopt(CURLOPT_WRITEDATA, $RD); $curl->setopt(CURLOPT_HEADERDATA, $RH); $curl->perform; print "Response header: $response_header\n"; print "Response body: $response_body\n";
      I want $response_data to be rewritten every time I run curl->perform, is there a way to do that?