http://www.perlmonks.org?node_id=1034810

johnlumby has asked for the wisdom of the Perl Monks concerning the following question:

my perl app receives a buffer containing this format

{ short lengths[2]; char data[???]; } where there are two items in data, of length lengths[0] and lengths[1] respectively.

I have tried and tried to unpack this into 4 perl variables in a single unpack() call and failed. I had to resort to two unpack()'s, the first to extract the lengths followed by something like

eval('($junk1 , $junk2, $data0, $data1) = unpack "ssA' . $data0_length + . "A" . $data1_length . '",$rcvd_buf;');

which works fine, but can some kind Monk tell me how to do this in one unpack.