com
com

Reputation: 79

How to store the output of wget into a variable

With the backticks, the system call just displays the wget data to the screen.

What I would like to do is have the information from wget "piped" into a string or an array rather than the screen.

Below is a snippet of my code.

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

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

Upvotes: 2

Views: 5779

Answers (2)

slayedbylucifer
slayedbylucifer

Reputation: 23532

You can run any OS command (I am referring Linux Only) and capture the output/error returned by the command as below:

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

EDIT after reading OP's comment:

Any way to not have the wget information print to stdout?

Below code will download the file without posting anything to screen:

#!/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>)
{
    ;
}

Refer perlipc for more information.

Upvotes: 5

perreal
perreal

Reputation: 98118

With a pipe-open:

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

to check for the connected string:

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

Upvotes: 3

Related Questions