2013-05-14 44 views
2

使用反引號,系統調用僅將wget數據顯示在屏幕上。如何將wget的輸出存儲到變量中

我想要做的就是將wget中的信息「管道化」爲字符串或數組而不是屏幕。

下面是我的代碼片段。

sub wgetFunct { 
    my $page = `wget -O - "$wgetVal"`; 

    while (<INPUT>) { 
     #line by line operations 
    } 
} 
+4

「LWP」有什麼問題? – squiguy 2013-05-14 05:57:27

+0

@squiguy:如果網站已連接,我正在查找信息。 wget在其可以解析的信息塊中返回單詞「connected」。 – com 2013-05-14 07:17:16

+1

相信與否:LWP確實有錯誤處理。 – innaM 2013-05-14 07:34:23

回答

4

您可以運行任何操作系統命令(我指的僅限Linux)和捕捉輸出/錯誤如下:

open (CMDOUT,"wget some_thing 2>&1 |"); 
while (my $line = <CMDOUT>) 
{ 
    ### do something with each line of hte command output/eror; 
} 

編輯看完OP的評論後:

任何方式沒有wget信息打印到標準輸出?

下面的代碼會下載文件,而無需繳納任何屏幕:

#!/usr/bin/perl -w 
use strict; 
open (CMDOUT,"wget ftp://ftp.redhat.com/pub/redhat/jpp/6.0.0/en/source/MD5SUM 2>&1 |"); 
while (my $line = <CMDOUT>) 
{ 
    ; 
} 

參考perlipc以獲取更多信息。

+0

任何方式沒有wget信息打印到標準輸出? – com 2013-05-14 14:02:55

+0

這很好,謝謝。 – com 2013-05-14 19:57:02

2

與管道開:

open my $input, "-|", "wget -O - $wgetVal 2>/dev/null"; 
while (<$input>) { 
    print "Line $_"; 
} 
close $input; 

檢查連接字符串:

open my $input, "-|", "wget -O - $wgetVal 2>&1"; 
while (<$input>) { 
    print "Good\n" and last if /Connecting to.*connected/; 
} 
close $input; 
+0

我看到它確實填充該變量,但有沒有辦法打印到屏幕上? – com 2013-05-14 07:19:04

+0

@com,更新了答案 – perreal 2013-05-14 07:24:11

+0

絕對是OP要求的,但也是一個非常噁心的方式來解決這個問題。 – innaM 2013-05-14 09:46:30

相關問題