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); #### binmode($fh_in); # Or open with :raw binmode($fh_out); # Or open with :raw while (1) { # Or this could be a select loop. my $rv = sysread($fh, my $buf, BLOCK_SIZE, length($buf); die($!) if !defined($rv); last if !$rv; print($fh_out $buf); }