#!/usr/bin/env perl -l use strict; use warnings; use Time::HiRes qw{ualarm time}; my $timeout = 1_000_000; #microseconds my $limit = 3; my @packets = (0 .. 11); print time; { my $local_limit = $limit; local $SIG{ALRM} = sub { print time; $local_limit = $limit; ualarm $timeout; }; ualarm $timeout; while (1) { next unless $local_limit-- > 0; last unless @packets; my $line = shift @packets; ualarm 1 if $line == 6; print $line; } } #### 1474530606.74812 0 1 2 1474530607.75322 3 4 5 1474530608.75744 6 1474530608.75752 7 8 9 1474530609.75927 10 11 #### last unless @packets; my $line = shift @packets; #### last if eof $fh; my $line = <$fh>; #### ualarm 1 if $line == 6;