File Coverage

blib/lib/BSON/Int64.pm
Criterion Covered Total %
statement 50 76 65.7
branch 13 46 28.2
condition 10 12 83.3
subroutine 14 20 70.0
pod 1 1 100.0
total 88 155 56.7


line stmt bran cond sub pod time code
1 71     71   28969 use 5.010001;
  71         215  
2 71     71   316 use strict;
  71         107  
  71         1386  
3 71     71   321 use warnings;
  71         115  
  71         2264  
4              
5             package BSON::Int64;
6             # ABSTRACT: BSON type wrapper for Int64
7              
8 71     71   331 use version;
  71         117  
  71         302  
9             our $VERSION = 'v1.12.2';
10              
11 71     71   5001 use Carp;
  71         180  
  71         4597  
12 71     71   415 use Config;
  71         116  
  71         2844  
13 71     71   358 use Moo;
  71         122  
  71         386  
14              
15             #pod =attr value
16             #pod
17             #pod A numeric scalar. It will be coerced to an integer. The default is 0.
18             #pod
19             #pod =cut
20              
21             has 'value' => (
22             is => 'ro'
23             );
24              
25 71     71   28493 use if !$Config{use64bitint}, "Math::BigInt";
  71         359  
  71         1403  
26              
27 71     71   3733 use namespace::clean -except => 'meta';
  71         142  
  71         407  
28              
29             # With long doubles or a 32-bit integer perl, we're able to directly check
30             # if a value exceeds the maximum bounds of an int64_t. On a 64-bit Perl
31             # with only regular doubles, the loss of precision for doubles makes an
32             # exact check against the negative boundary impossible from pure-Perl.
33             # (The positive boundary isn't an issue because Perl will upgrade
34             # internally to uint64_t to do the comparision). Fortunately, we can take
35             # advantage of a quirk in pack(), where a float that is in the ambiguous
36             # negative zone or that is too negative to fit will get packed like the
37             # smallest negative int64_t.
38              
39             BEGIN {
40 71 50   71   38151 my $max_int64 = $Config{use64bitint} ? 9223372036854775807 : Math::BigInt->new("9223372036854775807");
41 71 50       481 my $min_int64 = $Config{use64bitint} ? -9223372036854775808 : Math::BigInt->new("-9223372036854775808");
42 71 50 33     5017 if ( $Config{nvsize} == 16 || ! $Config{use64bitint} ) {
43             *BUILD = sub {
44 0         0 my $self = shift;
45              
46 0 0       0 my $value = defined $self->{value} ? int($self->{value}) : 0;
47              
48 0 0       0 if ( $value > $max_int64 ) {
    0          
49 0         0 $value = $max_int64;
50             }
51             elsif ( $value < $min_int64 ) {
52 0         0 $value = $min_int64;
53             }
54              
55 0         0 return $self->{value} = $value;
56             }
57 0         0 }
58             else {
59 71         465 my $packed_min_int64 = pack("q<", $min_int64);
60             *BUILD = sub {
61 67     67   6349 my $self = shift;
62              
63 67 100       248 my $value = defined $self->{value} ? int($self->{value}) : 0;
64              
65 67 100 100     686 if ( $value >= 0 && $value > $max_int64 ) {
    100 100        
66 4         347 $value = $max_int64;
67             }
68             elsif ( $value < 0 && pack("q<", $value) eq $packed_min_int64 ) {
69 9         312 $value = $min_int64;
70             }
71              
72 67         1447 return $self->{value} = $value;
73             }
74 71         28572 }
75             }
76              
77             #pod =method TO_JSON
78             #pod
79             #pod On a 64-bit perl, returns the value as an integer. On a 32-bit Perl, it
80             #pod will be returned as a Math::BigInt object, which will
81             #pod fail to serialize unless a C method is defined
82             #pod for that or in package C.
83             #pod
84             #pod If the C environment variable is true and the
85             #pod C environment variable is false, returns a hashref
86             #pod compatible with
87             #pod MongoDB's L
88             #pod format, which represents it as a document as follows:
89             #pod
90             #pod {"$numberLong" : "223372036854775807"}
91             #pod
92             #pod =cut
93              
94             sub TO_JSON {
95 21 100 100 21 1 280 return int($_[0]->{value}) if ! $ENV{BSON_EXTJSON} || $ENV{BSON_EXTJSON_RELAXED};
96 14         45 return { '$numberLong' => "$_[0]->{value}" };
97             }
98              
99             use overload (
100             # Unary
101 0     0   0 q{""} => sub { "$_[0]->{value}" },
102 30     30   2726 q{0+} => sub { $_[0]->{value} },
103 0     0   0 q{~} => sub { ~( $_[0]->{value} ) },
104             # Binary
105 142     0   10802 ( map { $_ => eval "sub { return \$_[0]->{value} $_ \$_[1] }" } qw( + * ) ), ## no critic
  0         0  
  0         0  
106             (
107             map {
108 852 0   0   66237 $_ => eval ## no critic
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 50       0  
  12 50       354  
  20 0       201  
  0 0       0  
  0         0  
109             "sub { return \$_[2] ? \$_[1] $_ \$_[0]->{value} : \$_[0]->{value} $_ \$_[1] }"
110             } qw( - / % ** << >> x <=> cmp & | ^ )
111             ),
112             (
113 426     0   24528 map { $_ => eval "sub { return $_(\$_[0]->{value}) }" } ## no critic
  0         0  
  6         19  
  9         28  
  0            
  0            
  0            
114             qw( cos sin exp log sqrt int )
115             ),
116             q{atan2} => sub {
117 0 0   0   0 return $_[2] ? atan2( $_[1], $_[0]->{value} ) : atan2( $_[0]->{value}, $_[1] );
118             },
119              
120             # Special
121 71         385 fallback => 1,
122 71     71   501 );
  71         120  
123              
124             1;
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             BSON::Int64 - BSON type wrapper for Int64
133              
134             =head1 VERSION
135              
136             version v1.12.2
137              
138             =head1 SYNOPSIS
139              
140             use BSON::Types ':all';
141              
142             bson_int64( $number );
143              
144             =head1 DESCRIPTION
145              
146             This module provides a BSON type wrapper for a numeric value that
147             would be represented in BSON as a 64-bit integer.
148              
149             If the value won't fit in a 64-bit integer, an error will be thrown.
150              
151             On a Perl without 64-bit integer support, the value must be a
152             L object.
153              
154             =head1 ATTRIBUTES
155              
156             =head2 value
157              
158             A numeric scalar. It will be coerced to an integer. The default is 0.
159              
160             =head1 METHODS
161              
162             =head2 TO_JSON
163              
164             On a 64-bit perl, returns the value as an integer. On a 32-bit Perl, it
165             will be returned as a Math::BigInt object, which will
166             fail to serialize unless a C method is defined
167             for that or in package C.
168              
169             If the C environment variable is true and the
170             C environment variable is false, returns a hashref
171             compatible with
172             MongoDB's L
173             format, which represents it as a document as follows:
174              
175             {"$numberLong" : "223372036854775807"}
176              
177             =for Pod::Coverage BUILD
178              
179             =head1 OVERLOADING
180              
181             The numification operator, C<0+> is overloaded to return the C,
182             the full "minimal set" of overloaded operations is provided (per L
183             documentation) and fallback overloading is enabled.
184              
185             =head1 AUTHORS
186              
187             =over 4
188              
189             =item *
190              
191             David Golden
192              
193             =item *
194              
195             Stefan G.
196              
197             =back
198              
199             =head1 COPYRIGHT AND LICENSE
200              
201             This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
202              
203             This is free software, licensed under:
204              
205             The Apache License, Version 2.0, January 2004
206              
207             =cut
208              
209             __END__