2010-12-13 23 views
1

首先我已經搜索論壇,並沒有發現確切的我的問題。 我使用安裝了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 

這裏任何幫助將非常感激。

感謝,

+0

你在哪裏收到錯誤? – KevinDTimm 2010-12-13 20:05:25

+0

@Kevin at scraper.pl line 15.;) – bzlm 2010-12-13 20:07:02

+0

對不起......錯誤發生在第15行... my $ return_code = $ curl-> perform; – Bnjmn 2010-12-13 20:07:05

回答

6

在地方:

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

做:

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

如果你檢查你實際上已經安裝了WWW捲曲的版本的文檔,我想你會看到它通過一個文件句柄,而不是標量引用。

或者,升級WWW-Curl。

另請注意,-W通常不可取;通常模塊會禁用特定範圍的警告,並且大寫W開關會阻止該警告。使用-w代替(或僅爲use warnings;爲您自己的代碼,你已經在做)。

+0

只是問......當我指定解決問題的人仍然不受歡迎時,爲什麼社區選擇支持關於解決方案的評論? – Bnjmn 2010-12-16 01:56:31

+0

@Bnjmn:有人猜測,因爲另一個人在閱讀錯誤的文檔而不是解釋你做了什麼之後向你提供了一個解決方法? – ysth 2010-12-16 01:59:10

+0

此外,使用curl將某些東西放入臨時文件中有點奇怪。 – ysth 2010-12-16 01:59:46

0

有惡意代碼在:

print ("Success ".$response_code); 

看那文檔print:由於當您使用括號參數解析的方式,第一個參數將被解釋爲文件句柄,即而不是你的意圖。在你的代碼中,括號是不必要的;只是傳遞一個連接字符串,或更好,避免串聯,並通過字符串列表:

print 'Success ', $response_code; 

另外,請始終包括use strict; use warnings;頂部每隔腳本編寫。你會發現許多錯誤被突出顯示,否則這些錯誤可能會在相當長一段時間內隱藏起來,並且在你必須詢問堆棧溢出之前捕獲錯誤時,它也可以節省每個人的時間。 :)

+0

雖然這是在行20。 – Quentin 2010-12-13 20:12:08

+0

感謝您的輸入...一個真正的業餘的錯誤:( – Bnjmn 2010-12-13 20:12:18

+0

@Bnjmn:如果該行永遠不會被執行:p – Ether 2010-12-13 20:13:06

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

您已經聲明$response_body,但還沒有賦值給它。我認爲如果你將它設爲一個字符串,這將起作用。

my $response_body = ""; 

這就是說,我不能確定,因爲我不能重現錯誤。也許安裝更新版本的模塊也會有幫助。

+0

感謝你的迴應,我知道perl在變量初始化方面特別慷慨,所以你提出的修復方案並沒有解決問題,我也下載了最新版本的Curl模塊,並確保我的perl解釋器也是相當新近的,因爲論壇中的以前的帖子報告了舊版本的perl 快速更新,當我將$ response_body更改爲空字符串時,解釋器錯誤更改並顯示「Bad filehandle:在scraper.pl 15行「 – Bnjmn 2010-12-13 20:31:00

+0

從這個評論中,$ curl-> setopt是否有可能期望一個文件句柄作爲它的第二個參數,而不是對標量的引用?如果你不介意,試試這個: 打開我的$ fh,'>',\ $ response_body或者死掉「$!」; $ curl-> setopt(CURLOPT_WRITEDATA,$ fh); 編輯:文檔暗示這應該無關緊要,但。 – Hugmeir 2010-12-13 22:09:21

+0

@Hugmeir:但他正在檢查最新的文檔,而不是他版本的文檔。 4.11至少需要一個文件句柄,而不是一個標量文件。 – ysth 2010-12-14 08:34:10

2
#!/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
+0

謝謝d5e5,你已經解決了這個問題。非常感謝您的幫助。 – Bnjmn 2010-12-14 00:40:09

+0

本帖已被降級爲建議的解決方案,因爲它代表了一個有點尷尬的解決方法,但它仍然提供了預期的結果,因此我投了贊成票。 – Bnjmn 2010-12-16 02:05:34

+0

謝謝Bnjmn。在我發佈上述內容之後,我看到ysth在4分鐘之前發佈了一個答案,我投了贊成票,因爲它包含了一個解釋。此外,我還沒有看到Curl.pm文件與WWW :: Curl :: Easy一起安裝,從包含Easy.pm的Curl文件夾上升一級。它包含使用各種方法的示例,包括執行。 – d5e5 2010-12-16 20:28:02

0
 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);