MsgPack::Encoder - Encode a structure into a MessagePack binary string
version 2.0.3
use MsgPack::Encoder; my $binary = MsgPack::Encoder->new( struct => [ "hello world" ] )->encoded; # using the msgpack_* functions my $binary = msgpack [ "hello world" ]; # or my $specific = msgpack_array16 [ "hello", "world" ]; use MsgPack::Decoder; my $struct = MsgPack::Decoder->new->read_all($binary);
MsgPack::Encoder objects encapsulate a Perl data structure, and provide its MessagePack serialization.
MsgPack::Encoder
In addition of the MsgPack::Encoder class, the module exports msgpack_* helper functions that would convert data structures to their MessagePack representations.
msgpack_*
$packed = msgpack_nil(); $packed = msgpack_bool($boolean); $packed = msgpack_positive_fixnum($num); $packed = msgpack_negative_fixnum($num); $packed = msgpack_uint8($int); $packed = msgpack_uint16($int); $packed = msgpack_uint32($int); $packed = msgpack_uint64($int); $packed = msgpack_int8($int); $packed = msgpack_int16($int); $packed = msgpack_int32($int); $packed = msgpack_int64($int); $packed = msgpack_bin8($binary); $packed = msgpack_bin16($binary); $packed = msgpack_bin32($binary); $packed = msgpack_float32($float); $packed = msgpack_float64($float); $packed = msgpack_fixstr($string); $packed = msgpack_str8($string); $packed = msgpack_str16($string); $packed = msgpack_str32($string); $packed = msgpack_fixarray(\@array); $packed = msgpack_array16(\@array); $packed = msgpack_array32(\@array); $packed = msgpack_fixmap(\%hash); $packed = msgpack_map16(\%hash); $packed = msgpack_map32(\%hash); $packed = msgpack_fixext1($type => $data); $packed = msgpack_fixext2($type => $data); $packed = msgpack_fixext4($type => $data); $packed = msgpack_fixext8($type => $data); $packed = msgpack_fixext16($type => $data); $packed = msgpack_ext8($type => $data); $packed = msgpack_ext16($type => $data); $packed = msgpack_ext32($type => $data);
$packed = msgpack( $data )
Which is equivalent to
$packed = MsgPack::Encoder->new(struct=>$data);
The stringification of a MsgPack::Encoder object is its MessagePack encoding.
print MsgPack::Encoder->new( struct => $foo ); # equivalent to print MsgPack::Encoder->new( struct => $foo )->encoded;
The constructor accepts a single argument, struct, which is the perl structure (or simple scalar) to encode.
struct
Returns the MessagePack representation of the structure.
Yanick Champoux <yanick@cpan.org>
This software is copyright (c) 2019, 2017, 2016, 2015 by Yanick Champoux.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MsgPack::RPC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MsgPack::RPC
CPAN shell
perl -MCPAN -e shell install MsgPack::RPC
For more information on module installation, please visit the detailed CPAN module installation guide.