簡體   English   中英

Perl檢查要獲取的URL中是否存在文件

[英]Perl check if file exists in url to be fetched

在網址數據庫上執行file :: fetch時,某些網址僅包含頂級域名,例如http://google.com 我該如何構造我的if檢查,以便它將驗證是否有文件要提取,因此當我創建新的file :: fetch對象時,它不會拋出此錯誤:

Use of uninitialized value $path in pattern match (m//) at C:/Perl/lib/File/Spec/Unix.pm line 267.
Use of uninitialized value in string eq at C:/Perl/lib/File/Fetch.pm line 395.

這是我的代碼片段,我正在嘗試進行一些驗證

my $uri_handle = File::Fetch->new(uri => $url);
my $getfile = $uri_handle ->file;
if ($getfile){
    my $dir_handle = $uri_handle->fetch( to => $dir ) or die "Couldn't fetch file: $uri_handle->error\n";
    #print "[ID:$myid] File fetch completed!\n\n";
}else{
    print "[ID:$myid] There is no file to be fetched from $url\n\n";
}   

但是file方法也失敗了,因為它無法在URL上檢索文件名。

Thread 1 terminated abnormally: Can't call method "file" on an undefined value at C:\test\multihashtest2.pl line 102.

在嘗試加載URI之前,您的perl腳本將不知道Web服務器上的內容,什么也不知道-URL的外觀無關緊要。 您將需要嘗試獲取文件並妥善處理所有錯誤。

如果發布代碼會有所幫助,但是您可以通過查看文檔來回答自己的問題。

特別是, new()方法returns false on error ,但這只是在實際獲取URI之前對其進行驗證。 在File :: Fetch對象上調用fetch()時,如果Returns the full path to the downloaded file on success, and false on failure.它將Returns the full path to the downloaded file on success, and false on failure. ,則Returns the full path to the downloaded file on success, and false on failure.

因此,請處理這些方法相應返回的內容。

編輯:現在您發布代碼。 如果錯誤提示未定義$ uri_handle,請檢查是否正確設置了$ url並檢查錯誤。 在第1行之后:

print $url;
print $uri_handle->error;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM