Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

What are pack/unpack used for?

by Anonymous Monk
on Jun 09, 2012 at 12:54 UTC ( #975298=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am a perl newbie, I am reading the tutorial on pack/unpack functions present on perldoc site.

What are pack/unpack used for the in practical applications. Does anybody have use cases where pack and unpack are used.

How to go about using, those functions and in what situations are they appropriate?

Comment on What are pack/unpack used for?
Re: What are pack/unpack used for?
by flexvault (Parson) on Jun 09, 2012 at 13:36 UTC

    I just did a super search to find this post. I think it explains just some of things you can use 'pack/unpack' for.

    "Well done is better than well said." - Benjamin Franklin

Re: What are pack/unpack used for?
by Utilitarian (Vicar) on Jun 09, 2012 at 15:05 UTC
    Wow, they are used for so many things, to get a binary representation of a number, to parse binary data out of a file format, to parse network packets... the list is pretty much endless.

    Could I suggest you buy the Perl Cookbook and check them out in the index... or search for them on this site, to see a range of their uses in converting between data formats.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: What are pack/unpack used for?
by ww (Bishop) on Jun 09, 2012 at 15:19 UTC
Re: What are pack/unpack used for?
by bulk88 (Priest) on Jun 09, 2012 at 18:21 UTC
    Un/pack is used to convert from human readable perl scalars to unreadable binary gibberish that makes all kinds of dings and bells and sounds when you print it to console. General guidelines are, if you have fixed length data, use un/pack. If you have variable length data, use a regexp. Un/pack has a little bit of variable length data parsing ability FYI so the guidelines aren't set in stone. For XML use a regexp (or better a proper XML parser), for C structs or things that are mostly squares if you open the file in a text editor, use un/pack. Un/pack is faster than a regexp for the same data if both can do the same thing. A regexp can not convert unprintable binary to printable numbers (but see tr and s). For a perl program that takes no input from a network, a disk file, or a C function and only outputs human readable text, you don't usually need un/pack. If you see you need to write "$number = 0xF000 | 0x80;" you might need to use pack later on. In a perfect world, all input/output is done through Perl modules dedicated to that I/O medium, so you never need to call un/pack. In real life, thats not true, and then you have to call un/pack. Un/pack makes Perl the ultimate glue language.
Re: What are pack/unpack used for?
by ikegami (Pope) on Jun 11, 2012 at 06:10 UTC
    Perl scalars cannot be stored or communicated; they are confined to a Perl interpreter instance. The values they contain must be serialised, which is to say converted to a stream of bytes, in order to do so. This is pack's primary purpose. unpack is used to do the inverse, namely deserialising, which is to say converting sequences of bytes into scalars.
Re: What are pack/unpack used for?
by Tux (Monsignor) on Jun 11, 2012 at 10:50 UTC

    unrelated to the Unix pack command, it can have the same effect, which matters if resources lack:

    use Devel::Size qw(total_size); my @aoa = map { [ int rand 1_000_000, int rand 20_000, chr 32 + int ra +nd 40 ] } 0 .. 450_000; my @aop = map { pack "l>s>A", @$_ } @aoa; say "Unpacked: ", total_size (\@aoa); say "Packed: ", total_size (\@aop); => $ perl test.pl Unpacked: 100800288 Packed: 28800128

    Enjoy, Have FUN! H.Merijn
Re: What are pack/unpack used for?
by DrHyde (Prior) on Jun 11, 2012 at 11:10 UTC
    If you ever need to deal with binary data, or with fixed width records, then pack/unpack may be useful tools and should be considered.

    That doesn't mean that they'll always be the *best* tool for the job as they have some annoying limitations and the documentation is somewhat, umm, opaque, but you should certainly consider them, and if you decide to use something else you should be able to justify it. Remember - TIMTOWTDI,BMOTAWMOTT,ESOTT (but most of them are wrong most of the time, essential some of the time).

    For example, I mostly don't use them in Data::Hexdumper because I want to support 64 bit data on 32 bit platforms.

Re: What are pack/unpack used for?
by Tux (Monsignor) on Jun 11, 2012 at 12:03 UTC

    Related, but worth browsing, are the way flags are dealt with on different architectures. I have made an overview for one of my talks.


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975298]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-07-11 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (221 votes), past polls