Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Behaviour of unpack() with the Z template

by Eily (Monsignor)
on Feb 07, 2017 at 18:55 UTC ( [id://1181329]=note: print w/replies, xml ) Need Help??


in reply to Behaviour of unpack() with the Z template

A little variation of your code (with a null byte inside the stream that is read by the first Z pattern):

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
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.

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.

Replies are listed 'Best First'.
Re^2: Behaviour of unpack() with the Z template
by johngg (Canon) on Feb 08, 2017 at 10:25 UTC
    the Z5 pattern makes perl read the first five bytes and strip away everything after the first null byte

    I agree with your analysis but the word "after" here and in the documentation is where I see a problem. It implies that only text that follows the null will be stripped, not the null itself. The behaviour makes sense, the description is, I think, wrong.

    Cheers,

    JohnGG

      That was precisely my point, that the null byte is not ignored like the other characters after it. It's part of the data when encoded in the format "null-terminated string", but not part of the value. It doesn't appear in the output value because it's a format detail, the same way the byte order doesn't appear after decoding a little endian or big endian integer value.

      But that's nitpicking, this would make the documentation misleading rather than plain wrong, which should also be avoided in a documentation. I think your phrasing returns everything up to but not including the first null. is fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found