Take a look at the use of the '/' in the documentation for pack, you can also use this format character in unpack.

Basically, an unpack format of 'C/a', will read the byte value represented by the C, and then use that as the length specifier for the character following the '/'; in this case 'A' for ascii data. As you also need the length of the metadata in order to remove it from the stream, you'll need a template of "a$DATASIZE C X C/A", which will capture the data to the first variable, the length to the second, backup over the length byte and then use it to capture the metadata to the third variable.

I've used a datasize of 10 and an array to simulate the read in this example. The critical part is exiting the while loop when there is not enough data left to fulfill the data size, then read the next lump and append it to the residual:

#! perl -slw use strict; use bytes; my $DATASIZE = 10; my @stream = ( "abcdefghij\x04fredabcdefghij\x06barneyabcdefghij\x00abcde", "fghij\x07bam bamabcdefghij\x00abcdefghij" ); my $stream = ''; for ( @stream ) { $stream .= $_; while( length( $stream ) > $DATASIZE) { my( $data, $len, $meta ) = unpack "a$DATASIZE C X C/A", $strea +m; print "\ndata:$data"; print "meta:$meta" if $len; my $trim = $len ? $len+1 : 1; $stream = bytes::substr( $stream, $DATASIZE + $trim ); } } __END__ c:\test>junk data:abcdefghij meta:fred data:abcdefghij meta:barney data:abcdefghij data:abcdefghij meta:bam bam data:abcdefghij

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Reading (and parsing) a byte stream by BrowserUk
in thread Reading (and parsing) a byte stream by qbxk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.