4

I am REALLY confused about pack and unpack definition for perl. Below is the excerpt from perl.doc.org

The pack function converts values to a byte sequence containing representations according to a given specification, the so-called "template" argument. unpack is the reverse process, deriving some values from the contents of a string of bytes.

So I get the idea that pack takes human readable things(such as A) and turn it into binary format. Am I wrong on this interpretation??

So that is my interpreation but then same doc immediately proceeds to put this example which put my understanding exactly the opposite.

my( $hex ) = unpack( 'H*', $mem );
print "$hex\n";

What am I missing?

ikegami
391k17 gold badges291 silver badges555 bronze badges
asked Nov 12, 2017 at 23:35
6
  • I find the example to agree with the description: "a string of bytes" ($mem) is converted to "values" (hex). Also, "binary format" is a fairly broad term -- does "byte sequence" agree with what you mean by it? Can you explain a little better what is unclear? Commented Nov 12, 2017 at 23:46
  • (The example is on unpack -- so the opposite of your interpretation of pack, right?) Commented Nov 12, 2017 at 23:48
  • so yes, description of what I read is on pack. But then example shows unpack doing what I thought pack would be doing. What am I missing here??? Commented Nov 12, 2017 at 23:55
  • I guess this is what I am missing "Assuming that the variable $mem holds a sequence of bytes that we'd like to inspect without assuming anything about its meaning, we can write" Commented Nov 13, 2017 at 0:06
  • Yes, imagine somtething like my $mem = join "", map chr, 0, 1, 31, 32, 63; preceding the snippet. Commented Nov 13, 2017 at 0:08

1 Answer 1

7

The pack function puts one or more things together in a single string. It represents things as octets (bytes) in a way that it can unpack reliably in some other program. That program might be far away (like, the distance to Mars far away). It doesn't matter if it starts as something human readable or not. That's not the point.

Consider some task where you have a numeric ID that's up to about 65,000 and a string that might be up to six characters.

print pack 'S A6', 137, $ARGV[0];

It's easier to see what this is doing if you run it through a hex dumper as you run it:

$ perl pack.pl Snoopy | hexdump -C
00000000 89 00 53 6e 6f 6f 70 79 |..Snoopy|

The first column counts the position in the output so ignore that. Then the first two octets represent the S (short, 'word', whatever, but two octets) format. I gave it the number 137 and it stored that as 0x8900. Then it stored 'Snoopy' in the next six octets.

Now try it with a shorter name:

$ perl test.pl Linus | hexdump -C
00000000 89 00 4c 69 6e 75 73 20 |..Linus |

Now there's a space character at the end (0x20). The packed data still has six octets. Try it with a longer name:

$ perl test.pl 'Peppermint Patty' | hexdump -C
00000000 89 00 50 65 70 70 65 72 |..Pepper|

Now it truncates the string to fit the six available spaces.

Consider the case where you immediately send this through a socket or some other way of communicating with something else. The thing on the other side knows it's going to get eight octets. It also knows that the first two will be the short and the next six will be the name. Suppose the other side stored that it $tidy_little_package. It gets the separate values by unpacking them:

my( $id, $name ) = unpack 'S A6', $tidy_little_package;

That's the idea. You can represent many values of different types in a binary format that's completely reversible. You send that packed string wherever it needs to be used.

I have many more examples of pack in Learning Perl and Programming Perl.

answered Nov 13, 2017 at 2:34
Sign up to request clarification or add additional context in comments.

1 Comment

Why is the s in the unpack 's A6' lower case?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.