Here's a minimal example using piped open (instead of manually creating the pipes), and IO::Select as a more convenient wrapper around the select builtin:
use IO::Select;
sub spawn_ping {
my $addr = shift;
open my $fh, "-|", "ping $addr -w 2 -q | sed -n '\$p'" or die $!;
return $fh;
}
my $sel = new IO::Select();
for (1..10) {
my $addr = 'localhost'; # or whatever
$sel->add( spawn_ping($addr) );
}
while ( my @ready = $sel->can_read ) {
for my $fh (@ready) {
my $resp = <$fh>;
if (defined $resp) {
print STDERR $resp; # or do with it whatever you like
} else {
$sel->remove($fh);
close $fh;
}
}
}
Note that the <$fh> would (temporarily) block until a whole line is available on the respective pipe. This shouldn't be a problem in this particular case, though, as you're outputting entire lines.
|