#! perl -slw use strict; sub timedCommand { use threads; use threads::shared; my( $cmd, $timeout ) = @_; my @results :shared; my $pid :shared; async { $pid = open my $fh, "$cmd |" or die "$!, $^E"; @results = <$fh>; }->detach; kill 0, $pid while sleep 1 and $timeout--; kill 3, $pid and return if $timeout; return @results; } ... ## Run foo.exe, kill it after 20 seconds. my @results = timedCommand( 'foo.exe -opt', 20 ); ...