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

Explanation of pack

by razor8 (Acolyte)
on May 31, 2001 at 18:31 UTC ( [id://84568]=perlquestion: print w/replies, xml ) Need Help??

razor8 has asked for the wisdom of the Perl Monks concerning the following question:

I saw pack being used somewhere and I had no idea what it did. So I pulled out my trusty copy of the Camel book and went to read up on it. It was well documented but I couldn't understand any of it.

Eg:

c A signed char value. C An unsigned char value.

Is there a good explanation somewhere of what all of them mean?
Eg: I have no idea what a signed char is.
From what I have tried to find out they seem to come from C. Is this true?

Replies are listed 'Best First'.
Re: Explanation of pack
by bikeNomad (Priest) on May 31, 2001 at 20:42 UTC
    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.

Re: Explanation of pack
by thpfft (Chaplain) on May 31, 2001 at 18:47 UTC

    This confession is one of my favourite nodes. It should help, and it'll certainly make you feel better.

Re: Explanation of pack
by thabenksta (Pilgrim) on May 31, 2001 at 18:39 UTC

    There are many wonderfull explanations right here in the Monastery, just search for pack or pack explanation.

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Re: Explanation of pack
by azatoth (Curate) on May 31, 2001 at 18:40 UTC
      Yes I have.
      But I am not understanding what the various things like a signed char that means nothing to me.
      I have searched for explanations but all the questions are from people that understand how pack works.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found