A little variation of your code (with a null byte inside the stream that is read by the first Z pattern):
The way I see it is that the Z5 pattern makes perl read the first five bytes and strip away everything after the first null byte (ie: the last byte with value 64 is stripped) to obtain the bytes (61, 62, 63, 00). Those four bytes are not yet a perl string but "A null-terminated (ASCIZ) string", where the null byte is part of the formatting, not the data. It is the translation from one format (null-terminated string) to another (perl string) that makes the null byte disappear, not the stripping.use v5.10; my $str = qq{abc\0def}; my $pck = pack q{Z*}, $str; say sprintf q{%02x}, ord for split m{}, $pck; say q{-} x 5; say join ",", unpack q{Z5Z*}, $pck; __DATA__ 61 62 63 00 64 65 66 00 ----- abc,ef
With that interpretation it's the fact that you can do unpack "Z4", "hello", where the obtained data is not a valid null-terminated string, that might be surprising, but that's just perl Doing What You Mean as it usually does.
In reply to Re: Behaviour of unpack() with the Z template
by Eily
in thread Behaviour of unpack() with the Z template
by johngg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |