# $sock is open, probably is a IO::Socket of some type my $total_read = 0; my $full_data = ''; my $total; while ( 1 ) { my $read = 0; while ($read < 4) { $read += read($sock,$data,4-$read,$read) # not sure if last arg should have a +1 on it } $total_read += $read; if (! $total) { $total = unpack("N",$data); } elsif ( $total_read >= $total + 4 ) { process(substr($data,0,$total_read-4-$total)); $full_data .= substr($data,0,$total_read-4-$total); last; } else { $full_data .= $data; process($data); other_process($full_data); } } full_process(substr($full_data,0,$total));