Struct packing/unpacking from binary data
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
t add :offset option to read(), unpack() Nov 23, 2017
.travis.yml bump rakudo for .travis/zef Nov 17, 2017
Changes add :offset option to read(), unpack() Nov 23, 2017
LICENSE adding basic packing Apr 11, 2017
META6.json allow nesting packing structs Nov 21, 2017 add :offset option to read(), unpack() Nov 23, 2017


Build Status


Native::Packing is a simple solution for structured reading and writing of binary numerical data.


use v6;
use Native::Packing :Endian;

# open a GIF read the header
my class LogicalDescriptor
    does Native::Packing[Endian::Vax] {

    has uint16 $.width;
    has uint16 $.height;
    has uint8  $.flags;
    has uint8  $.bgColorIndex;
    has uint8  $.aspect;

my $fh = "t/lightbulb.gif" :r, :bin);
my $offset = 6;  # skip GIF header

my LogicalDescriptor $screen .= read: $fh, :$offset;
say "GIF has size {$screen.width} X {$screen.height}";

It currently handles records containing native integers (int8, uint8, int16, etc), numerics (num32, num64) and sub-records of type Native::Packing.

  • Data may read be and written to binary files, via the read and write methods

  • Or read and written to buffers via the unpack and pack methods.


The two fixed modes are:

  • Vax (big endian) - least significant byte written first

  • Network (little endian) - most significant byte written first

The endianess of the binary format needs to be known to correctly read and write to it.

There is also a platform-dependant Host mode. This will read and write binary data in the same endianess as the host computer.

Endian Examples:

use Native::Packing :Endian;
class C { has int16 $.a }
my $c = :a(42);
say ($c but Native::Packing[Vax]).pack;     # Buf[uint8]:0x<2a 00>
say ($c but Native::Packing[Network]).pack; # Buf[uint8]:0x<00 2a>
say ($c but Native::Packing[Host]).pack;    # Depends on your host