my $done = 0; local $SIG{ALRM} = sub { $done = 1; }; alarm 10; # or whatever while (not $done and $output = ) { print $output; # don't need newline as it's there already }