binmode($fh); # Or open with :raw my $buf = ''; while (1) { # Or this could be a select loop. my $rv = sysread($fh, $buf, BLOCK_SIZE, length($buf); die($!) if !defined($rv); last if !$rv; # Identify and extract message. # Using LF-terminated messages in this example. while (s/^([^\n]*)\n//) { my $msg = $1; process_message($msg); } } die("Premature EOF") if length($buf);