in reply to Re: Modifying pos() from within (?{...})
in thread Modifying pos() from within (?{...})
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Modifying pos() from within (?{...})
by Eily (Monsignor) on Apr 27, 2018 at 15:10 UTC | |
by AnomalousMonk (Archbishop) on Apr 27, 2018 at 16:31 UTC |