File Coverage

blib/lib/BSON/Binary.pm
Criterion Covered Total %
statement 30 35 85.7
branch 4 4 100.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 0 6 0.0
total 46 59 77.9


line stmt bran cond sub pod time code
1 71     71   26290 use 5.010001;
  71         231  
2 71     71   369 use strict;
  71         133  
  71         1512  
3 71     71   329 use warnings;
  71         130  
  71         2489  
4              
5             package BSON::Binary;
6             # ABSTRACT: Legacy BSON type wrapper for binary data (DEPRECATED)
7              
8 71     71   408 use version;
  71         155  
  71         294  
9             our $VERSION = 'v1.12.1';
10              
11             our $TYPE_SIMPLE = 0x00;
12             our $TYPE_BYTES = 0x02;
13             our $TYPE_UUID = 0x03;
14             our $TYPE_MD5 = 0x05;
15             our $TYPE_USER_DEFINED = 0x80;
16              
17             sub new {
18 5     5 0 2927 my ( $class, $data, $type ) = @_;
19 5   66     37 $type ||= $TYPE_SIMPLE;
20 5         17 my $self = bless { type => $type }, $class;
21 5         19 $self->data($data);
22 5         25 return $self;
23             }
24              
25             sub data {
26 15     15 0 993 my ( $self, $data ) = @_;
27 15 100       38 if ( defined $data ) {
28 5 100       26 $data = [ unpack( 'C*', $data ) ] unless ref $data eq 'ARRAY';
29 5         86 $self->{data} = $data;
30             }
31 15         62 return $self->{data};
32             }
33              
34             sub type {
35 6     6 0 933 return $_[0]->{type};
36             }
37              
38             # alias for compatibility with BSON::Bytes
39             sub subtype {
40 4     4 0 11 return $_[0]->{type};
41             }
42              
43             sub to_s {
44 2     2 0 5 my $self = shift;
45 2         3 my @data = @{ $self->data };
  2         5  
46 2         6 return pack( 'ltype, @data );
47             }
48              
49             sub TO_JSON {
50 0     0 0   my %data;
51 0           tie( %data, 'Tie::IxHash' );
52 0           $data{base64} = $_[0]->to_s;
53 0           $data{subType} = $_[0]->{type};
54 0           return { '$binary' => \%data };
55             }
56              
57 71     71   28763 use overload '""' => \&to_s;
  71         173  
  71         525  
58              
59             1;
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             BSON::Binary - Legacy BSON type wrapper for binary data (DEPRECATED)
68              
69             =head1 VERSION
70              
71             version v1.12.1
72              
73             =head1 DESCRIPTION
74              
75             This module has been deprecated as it was horribly inefficient (unpacking
76             binary data to individual single-byte elements of an array!) and had a
77             weird API that was not compatible with the existing MongoDB Binary wrapper
78             implementation on CPAN.
79              
80             You are strongly encouraged to use L instead.
81              
82             =for Pod::Coverage new data type subtype to_s TO_JSON
83              
84             =head1 AUTHORS
85              
86             =over 4
87              
88             =item *
89              
90             David Golden
91              
92             =item *
93              
94             Stefan G.
95              
96             =back
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
101              
102             This is free software, licensed under:
103              
104             The Apache License, Version 2.0, January 2004
105              
106             =cut
107              
108             __END__