$SIG{ALRM} = \&Done; alarm $timeout; # Set your timeout here, in secs while () { print OUTPUT $_; # Do whatever } alarm 0; # So as not to trip ALRM after we're done with INPUT close(OUTPUT); # Likewise, in case we got an EOF in INPUT before $timeout close(INPUT); sub Done { close(OUTPUT); # Do your cleanup and optionally exit close(INPUT); exit 0; }