Pack and unpack allow Perl to deal with primitive data types that are often found in binary file formats or in network packets. Where space is at a premium, data is often encoded using short, space-efficient binary representations rather than text.

What many of the pack formats involve is different representations of numbers. Your "signed char" is an example of these. Some of these types are specific sizes (which translates to fixed numeric ranges). Where it says in the "perldoc -f pack" document that something is exactly so many bits, these are fixed sizes:<bl>

  • unsigned 8 bits (unsigned char, 'C'): 0 - 255
  • signed 8 bits (signed char, 'c'): -128 - 127
  • unsigned 16 bits (unsigned short, 'S', 'N', 'V'): 0 - 65535
  • signed 16 bits (short, 's', 'n', 'v'): -32678 - 32767
  • unsigned 32 bits (unsigned long, 'L', 'V'): 0 - 4294967295
  • signed 32 bits (long, 'l', 'v'): -2147483648 - 2147483647
  • </bl> What complicates the representation of numbers as streams of bits is that the bits are arranged into bytes. So if you have a 32 bit number that takes 4 bytes to represent, there are several different ways to arrange those 4 bytes. Hence the various different "byte orders".

    Other pack formats deal with other kinds of encodings for numbers, or representations of strings. These, again, are typically found in files or in network packets.

    Unless you're dealing with external files or network protocols that use binary or other non-text representation of data, you shouldn't need to bother with pack

    But it's quite useful when you need it, like any power tool. For instance, I couldn't have written Archive::Zip nearly as fast without it, because the zip file format has fixed binary headers.


    In reply to Re: Explanation of pack by bikeNomad
    in thread Explanation of pack by razor8

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.