blib/lib/CBOR/Free/Decoder.pm

filter: Statementstmt 100% 19 / 19 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 76.6% 36 / 47 CC 7 SCAR 9.5
CC
7
Cov
79%
preserve_references
3.5
naive_utf8
3.5
BEGIN
2.0
CRAP 2.6
« blib/lib/CBOR/Free/AddOne.pm ↑ Summary blib/lib/CBOR/Free/X/CannotDecode64Bit.pm »
1 package CBOR::Free::Decoder;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 CBOR::Free::Decoder
8  
9 =head1 SYNOPSIS
10  
11 my $decoder = CBOR::Free::Decoder->new()->set_tag_handlers(
12 2 => sub { DateTime->from_epoch( epoch => shift() ) },
13 );
14  
15 # Enable shared/circular references:
16 $decoder->preserve_references();
17  
18 =head1 DESCRIPTION
19  
20 This class provides an object-oriented interface to L<CBOR::Free>’s
21 decoder. This interface allows interpretation of tagged values.
22  
23 =cut
24  
25 #----------------------------------------------------------------------
26  
27 4 ▶use CBOR::Free ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #----------------------------------------------------------------------
30  
31 =head1 METHODS
32  
33 =head2 $obj = I<CLASS>->new()
34  
35 Creates a new CBOR decoder object.
36  
37 =cut
38  
39 104 ▶sub new { bless { _flags => 0 } } # TODO: implement in XS, and store a context.
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40  
41 #----------------------------------------------------------------------
42  
43 =head2 $data = I<OBJ>->decode( $CBOR )
44  
45 Same as L<CBOR::Free>’s static function of the same name but applies
46 any tag handlers configured in C<set_tag_handlers()>.
47  
48 As in L<CBOR::Free>, any unrecognized tags prompt a warning but are
49 otherwise ignored.
50  
51 =cut
52  
53 #----------------------------------------------------------------------
54  
55 =head2 $enabled_yn = I<OBJ>->preserve_references( [$ENABLE] )
56  
57 Enables/disables recognition of CBOR’s shared references. (If no
58 argument is given, shared references will be enabled.)
59  
60 B<HANDLE WITH CARE.> This option can cause CBOR::Free to create circular
61 references, which can cause memory leaks if not handled properly.
62  
63 =cut
64  
65 sub preserve_references {
66 1 ▶ if (@_ < 2 || !!$_[1]) {
Subroutine preserve_references: called CC=3 SCAR=12.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2 or not !$_[1]) 1 0 1
Condition33%1/3
@_ < 2 or not !$_[1]
l!l&&r!l&&!r
100
MC/DC0%0/2
@_ < 2 or not !$_[1]
@_ < 2 not !$_[1]
Truth table33%1/3
A or B
A= @_ < 2B= not !$_[1]
ABresult
1X1
011
000
67 1 $_[0]{'_flags'} |= _FLAG_PRESERVE_REFERENCES();
68 }
69  
70 1 return !!($_[0]{'_flags'} & _FLAG_PRESERVE_REFERENCES());
71 }
72  
73 #----------------------------------------------------------------------
74  
75 =head2 $enabled_yn = I<OBJ>->naive_utf8( [$ENABLE] )
76  
77 Same interface as C<preserve_references()>, but this option tells I<OBJ>
78 to forgo UTF-8 validation of CBOR text strings when enabled. This speeds up
79 decoding of text strings but may confuse Perl if invalid UTF-8 is given in
80 a CBOR text string. That may or may not break your application.
81  
82 This I<should> be safe in contexts—such as IPC—where you control the CBOR
83 serialization and can thus ensure validity of the encoded text.
84  
85 If in doubt, leave this off.
86  
87 =cut
88  
89 sub naive_utf8 {
90 2 ▶ if (@_ < 2 || !!$_[1]) {
Subroutine naive_utf8: called CC=3 SCAR=12.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2 or not !$_[1]) 2 0 2
Condition33%1/3
@_ < 2 or not !$_[1]
l!l&&r!l&&!r
200
MC/DC0%0/2
@_ < 2 or not !$_[1]
@_ < 2 not !$_[1]
Truth table33%1/3
A or B
A= @_ < 2B= not !$_[1]
ABresult
1X1
011
000
91 2 $_[0]{'_flags'} |= _FLAG_NAIVE_UTF8();
92 }
93  
94 2 return !!($_[0]{'_flags'} & _FLAG_NAIVE_UTF8());
95 }
96  
97 #----------------------------------------------------------------------
98  
99 =head2 I<OBJ>->set_tag_handlers( %TAG_CALLBACK )
100  
101 Takes a list of key/value pairs where each key is a tag (i.e., number)
102 and each value is a coderef that CBOR::Free will run when that tag is
103 seen during a decode operation. The coderef will receive the tagged value,
104 and its (scalar) return will be inserted into the decoded data structure.
105  
106 To unset a tag handler, assign undef to it.
107  
108 This returns the I<OBJ>.
109  
110 B<NOTE:> Handlers assigned here will only fire if CBOR::Free itself
111 doesn’t decode the tag. For example, a handler for the “indirection” tag
112 here will be ignored.
113  
114 =cut
115  
116 4 ▶use constant _TAG_PACK_TMPL => eval { pack 'Q' } ? 'Q' : 'L';
Subroutine BEGIN: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "\000\000\000\000\000\000\000\000" } } ? : 4 0 4
117  
118 sub set_tag_handlers {
119 101 ▶ my ($self, @tag_cb) = @_;
Subroutine set_tag_handlers: called CC=2 SCAR=6.9
Pod documented
120  
121 101 ▶ while (my ($tag, $cb) = splice @tag_cb, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@tag_cb, 0, 2)) 101 101 202
122 101 $self->{'_tag_decode_callback'}{ pack( _TAG_PACK_TMPL(), $tag ) } = $cb;
123 }
124  
125 101 return $self;
126 }
127  
128 4 1;
« blib/lib/CBOR/Free/AddOne.pm ↑ Summary blib/lib/CBOR/Free/X/CannotDecode64Bit.pm »