开发者

Can't use an undefined value as a filehandle reference

first off I've searched the forums and didn't find exact开发者_StackOverflow社区ly my issue. I'm running Ubuntu with perl 5.10 installed.

I'm receiving the following error after executing my script:

 "Can't use an undefined value as filehandle reference at scraper.pl line 17"

Here is my script....

#!/usr/bin/perl -W
use strict;
use warnings;

use WWW::Curl::Easy;


my $curl = WWW::Curl::Easy->new;

$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_URL, 'http://something.com');


my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

my $return_code = $curl->perform;

if ($return_code == 0)
{
  my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
  print ("Success ".$response_code);
}
else
{
  # Error Code
  print ("An error occured: ".$return_code." ".$curl->strerror($return_code)." ".$curl->errbuf."\n");
}

# EOF

Any help here would be much appreciated.

Thanks,

Ben


In place of:

my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

do:

my $response_body = '';
open(my $fileb, ">", \$response_body);
$curl->setopt(CURLOPT_WRITEDATA,$fileb);

If you check the documentation for the version of WWW-Curl you actually have installed, I think you'll see it passes a filehandle, not a scalar reference.

Alternatively, upgrade WWW-Curl.

Also note that -W is not generally advisable; often modules will disable warnings for a particular scope and the capital W switch prevents that. Use -w instead (or just use warnings; for your own code, which you are already doing).


#!/usr/bin/perl
use strict;
use warnings;

use WWW::Curl::Easy;
use File::Temp qw/tempfile/;

my $response_body = tempfile();

my $curl = WWW::Curl::Easy->new;

$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_URL, 'http://yiddele.com/categoryindex.aspx');

#$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
$curl->setopt(CURLOPT_WRITEDATA, \$response_body);

my $return_code = $curl->perform;

if ($return_code == 0)
{
  my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
  print ("Success ".$response_code);
}
else
{
  # Error Code
  print ("An error occured: ".$return_code." ".$curl->strerror($return_code)." ".$curl->errbuf."\n");
}

# EOF

Output is:

Success 200


There is bad code at:

print ("Success ".$response_code);

Look at the documentation for print: due to the way arguments are parsed when you use parentheses, the first argument will be interpreted to be a filehandle, which is not what you intended. In your code, the parentheses are unnecessary; just pass a concatenated string, or better, avoid the concatenation and pass a list of strings:

print 'Success ', $response_code;

Also, please please always include use strict; use warnings; at the top of every script you write. You will discover that many errors are highlighted that may otherwise remain hidden for quite some time, and it also saves everyone's time when you catch an error before ever having to ask on Stack Overflow. :)


my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

You've declared $response_body, but haven't assigned a value to it. I assume that this would work if you made it a string.

my $response_body = "";

That said, I can't be sure as I can't reproduce the error. Perhaps installing a newer version of the module would help too.


        use Data::Printer ;
        use URI::Encode qw(uri_encode uri_decode);
        use JSON ();
        use JSON::Parse ':all' ;
        use WWW::Curl;
        use HTTP::Response ;

        use utf8 ;
        use Carp ;
        use Cwd qw ( abs_path ) ;
        use Getopt::Long;

         use WWW::Curl::Easy;

         my $curl = WWW::Curl::Easy->new;
         $curl->setopt(WWW::Curl::Easy::CURLOPT_HEADER(),1);
         $curl->setopt(WWW::Curl::Easy::CURLOPT_URL(), 'https://www.pivotaltracker.com/services/v5/me?fields=%3Adefault');
         $curl->setopt(WWW::Curl::Easy::CURLOPT_HTTPHEADER() , ['X-TrackerToken: ' . $TOKEN]  );
         #$curl->setopt(WWW::Curl::Easy::CURLOPT_POST(), 1);

         # A filehandle, reference to a scalar or reference to a typeglob can be used here.
         my $response_body;
         $curl->setopt(WWW::Curl::Easy::CURLOPT_WRITEDATA(),\$response_body);

         # Starts the actual request
         my $ret = $curl->perform;


         if ($ret == 0) {

              print("Transfer went ok\n");
              my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
              # judge result and next action based on $response_code

              $response_body = HTTP::Response->parse($response_body);
              print("Received response: $response_body\n");
              p($response_body);
              my $json_data = $response_body->content ;

              $json_data = JSON->new->utf8->decode($json_data);
              p($json_data);

         } else {
             # Error code, type of error, error message
              print("An error happened: $ret ".$curl->strerror($ret)." ".$curl->errbuf."\n");
         }

        # my $cmd='curl -X GET -H "X-TrackerToken: ' . "$TOKEN" . '" "https://www.pivotaltracker.com/services/v5/me?fields=%3Adefault"' ;
        # my $json_str = `$cmd`;
        # p($json_str);
        # my $json_data = JSON->new->utf8->decode($json_str);
        # p($json_data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜