不能使用未定义的值作为文件句柄引用
首先,我搜索了论坛,但没有找到我的问题。 我正在运行安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
代替:
do:
如果您检查实际安装的 WWW-Curl 版本的文档,我想您会看到它传递文件句柄,而不是标量引用。
或者,升级 WWW-Curl。
另请注意,通常不建议使用 -W;通常,模块会禁用特定范围的警告,而大写的 W 开关可以防止这种情况发生。使用 -w 代替(或者只是
use warnings;
对于您自己的代码,您已经在这样做了)。In place of:
do:
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).输出是:
Output is:
有错误的代码:
查看 print 的文档:由于参数的方式当您使用括号进行解析时,第一个参数将被解释为文件句柄,这不是您想要的。在您的代码中,括号是不必要的;只需传递一个连接字符串,或者更好的是,避免连接并传递一个字符串列表:
另外,请请始终包含
use strict;在您编写的每个脚本的顶部使用警告;
。您会发现许多错误都被突出显示,否则这些错误可能会隐藏相当长一段时间,并且当您发现错误之前无需在 Stack Overflow 上询问,它还可以节省每个人的时间。 :)There is bad code at:
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:
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. :)您已声明
$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.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.