#! /usr/bin/perl -w use strict; # Starting a co-process to read from. my $procid = open(READ, '-|', 'perl -e "$|=1; for ($i=0;$i<10;$i++) {print \"Line $i\n\"; sleep 1;}"'); my $timeout = 0; $SIG{ALRM} = sub { $timeout = 1; }; alarm(3); # Reading from co-process. while (!$timeout) { my $line = ; last unless defined $line; print $line; # for (my $i=0;$i<5000;$i++) {} } kill('INT', $procid) if $timeout;