my $command = 'dir'; my (@results) = _timedCommand($command, 30); if (@results) { print "Good:\n " . join(' ', @results); } else { print "Bad\n"; } sub _timedCommand { use threads; use threads::shared; my ($command, $time) = @_; my @results :shared; my $pid :shared; async { $pid = open my $fh, "$command |" or die "$+, $^E"; @results = <$fh>; $fh->close(); }->detach; kill 0, $pid while sleep 1 and $time--; kill 3, $pid and return if $time; return @results; }