NAME

MsgPack::Decoder - Decode data from a MessagePack stream

VERSION

version 2.0.2

SYNOPSIS

use MsgPack::Decoder;

use MsgPack::Encoder;
use Data::Printer;

my $decoder = MsgPack::Decoder->new;

my $msgpack_binary = MsgPack::Encoder->new(struct => [ "hello world" ] )->encoded;

$decoder->read( $msgpack_binary );

my $struct = $decode->next;  

p $struct;    # prints [ 'hello world' ]

DESCRIPTION

MsgPack::Decoder objects take in the raw binary representation of one or more MessagePack data structures, and convert it back into their Perl representations.

METHODS

new( %args )

Constructor. Accepts the following arguments.

emitter

If sets to true, incoming decoded data is immediately removed from the buffer and broadcasted via a decoded event encapsulated in a MsgPack::Decoder::Event::Decoded object.

MsgPack::Decoder consumes the Beam::Emitter role and subscription/unsubscription to the decoded event is done via its methods.

my $decoder = MsgPack::Decoder->new( emitter => 1 );
$decoder->on( 'decoded' => sub {
    my $event = shift;
    my @structs = $event->payload_list;
    warn "we received ", scalar(@structs), " data structures";
});

read( @binary_values )

Reads in the raw binary to convert. The binary can be only a partial piece of the encoded structures. If so, all structures that can be decoded will be made available in the buffer, while the potentially last unterminated structure will remain "in flight".

Returns how many structures were decoded.

has_buffer

Returns the number of decoded structures currently waiting in the buffer.

next

Returns the next structure from the buffer.

$decoder->read( $binary );

while( $decoder->has_buffer ) {
    my $next = $decoder->next;
    do_stuff( $next );
}

Note that the returned structure could be undef, so don't do:

$decoder->read( $binary );

# NO! $next could be 'undef'
while( my $next = $decoder->next ) {
    do_stuff( $next );
}

all

Returns (and flush from the buffer) all the currently available structures.

read_all( @binaries )

Reads the provided binary data and returns all structures decoded so far.

@data = $decoder->read_all($binary);

# equivalent to

$decoder->read(@binaries);
@data = $decoder->all;

read_next( @binaries )

Reads the provided binary data and returns the next structure decoded so far. If there is no data in the buffer, dies.

$data = $decoder->read_next($binary);

# roughly equivalent to

$decoder->read(@binaries);
$data = $decoder->next or die;

AUTHOR

Yanick Champoux <yanick@cpan.org>

COPYRIGHT AND LICENSE

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.