in reply to How much was unpack()ed?

To "easily find out the place where unpack() finished" you could simply consume the string as you go:
(my($t),$foo) = unpack('Ca*', $foo); if ( $t == 3 ) { ($v,$foo) = unpack('Z*a*', $foo); }
Unforunately Z* doesn't finish at the zero byte - it actually consumes the remainder of the input and discards everything beyond the zero.

Replies are listed 'Best First'.
Re: Re: How much was unpack()ed?
by BrowserUk (Patriarch) on May 21, 2004 at 17:33 UTC

    If the fields are prefixed with length bytes, then you can prevent 'a*' (or 'A*' or 'Z*') from consuming the rest of the string by telling format that the length byte is there.

    $x = pack 'c/a*N', '12345123451234512345', 99999; print for unpack 'c/a* N', $x; 12345123451234512345 99999

    The downside is that the length byte is then consumed. To workaround that, you have to unpack the length byte twice.

    print for unpack 'cXc/a* N', $x; 20 12345123451234512345 99999

    However, I don't see how this helps the OP as his data has a 'type' byte but no 'length' byte.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Re: How much was unpack()ed?
by ambrus (Abbot) on May 22, 2004 at 12:12 UTC

    What? It seems to me that Z* does stop at the zero byte, it's only Z with a finite number that does not stop. Look.

    $ perl -we '($a,$b)= unpack "Z*a*", "nine\0eight"; warn ">>$a<< >>$b<< +";' >>nine<< >>eight<< at -e line 1. $ perl -we '($a,$b)= unpack "Z8a*", "nine\0eight"; warn ">>$a<< >>$b<< +";' >>nine<< >>ht<< at -e line 1.

    This was with perl, v5.8.1 built for i686-linux.

    Update: just checked, with perl 5.6.1, I get the wrong behaviour, that is, Z* consumes all the string.