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

I am trying to understand why I am receiving the following 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

I have to admit, I don't understand what the code is attempting. Maybe someone can enlighten me? Code with line numbers below:

233 my $content = ""; 234 open( my $fileb, ">", \$content ); 235 $self->{agent}->setopt( CURLOPT_WRITEDATA, $fileb ); 236 $self->{retcode} = $self->{agent}->perform;

How is it that the open() above will even work? Can anyone explain to me why I an getting the 'undefined' error? Thanks for any assistance.

Replies are listed 'Best First'.
Re: undefined value as filehandle from Curl.pm
by stevieb (Canon) on Jun 28, 2016 at 14:02 UTC

    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.

      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?

Re: undefined value as filehandle from Curl.pm
by kennethk (Abbot) on Jun 28, 2016 at 14:45 UTC
    The error is because you are not testing your open.
    When opening a file, it's seldom a good idea to continue if the request failed, so open is frequently used with die. Even if die won't do what you want (say, in a CGI script, where you want to format a suitable error message (but there are modules that can help with that problem)) always check the return value from opening a file.
    Changing line 234 to
    open( my $fileb, ">", \$content ) or die "$!";
    should be informative.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      That's not the OP's code. That's code in the LWP::Curl module itself: LWP::Curl line 234.

      Also, that open should never technically fail, as the file ($content) is declared on the immediate line above. However, it should still be checked, even if out of habit alone, as perhaps corrupt/lack of memory or other oddness can still prevent the open.

        as perhaps corrupt/lack of memory or other oddness can still prevent the open.

        :) if it failed, wouldn't writing to the filehandle fail also?

      The open() you refer to is not in my code, it's in the perl module. I agree the success/failure of the open() should be tested.

        So, here is an oddity. If I remove the following line:

        $lwpcurl->{agent}->setopt( CURLOPT_HEADERDATA, ['Content-Type:text/xml +'] );

        The error:

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

        Goes away.

        Although, the http post still does not work properly. I'm not seeing my test file on the server. I have a curl command line test that works correctly, but can not seem to configure the perl code to do the same.