File Coverage

blib/lib/BSON/String.pm
Criterion Covered Total %
statement 37 60 61.6
branch 8 34 23.5
condition n/a
subroutine 11 18 61.1
pod 1 2 50.0
total 57 114 50.0


line stmt bran cond sub pod time code
1 71     71   27470 use 5.010001;
  71         248  
2 71     71   361 use strict;
  71         119  
  71         1378  
3 71     71   322 use warnings;
  71         126  
  71         2309  
4              
5             package BSON::String;
6             # ABSTRACT: BSON type wrapper for strings
7              
8 71     71   340 use version;
  71         129  
  71         279  
9             our $VERSION = 'v1.12.1';
10              
11 71     71   5693 use Moo;
  71         178  
  71         402  
12              
13             #pod =attr value
14             #pod
15             #pod A scalar value, which will be stringified during construction. The default
16             #pod is the empty string.
17             #pod
18             #pod =cut
19              
20             has 'value' => (
21             is => 'ro'
22             );
23              
24 71     71   23117 use namespace::clean -except => 'meta';
  71         169  
  71         527  
25              
26             sub BUILDARGS {
27 27358     27358 0 6116200 my $class = shift;
28 27358         41756 my $n = scalar(@_);
29              
30 27358         39056 my %args;
31 27358 100       69121 if ( $n == 0 ) {
    100          
    50          
32 1         4 $args{value} = '';
33             }
34             elsif ( $n == 1 ) {
35 27334         50899 $args{value} = shift;
36             }
37             elsif ( $n % 2 == 0 ) {
38 23         69 %args = @_;
39 23 100       119 $args{value} = '' unless defined $args{value};
40             }
41             else {
42 0         0 croak("Invalid number of arguments ($n) to BSON::String::new");
43             }
44              
45             # normalize all to internal PV type
46 27358         58036 $args{value} = "$args{value}";
47              
48 27358         426757 return \%args;
49             }
50              
51             #pod =method TO_JSON
52             #pod
53             #pod Returns value as a string.
54             #pod
55             #pod =cut
56              
57 6     6 1 14 sub TO_JSON { return "$_[0]->{value}" }
58              
59             use overload (
60             # Unary
61 1     1   104 q{bool} => sub { !! $_[0]->{value} },
62 44743     44743   9429710 q{""} => sub { $_[0]->{value} },
63 0     0   0 q{0+} => sub { 0+ $_[0]->{value} },
64 0     0   0 q{~} => sub { ~( $_[0]->{value} ) },
65             # Binary
66 142     0   11544 ( map { $_ => eval "sub { return \$_[0]->{value} $_ \$_[1] }" } qw( + * ) ), ## no critic
  0     0   0  
  0         0  
67             (
68             map {
69 852 0   0   69056 $_ => 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 0       0  
  0 50       0  
  3 0       829  
  0 0          
  0            
70             "sub { return \$_[2] ? \$_[1] $_ \$_[0]->{value} : \$_[0]->{value} $_ \$_[1] }"
71             } qw( - / % ** << >> x <=> cmp & | ^ )
72             ),
73             (
74 426     0   26622 map { $_ => eval "sub { return $_(\$_[0]->{value}) }" } ## no critic
  0            
  0            
  0            
  0            
  0            
  0            
75             qw( cos sin exp log sqrt int )
76             ),
77             q{atan2} => sub {
78 0 0   0   0 return $_[2] ? atan2( $_[1], $_[0]->{value} ) : atan2( $_[0]->{value}, $_[1] );
79             },
80              
81             # Special
82 71         672 fallback => 1,
83 71     71   49046 );
  71         184  
84              
85             1;
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             BSON::String - BSON type wrapper for strings
94              
95             =head1 VERSION
96              
97             version v1.12.1
98              
99             =head1 SYNOPSIS
100              
101             use BSON::Types ':all';
102              
103             bson_string( $string );
104              
105             =head1 DESCRIPTION
106              
107             This module provides a BSON type wrapper for a string value.
108              
109             Since Perl does not distinguish between numbers and strings, this module
110             provides an explicit string type for a scalar value.
111              
112             =head1 ATTRIBUTES
113              
114             =head2 value
115              
116             A scalar value, which will be stringified during construction. The default
117             is the empty string.
118              
119             =head1 METHODS
120              
121             =head2 TO_JSON
122              
123             Returns value as a string.
124              
125             =for Pod::Coverage BUILDARGS
126              
127             =head1 OVERLOADING
128              
129             The stringification operator (C<"">) is overloaded to return the C,
130             the full "minimal set" of overloaded operations is provided (per L
131             documentation) and fallback overloading is enabled.
132              
133             =head1 AUTHORS
134              
135             =over 4
136              
137             =item *
138              
139             David Golden
140              
141             =item *
142              
143             Stefan G.
144              
145             =back
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
150              
151             This is free software, licensed under:
152              
153             The Apache License, Version 2.0, January 2004
154              
155             =cut
156              
157             __END__