#!/usr/bin/env perl # Example code to show how reading from file descriptors fails after an alarm is triggered? use strict; use warnings; my $timeout = defined($ARGV[0]) ? $ARGV[0] : 2; my $sleep = 5; print "TIMEOUT=$timeout\n"; print "SLEEP=$sleep\n\n"; #MAIN, a #MAIN, b #MAIN, c sub sub1 { open(FD1, "$0") or die "ERROR: Could not open $0 : $!\n"; $. = defined($.) ? $. : 0; print "(sub1):begin_pos:$.\n"; while (){ next if ! /^#MAIN|^#DATA/; print $_; } eval { local $SIG{ALRM} = sub { die "alarm\n"; }; alarm $timeout; print "(sub1):sleeping...\n"; my $cmd = `sleep $sleep 2>&1;echo slept`; alarm 0; }; if ($@){ print "(sub1):timed_out!\n"; } else { # did not time out } print "(sub1):end_pos:$.\n\n"; close FD1 or die "$!\n"; } sub sub2 { open(FD2, "$0") or die "ERROR: Could not open $0 : $!\n"; print "(sub2):begin_pos:$.\n"; while (){ next if ! /^#MAIN|^#DATA/; print $_; } print "(sub2):end_pos:$.\n\n"; close FD2 or die "$!\n"; } &sub1; &sub2; &sub2;