use IO::Select qw( ); # How long (in seconds) of not getting anything is considered done. use constant TIMEOUT => 2.0; open(my $pipe, '-|', 'mDNS -L "Service Name" _service._tcp domain'); my $buf = ''; # Wait to get something. my $rv = sysread($pipe, $buf, 64*1024, length($buf)); die $! if !defined($rv); # Wait until we stop getting if ($rv) { my $sel = IO::Select->new($pipe); for (;;) { if (!$sel->can_read(TIMEOUT)) { kill KILL => $pid; last; } my $rv = sysread($pipe, $buf, 64*1024, length($buf)); die $! if !defined($rv); last if !$rv; } } close($pipe); print("Got:\n$buf\n");