Thanks for your help also!

But using unpack is better for, ahem, unpacking binary formats. You could write something like:

my ($tag, $data, $crc) = unpack q( (x4L X8Lx4/a* L)> ), $input;

Place ( )* around the template to get all the chunks.

Wow, that's concise and reasonably clear. I'm learning regex and more advanced pack|unpack at the same time so it's definitely something I need to dig into deeper.

Armed with the pack documentation I dissected your pack string into the following:

x4 - four null bytes (effectively skip the length fields)

L - 32bit value (the tag)

X8 - seek backwards 8 bytes (back to the start of the chunk

L - 32bit value (data length)

x4 - four null bytes (skip the tag field)

/ - pops the last value (data length) off the extraction stack and uses it as a repetition count for the next item

a* - binary data (count is set by the / above); not sure why * follows it, it works when removed

L - 32 bit CRC

It seems that I need to go through perlpacktut next!

I do have a small question however, when I try to repeat the group with * I get the following error:

'x' outside of string in unpack

Maybe this is something to do with the x at the start of the group? It's trying to read a new group starting just past the end of the string?

edit:Changing the first part of the pack string to L seemed to fix the 'outside of string' error. The data length is now explicitly within the returned values, but could be ignored later.

edit2:I span this out into a separate thread as it was getting too far off the original topic.


In reply to Re^2: Modifying pos() from within (?{...}) by mxb
in thread Modifying pos() from within (?{...}) by mxb

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.