user3098497
user3098497

Reputation: 17

Pack and unpack in Perl

I confused with pack and unpack while writing a Perl script. What is the difference between between pack and unpack in Perl (explain with a simple example)?

Upvotes: -4

Views: 1307

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

Do you understand the difference between split and join? unpack is a bit like split (it takes a string and produces a list of values) and pack is a bit like join (it takes a list of values and produces a string).

Upvotes: 5

DavidO
DavidO

Reputation: 13942

pack takes a list of values and converts it into a string using the rules given by the template.

unpack takes a string and expands it out to a list of values using the rules given by the template.

$ perl -E 'say for unpack "H2H2", "56"'
35
36

$ perl -E 'say pack "H2H2", 35, 36'
56

Upvotes: 3

Related Questions