#! perl -slw use strict; use threads; use threads::shared; my @output :shared; my $pid :shared; my $t = async { $pid = open CMD, qq[ perl -le"\$|++; print ~~localtime while sleep 1" |] or die $!; while( ) { chomp; lock @output; push @output, $_; } close CMD; }; sleep 10; ## do other stuff for my $out ( do{ lock @output; @output } ) { if( $out =~ m[6] ) { print $out; } } kill 9, $pid; $t->join;