http://qs1969.pair.com?node_id=1116886


in reply to pack() v unpack() padding: bug or ???

Hello BrowserUk,

As I understand it, pack (A2)*, LIST says: for each element in LIST, convert it to a 2-byte ASCII string, padded with a trailing space character if necessary; then return the concatenation of these converted elements as a single string.

Logically, then, since unpack “does the reverse of pack”, unpack (A2)*, STRING should break STRING into 2-character chunks, and for each chunk, first remove any trailing spaces, then convert it to an appropriate value. Which is what I see:

2:03 >perl -MData::Dump -wE "my @c = unpack '(A2)*', 'f 12dx z'; dd \ +@c;" ["f", 12, "dx", " z"] 2:04 >

Note that "f " has been converted to "f" by removal of the trailing space (padding).

print unpack 'C*', pack '(Z2)*', 'fr','ed','x';; 102 0 101 0 120 0 ## Note! the 'r' & 'd' characters have been thrown +away? Another bug?

I don’t see a problem here, either. The template Z2 specifies a 2-byte null-terminated string. Packing 'fr' of course requires 3 bytes, so the second character is thrown away and the packed result is f\0. Isn’t that what it’s supposed to do?

Hope that helps (and I apologise if I’ve missed the point!),

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,