blib/lib/DR/Tarantool/MsgPack.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 77 CC 3 SCAR 10.8
CC
3
Cov
0%
new
6.0
msgpack
6.0
msgpack
2.0
CRAP 2.9
1 use utf8;
2 use strict;
3 use warnings;
4  
5 =head1 NAME
6  
7 DR::Tarantool::MsgPack - msgpack encoder/decoder.
8  
9 =head1 SYNOPSIS
10  
11 use DR::Tarantool::MsgPack 'msgpack', 'msgunpack', 'msgcheck';
12  
13 # encode object
14 my $pkt = msgpack({ a => 'b' });
15  
16 # decode object
17 my $object = msgunpack($pkt);
18  
19 # decode object with utf8-strings
20 my $object = msgunpack($pkt, 1);
21  
22 # check if $string is valid msgpack
23 $object = msgunpack($str, 1) if msgcheck($str);
24  
25 =head1 METHODS
26  
27 =head2 msgpack($OBJECT)
28  
29 Encode perl object (scalar, hash, array) to octets.
30  
31 =head2 msgunpack($OCTETS[, $UTF8])
32  
33 Decide octets to perl object. Return perl object and tail of input string.
34  
35 If C<$UTF8> is true, L<msgunpack> will decode utf8-strings.
36  
37 =cut
38  
39  
40 package DR::Tarantool::MsgPack;
41 use Carp;
42 require DR::Tarantool;
43 use base qw(Exporter);
44 our @EXPORT_OK = qw(msgpack msgunpack msgcheck);
45  
46 sub msgpack($) {
47 DR::Tarantool::_msgpack($_[0])
48 }
49  
50 sub msgunpack($;$) {
51 my ($pkt, $utf8) = @_;
52 $utf8 ||= 0;
53 $utf8 &&= 1;
54 DR::Tarantool::_msgunpack($pkt, $utf8)
55 }
56  
57 sub msgcheck($) {
58 DR::Tarantool::_msgcheck($_[0])
59 }
60  
61 sub TRUE() { DR::Tarantool::MsgPack::Bool->new(1) };
62 sub FALSE() { DR::Tarantool::MsgPack::Bool->new(0) };
63  
64  
65 =head2 true and false
66  
67 Protocol supports C<true> and C<false> statements.
68 L<msgunpack> unpacks them to C<1> and C<0>.
69  
70 If You want to pack C<true> You can use B<DR::Tarantool::MsgPack::Bool>:
71  
72 use DR::Tarantool::MsgPack 'msgpack';
73  
74 my $to_pack = { a => DR::Tarantool::MsgPack::Bool->new(0) };
75 my $pkt = msgpack($to_pack);
76  
77 =cut
78  
79 package DR::Tarantool::MsgPack::Bool;
80 use Carp;
81 use overload
82 'int' => sub { ${ $_[0] } },
83 '""' => sub { ${ $_[0] } },
84 'bool' => sub { ${ $_[0] } }
85 ;
86  
87 sub new {
88 my ($class, $v) = @_;
89 my $bv = $v ? 1 : 0;
90 return bless \$v => ref($class) || $class;
91 }
92  
93 sub msgpack :method {
94 my ($self) = @_;
95 return scalar pack 'C', ($$self ? 0xC3 : 0xC2);
96 }
97  
98  
99 1;