BSON/Types.pm

filter: Statementstmt 99.1% 120 / 121 Branchbran 96.6% 29 / 30 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 47 / 47 Podpod 100% 18 / 18 totaltotal 99.0% 217 / 219 CC 17 SCAR 5.5
CC
17
Cov
99%
bson_oid
4.1
bson_bytes
3.0
bson_code
3.0
CRAP 1.7
« BSON/Bool.pm ↑ Summary BSON/Doc.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package BSON::Types;
6 # ABSTRACT: Helper functions to wrap BSON type classes
7  
8 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 our $VERSION = 'v1.12.2';
10  
11 71 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 our @EXPORT_OK = qw(
13 bson_bool
14 bson_bytes
15 bson_code
16 bson_dbref
17 bson_decimal128
18 bson_doc
19 bson_array
20 bson_double
21 bson_int32
22 bson_int64
23 bson_maxkey
24 bson_minkey
25 bson_oid
26 bson_raw
27 bson_regex
28 bson_string
29 bson_time
30 bson_timestamp
31 );
32 71 our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );
33  
34 71 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 71 ▶use boolean; # bson_bool
Subroutine BEGIN: called CC=1 SCAR=0.0
37 71 ▶use BSON::Bytes; # bson_bytes
Subroutine BEGIN: called CC=1 SCAR=0.0
38 71 ▶use BSON::Code; # bson_code
Subroutine BEGIN: called CC=1 SCAR=0.0
39 71 ▶use BSON::DBRef; # bson_dbref
Subroutine BEGIN: called CC=1 SCAR=0.0
40 71 ▶use BSON::Decimal128; # bson_decimal128
Subroutine BEGIN: called CC=1 SCAR=0.0
41 71 ▶use BSON::Doc; # bson_doc
Subroutine BEGIN: called CC=1 SCAR=0.0
42 71 ▶use BSON::Array; # bson_array
Subroutine BEGIN: called CC=1 SCAR=0.0
43 71 ▶use BSON::Double; # bson_double
Subroutine BEGIN: called CC=1 SCAR=0.0
44 71 ▶use BSON::Int32; # bson_int32
Subroutine BEGIN: called CC=1 SCAR=0.0
45 71 ▶use BSON::Int64; # bson_int64
Subroutine BEGIN: called CC=1 SCAR=0.0
46 71 ▶use BSON::MaxKey; # bson_maxkey
Subroutine BEGIN: called CC=1 SCAR=0.0
47 71 ▶use BSON::MinKey; # bson_minkey
Subroutine BEGIN: called CC=1 SCAR=0.0
48 71 ▶use BSON::OID; # bson_oid
Subroutine BEGIN: called CC=1 SCAR=0.0
49 71 ▶use BSON::Raw; # bson_raw
Subroutine BEGIN: called CC=1 SCAR=0.0
50 71 ▶use BSON::Regex; # bson_regex
Subroutine BEGIN: called CC=1 SCAR=0.0
51 71 ▶use BSON::String; # bson_string
Subroutine BEGIN: called CC=1 SCAR=0.0
52 71 ▶use BSON::Time; # bson_time
Subroutine BEGIN: called CC=1 SCAR=0.0
53 71 ▶use BSON::Timestamp; # bson_timestamp
Subroutine BEGIN: called CC=1 SCAR=0.0
54 71 ▶use BSON::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 71 ▶use BSON::DBPointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # deprecated, but load anyway
58 71 ▶use BSON::Bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 71 ▶use BSON::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 71 ▶use BSON::ObjectId;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 #pod =func bson_bytes
63 #pod
64 #pod $bytes = bson_bytes( $byte_string );
65 #pod $bytes = bson_bytes( $byte_string, $subtype );
66 #pod
67 #pod This function returns a L<BSON::Bytes> object wrapping the provided string.
68 #pod A numeric subtype may be provided as a second argument, but this is not
69 #pod recommended for new applications.
70 #pod
71 #pod =cut
72  
73 7 ▶sub bson_bytes {
Subroutine bson_bytes: called CC=3 SCAR=11.0
Pod documented
74 7 ▶ return BSON::Bytes->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 6 1 7
Condition100%2/2
$_[1] || 0
l!l
25
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
75 data => ( defined( $_[0] ) ? $_[0] : '' ),
76 subtype => ( $_[1] || 0 ),
77 );
78 }
79  
80 #pod =func bson_code
81 #pod
82 #pod $code = bson_code( $javascript );
83 #pod $code = bson_code( $javascript, $hashref );
84 #pod
85 #pod This function returns a L<BSON::Code> object wrapping the provided Javascript
86 #pod code. An optional hashref representing variables in scope for the function
87 #pod may be given as well.
88 #pod
89 #pod =cut
90  
91 11 ▶sub bson_code {
Subroutine bson_code: called CC=3 SCAR=11.0
Pod documented
92 11 ▶ return BSON::Code->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 3 8 11
93 8 ▶ return BSON::Code->new( code => $_[0] ) unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 4 4 8
94 4 return BSON::Code->new( code => $_[0], scope => $_[1] );
95 }
96  
97 #pod =func bson_dbref
98 #pod
99 #pod $dbref = bson_dbref( $object_id, $collection_name );
100 #pod
101 #pod This function returns a L<BSON::DBRef> object wrapping the provided Object ID
102 #pod and collection name.
103 #pod
104 #pod =cut
105  
106 4 ▶sub bson_dbref {
Subroutine bson_dbref: called CC=2 SCAR=6.9
Pod documented
107 4 ▶ croak "Arguments to bson_dbref must an id and collection name"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 3 1 4
108 unless @_ == 2;
109 1 return BSON::DBRef->new( id => $_[0], ref => $_[1] );
110 }
111  
112 #pod =func bson_decimal128
113 #pod
114 #pod $decimal = bson_decimal128( "0.12" );
115 #pod $decimal = bson_decimal128( "1.23456789101112131415116E-412" );
116 #pod
117 #pod This function returns a L<BSON::Decimal128> object wrapping the provided
118 #pod decimal B<string>. Unlike floating point values, this preserves exact
119 #pod decimal precision.
120 #pod
121 #pod =cut
122  
123 142 ▶sub bson_decimal128 {
Subroutine bson_decimal128: called CC=2 SCAR=6.9
Pod documented
124 142 ▶ return BSON::Decimal128->new( value => defined $_[0] ? $_[0] : 0 )
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 141 1 142
125 }
126  
127 #pod =func bson_doc
128 #pod
129 #pod $doc = bson_doc( first => "hello, second => "world" );
130 #pod
131 #pod This function returns a L<BSON::Doc> object, which preserves the order
132 #pod of the provided key-value pairs.
133 #pod
134 #pod =cut
135  
136 59 ▶sub bson_doc {
Subroutine bson_doc: called CC=1 SCAR=0.0
Pod documented
137 59 return BSON::Doc->new( @_ );
138 }
139  
140 #pod =func bson_array
141 #pod
142 #pod $doc = bson_array(...);
143 #pod
144 #pod This function returns a L<BSON::Array> object, which preserves the order
145 #pod of the provided list of elements.
146 #pod
147 #pod =cut
148  
149 1 ▶sub bson_array {
Subroutine bson_array: called CC=1 SCAR=0.0
Pod documented
150 1 return BSON::Array->new( @_ );
151 }
152  
153 #pod =func bson_double
154 #pod
155 #pod $double = bson_double( 1.0 );
156 #pod
157 #pod This function returns a L<BSON::Double> object wrapping a native
158 #pod double value. This ensures it serializes to BSON as a double rather
159 #pod than a string or integer given Perl's lax typing for scalars.
160 #pod
161 #pod =cut
162  
163 14 ▶sub bson_double {
Subroutine bson_double: called CC=1 SCAR=0.0
Pod documented
164 14 return BSON::Double->new( value => $_[0] )
165 }
166  
167 #pod =func bson_int32
168 #pod
169 #pod $int32 = bson_int32( 42 );
170 #pod
171 #pod This function returns a L<BSON::Int32> object wrapping a native
172 #pod integer value. This ensures it serializes to BSON as an Int32 rather
173 #pod than a string or double given Perl's lax typing for scalars.
174 #pod
175 #pod =cut
176  
177 14 ▶sub bson_int32 {
Subroutine bson_int32: called CC=2 SCAR=6.9
Pod documented
178 14 ▶ return BSON::Int32->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 13 14
179 13 return BSON::Int32->new( value => $_[0] )
180 }
181  
182 #pod =func bson_int64
183 #pod
184 #pod $int64 = bson_int64( 0 ); # 64-bit zero
185 #pod
186 #pod This function returns a L<BSON::Int64> object, wrapping a native
187 #pod integer value. This ensures it serializes to BSON as an Int64 rather
188 #pod than a string or double given Perl's lax typing for scalars.
189 #pod
190 #pod =cut
191  
192 18 ▶sub bson_int64 {
Subroutine bson_int64: called CC=2 SCAR=6.9
Pod documented
193 18 ▶ return BSON::Int64->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 17 18
194 17 return BSON::Int64->new( value => $_[0] )
195 }
196  
197 #pod =func bson_maxkey
198 #pod
199 #pod $maxkey = bson_maxkey();
200 #pod
201 #pod This function returns a singleton representing the "maximum key"
202 #pod BSON type.
203 #pod
204 #pod =cut
205  
206 4 ▶sub bson_maxkey {
Subroutine bson_maxkey: called CC=1 SCAR=0.0
Pod documented
207 4 return BSON::MaxKey->new;
208 }
209  
210 #pod =func bson_minkey
211 #pod
212 #pod $minkey = bson_minkey();
213 #pod
214 #pod This function returns a singleton representing the "minimum key"
215 #pod BSON type.
216 #pod
217 #pod =cut
218  
219 4 ▶sub bson_minkey {
Subroutine bson_minkey: called CC=1 SCAR=0.0
Pod documented
220 4 return BSON::MinKey->new;
221 }
222  
223 #pod =func bson_oid
224 #pod
225 #pod $oid = bson_oid(); # generate a new one
226 #pod $oid = bson_oid( $bytes ); # from 12-byte packed OID
227 #pod $oid = bson_oid( $hex ); # from 24 hex characters
228 #pod
229 #pod This function returns a L<BSON::OID> object wrapping a 12-byte MongoDB Object
230 #pod ID. With no arguments, a new, unique Object ID is generated instead. If
231 #pod 24 hexadecimal characters are given, they will be packed into a 12-byte
232 #pod Object ID.
233 #pod
234 #pod =cut
235  
236 11 ▶sub bson_oid {
Subroutine bson_oid: called CC=4 SCAR=14.2
Pod documented
237 11 ▶ return BSON::OID->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 7 4 11
238 4 ▶ return BSON::OID->new( oid => $_[0] ) if length( $_[0] ) == 12;
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] == 12 3 1 4
239 1 ▶ return BSON::OID->new( oid => pack( "H*", $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\A[0-9a-f]{24}\z/i 1 0 1
240 if $_[0] =~ m{\A[0-9a-f]{24}\z}i;
241 0 croak "Arguments to bson_oid must be 12 packed bytes or 24 bytes of hex";
242 }
243  
244 #pod =func bson_raw
245 #pod
246 #pod $raw = bson_raw( $bson_encoded );
247 #pod
248 #pod This function returns a L<BSON::Raw> object wrapping an already BSON-encoded
249 #pod document.
250 #pod
251 #pod =cut
252  
253 3 ▶sub bson_raw {
Subroutine bson_raw: called CC=1 SCAR=0.0
Pod documented
254 3 return BSON::Raw->new( bson => $_[0] );
255 }
256  
257 #pod =func bson_regex
258 #pod
259 #pod $regex = bson_regex( $pattern );
260 #pod $regex = bson_regex( $pattern, $flags );
261 #pod
262 #pod This function returns a L<BSON::Regex> object wrapping a PCRE pattern and
263 #pod optional flags.
264 #pod
265 #pod =cut
266  
267 9 ▶sub bson_regex {
Subroutine bson_regex: called CC=3 SCAR=11.0
Pod documented
268 9 ▶ return BSON::Regex->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 3 6 9
269 6 ▶ return BSON::Regex->new( pattern => $_[0] ) unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 4 6
270 4 return BSON::Regex->new( pattern => $_[0], flags => $_[1] );
271 }
272  
273 #pod =func bson_string
274 #pod
275 #pod $string = bson_string( "08544" );
276 #pod
277 #pod This function returns a L<BSON::String> object, wrapping a native
278 #pod string value. This ensures it serializes to BSON as a UTF-8 string rather
279 #pod than an integer or double given Perl's lax typing for scalars.
280 #pod
281 #pod =cut
282  
283 5 ▶sub bson_string {
Subroutine bson_string: called CC=1 SCAR=0.0
Pod documented
284 5 return BSON::String->new( value => $_[0] );
285 }
286  
287 #pod =func bson_time
288 #pod
289 #pod $time = bson_time( $seconds_from_epoch );
290 #pod
291 #pod This function returns a L<BSON::Time> object representing a UTC date and
292 #pod time to millisecond precision. The argument must be given as a number of
293 #pod seconds relative to the Unix epoch (positive or negative). The number may
294 #pod be a floating point value for fractional seconds. If no argument is
295 #pod provided, the current time from L<Time::HiRes> is used.
296 #pod
297 #pod =cut
298  
299 9 ▶sub bson_time {
Subroutine bson_time: called CC=2 SCAR=6.9
Pod documented
300 9 ▶ return BSON::Time->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 4 5 9
301 # Old constructor format handles floating point math right on
302 # 32-bit platforms.
303 5 return BSON::Time->new( $_[0] );
304 }
305  
306 #pod =func bson_timestamp
307 #pod
308 #pod $timestamp = bson_timestamp( $seconds_from_epoch, $increment );
309 #pod
310 #pod This function returns a L<BSON::Timestamp> object. It is not recommended
311 #pod for general use.
312 #pod
313 #pod =cut
314  
315 26 ▶sub bson_timestamp {
Subroutine bson_timestamp: called CC=3 SCAR=11.0
Pod documented
316 26 ▶ return BSON::Timestamp->new unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 3 23 26
317 23 ▶ return BSON::Timestamp->new( seconds => $_[0] ) unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 2 21 23
318 21 return BSON::Timestamp->new( seconds => $_[0], increment => $_[1] );
319 }
320  
321 #pod =func bson_bool (DISCOURAGED)
322 #pod
323 #pod # for consistency with other helpers
324 #pod $bool = bson_bool( $expression );
325 #pod
326 #pod # preferred for efficiency
327 #pod use boolean;
328 #pod $bool = boolean( $expression );
329 #pod
330 #pod This function returns a L<boolean> object (true or false) based on the
331 #pod provided expression (or false if no expression is provided). It is
332 #pod provided for consistency so that all BSON types have a corresponding helper
333 #pod function.
334 #pod
335 #pod For efficiency, use C<boolean::boolean()> directly, instead.
336 #pod
337 #pod =cut
338  
339 6 ▶sub bson_bool {
Subroutine bson_bool: called CC=1 SCAR=0.0
Pod documented
340 6 return boolean($_[0]);
341 }
342  
343 71 1;
344  
345 =pod
346  
347 =encoding UTF-8
348  
349 =head1 NAME
350  
351 BSON::Types - Helper functions to wrap BSON type classes
352  
353 =head1 VERSION
354  
355 version v1.12.2
356  
357 =head1 SYNOPSIS
358  
359 use BSON::Types ':all';
360  
361 $int32 = bson_int32(42);
362 $double = bson_double(3.14159);
363 $decimal = bson_decimal("24.01");
364 $time = bson_time(); # now
365 ...
366  
367 =head1 DESCRIPTION
368  
369 This module provides helper functions for BSON type wrappers. Type
370 wrappers use objects corresponding to BSON types to represent data that
371 would have ambiguous type or don't have a native Perl representation
372  
373 For example, because Perl scalars can represent strings, integers or
374 floating point numbers, the serialization rules depend on various
375 heuristics. By wrapping a Perl scalar with a class, such as
376 L<BSON::Int32>, users can specify exactly how a scalar should serialize to
377 BSON.
378  
379 =head1 FUNCTIONS
380  
381 =head2 bson_bytes
382  
383 $bytes = bson_bytes( $byte_string );
384 $bytes = bson_bytes( $byte_string, $subtype );
385  
386 This function returns a L<BSON::Bytes> object wrapping the provided string.
387 A numeric subtype may be provided as a second argument, but this is not
388 recommended for new applications.
389  
390 =head2 bson_code
391  
392 $code = bson_code( $javascript );
393 $code = bson_code( $javascript, $hashref );
394  
395 This function returns a L<BSON::Code> object wrapping the provided Javascript
396 code. An optional hashref representing variables in scope for the function
397 may be given as well.
398  
399 =head2 bson_dbref
400  
401 $dbref = bson_dbref( $object_id, $collection_name );
402  
403 This function returns a L<BSON::DBRef> object wrapping the provided Object ID
404 and collection name.
405  
406 =head2 bson_decimal128
407  
408 $decimal = bson_decimal128( "0.12" );
409 $decimal = bson_decimal128( "1.23456789101112131415116E-412" );
410  
411 This function returns a L<BSON::Decimal128> object wrapping the provided
412 decimal B<string>. Unlike floating point values, this preserves exact
413 decimal precision.
414  
415 =head2 bson_doc
416  
417 $doc = bson_doc( first => "hello, second => "world" );
418  
419 This function returns a L<BSON::Doc> object, which preserves the order
420 of the provided key-value pairs.
421  
422 =head2 bson_array
423  
424 $doc = bson_array(...);
425  
426 This function returns a L<BSON::Array> object, which preserves the order
427 of the provided list of elements.
428  
429 =head2 bson_double
430  
431 $double = bson_double( 1.0 );
432  
433 This function returns a L<BSON::Double> object wrapping a native
434 double value. This ensures it serializes to BSON as a double rather
435 than a string or integer given Perl's lax typing for scalars.
436  
437 =head2 bson_int32
438  
439 $int32 = bson_int32( 42 );
440  
441 This function returns a L<BSON::Int32> object wrapping a native
442 integer value. This ensures it serializes to BSON as an Int32 rather
443 than a string or double given Perl's lax typing for scalars.
444  
445 =head2 bson_int64
446  
447 $int64 = bson_int64( 0 ); # 64-bit zero
448  
449 This function returns a L<BSON::Int64> object, wrapping a native
450 integer value. This ensures it serializes to BSON as an Int64 rather
451 than a string or double given Perl's lax typing for scalars.
452  
453 =head2 bson_maxkey
454  
455 $maxkey = bson_maxkey();
456  
457 This function returns a singleton representing the "maximum key"
458 BSON type.
459  
460 =head2 bson_minkey
461  
462 $minkey = bson_minkey();
463  
464 This function returns a singleton representing the "minimum key"
465 BSON type.
466  
467 =head2 bson_oid
468  
469 $oid = bson_oid(); # generate a new one
470 $oid = bson_oid( $bytes ); # from 12-byte packed OID
471 $oid = bson_oid( $hex ); # from 24 hex characters
472  
473 This function returns a L<BSON::OID> object wrapping a 12-byte MongoDB Object
474 ID. With no arguments, a new, unique Object ID is generated instead. If
475 24 hexadecimal characters are given, they will be packed into a 12-byte
476 Object ID.
477  
478 =head2 bson_raw
479  
480 $raw = bson_raw( $bson_encoded );
481  
482 This function returns a L<BSON::Raw> object wrapping an already BSON-encoded
483 document.
484  
485 =head2 bson_regex
486  
487 $regex = bson_regex( $pattern );
488 $regex = bson_regex( $pattern, $flags );
489  
490 This function returns a L<BSON::Regex> object wrapping a PCRE pattern and
491 optional flags.
492  
493 =head2 bson_string
494  
495 $string = bson_string( "08544" );
496  
497 This function returns a L<BSON::String> object, wrapping a native
498 string value. This ensures it serializes to BSON as a UTF-8 string rather
499 than an integer or double given Perl's lax typing for scalars.
500  
501 =head2 bson_time
502  
503 $time = bson_time( $seconds_from_epoch );
504  
505 This function returns a L<BSON::Time> object representing a UTC date and
506 time to millisecond precision. The argument must be given as a number of
507 seconds relative to the Unix epoch (positive or negative). The number may
508 be a floating point value for fractional seconds. If no argument is
509 provided, the current time from L<Time::HiRes> is used.
510  
511 =head2 bson_timestamp
512  
513 $timestamp = bson_timestamp( $seconds_from_epoch, $increment );
514  
515 This function returns a L<BSON::Timestamp> object. It is not recommended
516 for general use.
517  
518 =head2 bson_bool (DISCOURAGED)
519  
520 # for consistency with other helpers
521 $bool = bson_bool( $expression );
522  
523 # preferred for efficiency
524 use boolean;
525 $bool = boolean( $expression );
526  
527 This function returns a L<boolean> object (true or false) based on the
528 provided expression (or false if no expression is provided). It is
529 provided for consistency so that all BSON types have a corresponding helper
530 function.
531  
532 For efficiency, use C<boolean::boolean()> directly, instead.
533  
534 =for Pod::Coverage BUILD
535  
536 =head1 AUTHORS
537  
538 =over 4
539  
540 =item *
541  
542 David Golden <david@mongodb.com>
543  
544 =item *
545  
546 Stefan G. <minimalist@lavabit.com>
547  
548 =back
549  
550 =head1 COPYRIGHT AND LICENSE
551  
552 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
553  
554 This is free software, licensed under:
555  
556 The Apache License, Version 2.0, January 2004
557  
558 =cut
559  
560 __END__
« BSON/Bool.pm ↑ Summary BSON/Doc.pm »