#! perl -slw use strict; use threads stack_size => 4096; use threads::shared; sub timedCommand { my( $timeout, $cmd, @args ) = @_; my $pid :shared; my @input :shared; async { # local $_; ### LINE C ### $pid = open my $fh, "$cmd @args |" or die "$!, $^E"; push @input, $_ while <$fh>; }->detach; sleep 1 until $pid; sleep 1 while kill 0, $pid and $timeout--; kill 3, $pid if $timeout; print "@input"; ### LINE A ### return @input; } my $cmd = q[ perl -E"$|++; sleep(1), say for 1 .. $ARGV[0]" ]; print timedCommand( 3, $cmd, $_ ) for 2, 3, 4; ### LINE B ### __END__ C:\test>\perl64-16\bin\perl.exe timedCommand.pl Thread 1 terminated abnormally: Modification of a read-only value attempted at timedCommand.pl line 15. Thread 2 terminated abnormally: Modification of a read-only value attempted at timedCommand.pl line 15.