3.2.105 packpack
This function takes a list of values and packs it into a
binary structure, returning the string containing the structure.
The
Each character may optionally be followed by a number which gives a repeat
count. Together the character and the repeat count make a field specifier.
Field specifiers may be separated by whitespace, which will be ignored. With
all types except This first pair of examples packs numeric values into bytes: $out = pack "cccc", 65, 66, 67, 68; # $out eq "ABCD" $out = pack "c4", 65, 66, 67, 68; # same thing This does a similar thing, with a couple of nulls thrown in: $out = pack "ccxxcc", 65, 66, 67, 68; # $out eq "AB\0\0CD" Packing your shorts doesn't imply that you're portable: $out = pack "s2", 1, 2; # "\1\0\2\0" on little-endian # "\0\1\0\2" on big-endian On binary and hex packs, the count refers to the number of bits or nybbles, not the number of bytes produced: $out = pack "B32", "01010000011001010111001001101100"; $out = pack "H8", "5065726c"; # both produce "Perl"
The length on an $out = pack "a4", "abcd", "x", "y", "z"; # "abcd" To get around that limitation, use multiple specifiers: $out = pack "aaaa", "abcd", "x", "y", "z"; # "axyz" $out = pack "a" x 4, "abcd", "x", "y", "z"; # "axyz"
The $out = pack "a14", "abcdefg"; # "abcdefg\0\0\0\0\0\0\0"
This template packs a C $out = pack "i9pl", gmtime, $tz, $toff; The same template may generally also be used in the unpack function. If you want to join variable length fields with a delimiter, use the join function. Note that, although all of our examples use literal strings as templates, there is no reason you couldn't pull in your templates from a disk file. You could, in fact, build an entire relational database system around this function. |
|