Packing
NAME
Native::Packing
DESCRIPTION
This module provides a role for binary serialization of simple structs. At this stage, only scalar native integer and numeric attributes are supported.
This role is applicable to classes that contain only simple native numeric attributes, representing the structure of the data.
EXAMPLE
use v6;
use Native::Packing :Endian;
# open a GIF read the 'screen' header
my class GifHeader
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".IO.open( :r :bin);
$fh.read(6); # skip GIF header
my GifHeader $screen .= read: $fh;
say "GIF has size {$screen.width} X {$screen.height}";
METHODS
unpack(buf8)
Class level method. Unpack bytes from a buffer. Create a struct object.
pack(buf8?)
Object level method. Serialize the object to a buffer.
read(fh)
Class level method. Read data from a binary file. Create an object.
write(fh)
Object level method. Write the object to a file
bytes
Determine the overall size of the struct. Sum of all its attributes.
host-endian
Return the endian of the host Endian::Network(0) or Endian::Vax(1).