如何将wget的输出存储到变量中

时间:2013-05-14 05:54:42

标签: perl wget

使用反引号,系统调用只会将wget数据显示在屏幕上。

我想要做的是获取来自wget"管道的信息"到字符串或数组而不是屏幕。

以下是我的代码片段。

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

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

2 个答案:

答案 0 :(得分:4)

您可以运行任何OS命令(我只是指Linux)并捕获命令返回的输出/错误,如下所示:

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

编辑

任何方法都没有将wget信息打印到stdout?

下面的代码将下载文件而不向屏幕发布任何内容:

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

答案 1 :(得分: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;