in reply to undefined value as filehandle from Curl.pm

Please show us the code leading up to and including where you're calling the LWP::Curl code that triggers this error.

As far as the open call, it says: open a lexical file handle ($fileb) in write mode (>) to the file name that's located in $content. In this case, $content isn't a file name, it's a regular scalar, and by passing the reference to it treats it as an in-memory file, instead of an on-disk one.

Replies are listed 'Best First'.
Re^2: undefined value as filehandle from Curl.pm
by wanderedinn (Sexton) on Jun 28, 2016 at 15:04 UTC
    use LWP::Curl; use Net::Curl::Easy qw(:constants); my $lwpcurl = LWP::Curl->new(); my $referer = 'http://server.com/:5555'; my $post_url = 'http://userid:password@server.com:5555/receive'; open(my $fh, "<", "test.xml"); $lwpcurl->{agent}->setopt( CURLOPT_READDATA, $fh ); $lwpcurl->{agent}->setopt( CURLOPT_HEADERDATA, ['Content-Type:text/xml +'] ); my $content = $lwpcurl->post($post_url, {}, $referer);

      First, this *is* your code, and you're not checking whether the file opened successfully for read. Second, why are you breaking encapsulation by modifying the innards of the object?

      Also, where are you getting the examples for calling setopt()? Everything I've found takes a reference, which you're not passing in.

      Here's an example from Net::Curl::Easy:

      $self->setopt( CURLOPT_FILE, \$self->{body} ); $self->setopt( CURLOPT_HEADERDATA, \$self->{head} );

      I don't see any direct use examples for CURLOPT_READDATA.

      I'm not in a position to test at the moment, so this is just a very wild guess, but first, ensure you handle opens correctly, then try the code as-is. If it still fails, try changing $lwpcurl->{agent}->setopt( CURLOPT_READDATA, $fh ); to $lwpcurl->{agent}->setopt( CURLOPT_FILE, $fh );. If that still doesn't work, try <c>$lwpcurl->{agent}->setopt( CURLOPT_FILE, \$fh );. All that said, I really, really don't think that modifying the agent like that was intended by the developers (I could be wrong though).

      Can you please post a link to where you're taking your examples from, or what you're basing your code on?

        The open() I referenced is directly from LWP/Curl.pm, thus it is not my code.

        As for the setopt references, I'll be honest, I could not find any examples. I did attempt passing the filehandle reference, but resulted in the same error.

        As for the setip() examples, I got them from previous posts here in a different thread

        regardless of either:

        $lwpcurl->{agent}->setopt( CURLOPT_FILE, \$fh ); $lwpcurl->{agent}->setopt( CURLOPT_FILE, $fh );

        both generate the same previous error:

        Can't use an undefined value as filehandle reference at /home/geof/httppost/lib/perl5/site_perl/5.8.8/LWP/Curl.pm line 236.