my $BLOCK_SIZE = 64 * 1024; # Until EOF, for (;;) { # Read a chunk. my $rv = read($fh, my $buf='', $BLOCK_SIZE); die if !defined($rv); # I/O error. last if !$rv; # EOF # For each message in the buffer, for (;;) { my $pos = index($buf, "\0\0Msrc"); last if $pos < 0; # Discard the uninteresting bits. substr($buf, 0, $pos, ''); # Read the header. while (length($buf) < 22) { my $rv = read($fh, $buf, $BLOCK_SIZE, length($buf)); die if !defined($rv); # I/O error. die if !$rv; # Incomplete message } # Discard the header. substr($buf, 0, 22, ''); # Locate the end of the message. my $pos = 0; for (;;) { $pos = index($buf, "\0", $pos); last if $pos >= 0; $pos = length($buf); # Read a chunk. my $rv = read($fh, $buf, $BLOCK_SIZE, length($buf)); die if !defined($rv); # I/O error. die if !$rv; # Incomplete message } # Extract the message. my $msg = substr($buf, 0, $pos, ''); # Process the message. do_something($msg); } }