Creator
Creator

Reputation: 21

How to capture STDOUT from Perl open pipe?

How would I capture the STDOUT return from wget being called by Perl in the following way and put it into a variable?

my @urls = ('url1', 'url2', 'url3');

open(GET, "| xargs -n1 -P 3 wget -qO- ") || die "get failed: $!\n";
print GET "@urls";

Upvotes: 2

Views: 1508

Answers (2)

ephemient
ephemient

Reputation: 204778

If you want to slurp all the data at once, IPC::Run can do that.

use IPC::Run qw(run);
run [qw(xargs -n1 -P 3 wget -qO-)], \"@urls", \my $out;
print "$out";

If you want to process data as it is made available, IPC::Run can do that too.

use IPC::Run qw(run);
run [qw(xargs -n1 -P 3 wget -qO-)], \"@urls", sub {
    print $_[0];
};

Upvotes: 2

user554546
user554546

Reputation:

First of all, your pipe is an input pipe, and open doesn't support both input and output pipes. As an alternative, use a piped output and open one file handle per URL:

use strict;
use warnings;

my @urls=qw(url1 url2 url3);

foreach my $url(@urls)
{
  open(my $get,"-|","wget $url") or die $!;

  print while(<$get>);

  close($get);
}

Upvotes: 0

Related Questions