I'm reading null-separated blocks of data from a socket; upon finding a null, the block read so far is handed for processing, and the buffer is truncated to whatever was left after the null (usually nothing).
$in .= <$sock>
} while(!in =~ /^(.*?)\x00(.*)/) ;
print "A:[ $in, $block ]\n";
$block = $1;
$in = $2;
print "B:[ $in, $block ]";
B:[ , ]
Another idea is to change the value
$/ which determines how much will
<$sock> read from
$sock. So for example, if you do
then for that scope,
<$sock> will read until the end of the next (null-separated) block.
Edit: If the input consists of null-separated blocks, it does not make sense to use
$/ having its default value, because then you would be reading the input line by line. So I think there are 2 approaches:
<$sock>to read the next block.
$/set to a reference of the chunk size (e.g.
local $/=\4096), or use
sysread. I would also use
substrto search for the null and extract the chunk in that case.