in reply to perl pack/unpack optional variable length data

How do I use unpack in this case?

In what case?

You use too many words in the description but its still too vague

solution is simple, don't read more bytes than you can process

my( $tag, $length ) = unpack ..., $buffer; $buffer = readThisMany( $fh, $length ); my( $value ) = unpack ..., $buffer;

now if $value needs to be unpacked further, or you need to guess how many more bytes to read into $buffer, or ....

If the protocol/format doesn't follow length/value pattern, the docs must specify what pattern it follows, there must be a pattern, an assumption the original program that produced these bytes followed ... just gotta find out what it is

For example Declaring Hash entries