use IO::Select qw( ); # I like big blocks and and I can not lie. # Benchmarking might just deny. use constant BLK_SIZE => 64*1024; my $sel = IO::Select->new($fh); my $buf = ''; while ($sel->can_read()) { my $rv = sysread($fh, $buf, BLK_SIZE, length($buf)); if (!defined($rv)) { ... Handle error. Don't forget $buf might not be empty. ... $sel->remove($fh); } if (!$rv) { ... Handle EOF. Don't forget $buf might not be empty. ... $sel->remove($fh); } while ($buf =~ s/^(.*)\n//) { my $msg = $1; my $close = process_msg($msg); $sel->remove($fh) if $close; } }