At 07:29 AM 9/19/00 -0700, Dave Storrs wrote:
>         I guess, if I had to write an explanation of pack/unpack based on
>my limited understanding, it would be something like:
>
>         "Unpack takes binary data in some particular format and
>disassembles it, assigning various pieces of it to variables according to
>formatting that you supply.  Pack does the opposite, using your supplied
>formatting to crunch Perl scalar variables into binary data that is
>represented in some specific way.  The binary data used by (un)pack will
>belong to exactly one type of C numeric variable, meaning that it will be
>limited in what kinds of numbers it can store and how it will represent
>them."
>
>         Is this definition completely off-base?

I think you are on track, but I would probably have defined it the other 
way around:

"The perl function pack() takes Perl values and converts them to a compact 
binary representation based on a user-specified template.  This is useful 
for communicating with external data formats that expect data to be in a 
particular binary format that is not native to perl.  The perl function 
unpack() takes a compact binary representation of data and converts it to 
Perl values based on a user-specified template.  Pack() and unpack() are 
inverse-functions of each other: if used with the same template, unpack can 
be used to recover data that has been packed.

Example:

$Tudpheader = "nnnn"; # source, destination, length, checksum
$Tudppseudoheader = "NNxcn"; # source IP, dest IP, protocol (must be 17), 
length

$udplength = length($data)+8;

$udpcheck = (pack $Tpseudoheader, $sip,$dip,17, $udplength)
             . $(pack $Tudbheader,$sport,$dport,$udplength, 0)
             . $data;

$checksum = udpchecksum($udpcheck);

$udppacket = (pack $Tudbheader,$sport,$dport,$udplength,$checksum) . $data

"

>                                 Dave

Reply via email to