Pack
NAME
Numeric::Pack - Convert Raku Numerics to Bufs and back again!
SYNOPSIS
use Numeric::Pack :ALL;
# pack and unpack floats
my Buf $float-buf = pack-float 2.5;
say "{ $float-buf.perl } -> { unpack-float $float-buf }";
# pack and unpack doubles
my Buf $double-buf = pack-double 2.5;
say "{ $double-buf.perl } -> { unpack-double $double-buf }";
# pack and unpack Int (see also int64 variants)
my Buf $int-buf = pack-int32 11;
say "{ $int-buf.perl } -> { unpack-int32 $int-buf }";
# pack and unpack specific byte orders (native-endian is the default)
my Buf $little-endian-buf = pack-int32 11, :byte-order(little-endian);
say "{ $little-endian-buf.perl } -> {
unpack-int32 $little-endian-buf, :byte-order(little-endian)
}";
DESCRIPTION
Numeric::Pack is a Raku module for packing values of the Numeric role into Buf objects (With the exception of Complex numbers). This module provides a Rakudo compatible, non-experimental, numeric packing and unpacking facility, built with utilities of NativeCall. Integer ranges from 8 to 64 bits are supported and floating point as well as double precision floating point numbers. Byte order defaults to the native byte order of the system and can be specified with the ByteOrder enum.
Numeric::Pack exports the enum ByteOrder by default (ByteOrder is exported as :MANDATORY).
ByteOrder | Description |
---|---|
native-endian | The native byte ordering of the current system |
little-endian | Common byte ordering of contemporary CPUs |
big-endian | Also known as network byte order |
By default Numeric::Pack's pack and unpack functions return and accept big-endian Bufs. To override this provide the :byte-order named parameter with the enum value for your desired behaviour. To disable byte order management pass :byte-order(native-endian).
Use Numeric::Pack :ALL to export all exportable functionality.
Use :floats or :ints flags to export subsets of the module's functionality.
Export tag | | Functions |
---|---|
:floats | pack-float, unpack-float, pack-double, unpack-double |
:ints | pack-uint32, pack-int32, unpack-int32, unpack-uint32, pack-int64, unpack-int64, pack-uint64, unpack-uint64, pack-int16, unpack-int16, pack-uint16, unpack-uint16, pack-int8, unpack-int8, pack-uint8, unpack-uint8 |
:ber | pack-ber, unpack-ber, collect-ber |
CHANGES
Added ber encoding and decoding ala perl pack 'w' | Expanded potential use cases | 2020-04-28 |
Added 8 and 16 bit integer types | Expanded potential use cases | 2020-04-27 |
Removed bundled native library, now pure perl6 | Improved portability and reliability | 2018-06-20 |
Added pack-uint32, pack-uint32 and unpack-uint32 | Added support for unsigned types | 2017-04-20 |
Changed named argument :endianness to :byte-order | Signatures now read more naturally | 2016-08-30 |
SEE ALSO
Rakudo core from 6.d 2018 and later supports reading values from a blob8 with a very similar interface to this module, see the docs here: https://docs.perl6.org/type/Blob#Methods_on_blob8_only_(6.d,_2018.12_and_later).
The Native::Packing module provides a role based packing mechanism for classes.
The Binary::Structured module appears to be similar to Native::Packing but implemented via a class inheritance interface.
The P5pack module is a re-implementation of perl 5's pack subroutine (Raku's pack subroutine is currently experimental in rakudo).
AUTHOR
Sam Gillespie <[email protected]>
COPYRIGHT AND LICENSE
Copyright 2016 Sam Gillespie
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.