Stevie-O has asked for the wisdom of the Perl Monks concerning the following question:
I need to unpack() a pile of data that's in a flexible format. This data has a lot of metadata, e.g. length-of-string, type-of-next-value, etc.. One of the problems is that I don't often know *what* I'm unpacking until I just before need to unpack it.
For example, let's say one of the values was a 'type' byte. 0=byte, 1=short, 2=long, 3=ASCIIZ. I need to do this:
Now, that's not so hard. The problem arises when I have to fetch the NEXT value. For the first three cases it's easy (fixed lengths) but the third is variable-length. How to easily find out the place where unpack() finished, so I can pick up where it left off?$t = unpack('C', $foo); if ($t == 0) { $v = unpack('C', substr($foo, 1)) } elsif ($t == 1) { $v = unpack('S', substr($foo, 1)) } elsif ($t == 2) { $v = unpack('L', substr($foo, 1)) } elsif ($t == 3) { $v = unpack('Z*', substr($foo, 1)) }
Some might offer 'well, use length($v) + 1 for Z*'. Well, that works for Z*. But what about things like $v = unpack('w', $foo)? 'w' is a variable-length encoded integer value. How do I know whether it was encoded with 1, 2, 3, or 4 bytes? Better yet, try several things - unpack('(wZ*w)5)'
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How much was unpack()ed?
by NetWallah (Canon) on May 21, 2004 at 16:17 UTC | |
Re: How much was unpack()ed?
by meredith (Friar) on May 21, 2004 at 18:52 UTC | |
Re: How much was unpack()ed?
by ambrus (Abbot) on May 21, 2004 at 16:11 UTC | |
Re: How much was unpack()ed?
by Anonymous Monk on May 21, 2004 at 17:09 UTC | |
by BrowserUk (Patriarch) on May 21, 2004 at 17:33 UTC | |
by ambrus (Abbot) on May 22, 2004 at 12:12 UTC | |
Re: How much was unpack()ed? (stream)
by tye (Sage) on May 22, 2004 at 04:34 UTC | |
Re: How much was unpack()ed?
by rikkuru (Initiate) on Apr 01, 2020 at 11:30 UTC |