in reply to Unpack()ing a stream

Ok, that's more code than I can digest right now. Does it play well with formats like '(N/a*)*'?


Oops. I originally wrote that as "(a*/N)*" which is wrong - the N comes before the a*. pack will tell you that if you get it wrong.

Replies are listed 'Best First'.
Re: Re: Unpack()ing a stream
by Stevie-O (Friar) on May 24, 2004 at 18:03 UTC
    It should work with any input unpack() itself normally works with. It's a simple (and inefficient) technique: It appends 'a*' to the end of the unpack string and pops off the last element returned. This will be the remainder of the string. A simple subtraction (length($fullstring) - length($remainder)) will reveal the point at which unpack() stopped unpacking the data the user actually asked for.
    --Stevie-O
    $"=$,,$_=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
Re: Re: Unpack()ing a stream
by BrowserUk (Patriarch) on May 24, 2004 at 17:50 UTC

    What on earth does '(a*/N)*' do? unpack seems to be trying to use it, but I can't work out what kind of input to supply?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      When given to pack() it consumes a list of strings and returns a single string where every string is now prefixed with the length, encoded in a big-endian 32 bit value. Its fantastic for serializing a bunch of strings. Do note - I wrote the format backwards so the "N" comes before the "A*". pack will tell you this as well so you can't make the mistake and let it just get away from you.

      pack '(N/a*)*', list of strings; $foo = pack '(N/a*)*', "A", "BB", "CCC", "DDDD"; $foo eq ( "\0\0\0\1A" . "\0\0\0\2BB" . "\0\0\0\3CCC" . "\0\0\0\4DDDD" +);

        Okay. I'm aware of the 'N/a*' ('c/a*', 's/a*' etc.) formats.

        It was the reversal that confused me, combined with that when I tried it with unpack, it doesn't report an error.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail