#! perl -slw use strict; use threads qw[ yield async ]; use threads::shared; my( $cmd, $file ) = @ARGV; my $done : shared = 0; my @lines : shared; async { my $pid = open my $CMD, "$cmd |" or die "$cmd : $!"; open my $fh, '>', $file or die "$file : $!"; while( <$CMD> ) { chomp; print $fh $_; ## output to the file push @lines, $_; ## and push it to a shared array } $done = 1; }->detach; my $n = 0; while( !$done ) { if( @lines ) { ## lines to be processed print pop @lines; ## process them } else { ## Else nothing to do but wait. yield; } }