不能使用未定义的值作为文件句柄引用

发布于 2024-10-07 09:46:17 字数 850 浏览 5 评论 0原文

首先,我搜索了论坛,但没有找到我的问题。 我正在运行安装了 perl 5.10 的 Ubuntu。

执行脚本后,我收到以下错误:

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

这是我的脚本....

#!/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

如果有任何帮助,我们将不胜感激。

谢谢,

first off I've searched the forums and didn't find exactly 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〗斷ホ乔殘χμё〖 2024-10-14 09:46:17

代替:

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

do:

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

如果您检查实际安装的 WWW-Curl 版本的文档,我想您会看到它传递文件句柄,而不是标量引用。

或者,升级 WWW-Curl。

另请注意,通常不建议使用 -W;通常,模块会禁用特定范围的警告,而大写的 W 开关可以防止这种情况发生。使用 -w 代替(或者只是 use warnings; 对于您自己的代码,您已经在这样做了)。

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).

远山浅 2024-10-14 09:46:17
#!/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

输出是:

Success 200
#!/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
亚希 2024-10-14 09:46:17

有错误的代码:

print ("Success ".$response_code);

查看 print 的文档:由于参数的方式当您使用括号进行解析时,第一个参数将被解释为文件句柄,这不是您想要的。在您的代码中,括号是不必要的;只需传递一个连接字符串,或者更好的是,避免连接并传递一个字符串列表:

print 'Success ', $response_code;

另外,请始终包含use strict;在您编写的每个脚本的顶部使用警告;。您会发现许多错误都被突出显示,否则这些错误可能会隐藏相当长一段时间,并且当您发现错误之前无需在 Stack Overflow 上询问,它还可以节省每个人的时间。 :)

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. :)

护你周全 2024-10-14 09:46:17
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

您已声明 $response_body,但尚未为其分配值。我认为如果你把它做成一个字符串,这会起作用。

my $response_body = "";

也就是说,我不能确定,因为我无法重现该错误。也许安装该模块的较新版本也会有帮助。

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.

烏雲後面有陽光 2024-10-14 09:46:17
        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);
        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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文