Money.pm

filter: Statementstmt 93.3% 182 / 195 Branchbran 81.7% 67 / 82 Conditioncond 36.1% 13 / 36 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 93.6% 44 / 47 Podpod 77.7% 14 / 18 totaltotal 80.4% 321 / 399 CC 56 SCAR 13.5
CC
56
Cov
84%
clone
11.5
__ANON__
7.5
stringify
6.5
CRAP 3.8
↑ Summary Money/BaseException/InvalidCurrencyCode.pm »
1 package Data::Money;
2  
3 8 $Data::Money::VERSION = '0.21';
4 8 $Data::Money::AUTHORITY = 'cpan:GPHAT';
5  
6 =head1 NAME
7  
8 Data::Money - Money/currency with formatting and overloading.
9  
10 =head1 VERSION
11  
12 Version 0.21
13  
14 =cut
15  
16 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Data::Money::BaseException::MismatchCurrencyType;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Data::Money::BaseException::ExcessivePrecision;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use Data::Money::BaseException::InvalidCurrencyCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Data::Money::BaseException::InvalidCurrencyFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use Locale::Currency::Format qw/:default/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use Locale::Currency qw(code2currency);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 use overload
31 8 ▶ '+' => \&add,
Subroutine BEGIN: called CC=1 SCAR=0.2
32 '-' => \&subtract,
33 '*' => \&multiply,
34 '/' => \&divide,
35 '%' => \&modulo,
36 '+=' => \&add_in_place,
37 '-=' => \&subtract_in_place,
38 '*=' => \&multiply_in_place,
39 '/=' => \&divide_in_place,
40 0 ▶ '0+' => sub { $_[0]->value->numify; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 1 ▶ '""' => sub { shift->stringify },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 10 ▶ 'bool' => sub { shift->as_int; },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 '<=>' => \&three_way_compare,
44 'cmp' => \&three_way_compare,
45 2 ▶ 'abs' => sub { shift->absolute },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 0 ▶ '=' => sub { shift->clone },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 8 'neg' => \&negate,
48 8 fallback => 1;
49  
50 416 ▶my $Amount = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
51 416 my ($arg) = @_;
52  
53 416 ▶ return Math::BigFloat->new(0) unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 416 416
54  
55 416 ▶ return Math::BigFloat->new($arg->value)
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg eq "Data::Money" 0 416 416
56 if (ref($arg) eq 'Data::Money');
57  
58 416 $arg =~ tr/-()0-9.//cd;
59 416 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 400 16 416
60 400 Math::BigFloat->new($arg);
61 } else {
62 16 Math::BigFloat->new(0);
63 }
64 8 };
65  
66 196 ▶my $CurrencyCode = sub {
Subroutine __ANON__: called CC=4 SCAR=20.1
67 196 my ($arg) = @_;
68  
69 196 ▶ Data::Money::BaseException::InvalidCurrencyCode->throw
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg or $arg =~ /^[A-Z]{3}$/msx and defined code2currency($arg) 0 196 196
Condition16%1/6
defined $arg or $arg =~ /^[A-Z]{3}$/msx and defined code2currency($arg)
l!l&&r!l&&!r
19600
$arg =~ /^[A-Z]{3}$/msx and defined code2currency($arg)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $arg or $arg =~ /^[A-Z]{3}$/msx and defined code2currency($arg)
defined $arg $arg =~ /^[A-Z]{3}$/msx defined code2currency($arg)
Truth table25%1/4
A or B and C
A= defined $argB= $arg =~ /^[A-Z]{3}$/msxC= defined code2currency($arg)
ABCresult
1XX1
0111
00X0
0100
70 unless (defined $arg
71 || ($arg =~ /^[A-Z]{3}$/mxs && defined code2currency($arg)));
72 8 };
73  
74 196 ▶my $Format = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
75 196 my ($arg) = @_;
76  
77 196 my $format = {
78 'FMT_COMMON' => 1,
79 'FMT_HTML' => 1,
80 'FMT_NAME' => 1,
81 'FMT_STANDARD' => 1,
82 'FMT_SYMBOL' => 1
83 };
84  
85 Data::Money::BaseException::InvalidCurrencyFormat->throw
86 196 ▶ unless (defined $arg || exists $format->{uc($arg)});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg or exists $$format{uc $arg} 0 196 196
Condition33%1/3
defined $arg or exists $$format{uc $arg}
l!l&&r!l&&!r
19600
MC/DC0%0/2
defined $arg or exists $$format{uc $arg}
defined $arg exists $$format{uc $arg}
Truth table33%1/3
A or B
A= defined $argB= exists $$format{uc $arg}
ABresult
1X1
011
000
87 8 };
88  
89 54 ▶has code => (is => 'rw', isa => $CurrencyCode, default => sub { 'USD' });
Subroutine __ANON__: called CC=1 SCAR=0.0
90 69 ▶has format => (is => 'rw', isa => $Format, default => sub { 'FMT_COMMON' });
Subroutine __ANON__: called CC=1 SCAR=0.0
91 3 ▶has value => (is => 'rw', isa => $Amount, default => sub { Math::BigFloat->new(0) }, coerce => $Amount);
Subroutine __ANON__: called CC=1 SCAR=0.0
92  
93 =head1 DESCRIPTION
94  
95 The C<Data::Money> module provides basic currency formatting and number handling
96 via L<Math::BigFloat>:
97  
98 my $currency = Data::Money->new(value => 1.23);
99  
100 Each C<Data::Money> object will stringify to the original value except in string
101 context, where it stringifies to the format specified in C<format>.
102  
103 =head1 MOTIVATION
104  
105 Data::Money was created to make it easy to use different currencies (leveraging
106 existing work in C<Locale::Currency> and L<Moo|Moo>), to allow math operations
107 with proper rounding (via L<Math::BigFloat>) and formatting via L<Locale::Currency::Format>.
108  
109 =head1 SYNOPSIS
110  
111 use strict; use warnings;
112 use Data::Money;
113  
114 my $price = Data::Money->new(value => 1.2, code => 'USD');
115 print $price; # $1.20
116 print $price->code; # USD
117 print $price->format; # FMT_COMMON
118 print $price->as_string; # $1.20
119  
120 # Overloading, returns new instance
121 my $m2 = $price + 1;
122 my $m3 = $price - 1;
123 my $m4 = $price * 1;
124 my $m5 = $price / 1;
125 my $m6 = $price % 1;
126  
127 # Objects work too
128 my $m7 = $m2 + $m3;
129 my $m8 = $m2 - $m3;
130 my $m9 = $m2 * $m3;
131 my $m10 = $m2 / $m3;
132  
133 # Modifies in place
134 $price += 1;
135 $price -= 1;
136 $price *= 1;
137 $price /= 1;
138  
139 # Compares against numbers
140 print "Currency > 2 \n" if ($m2 > 2);
141 print "Currency < 3 \n" if ($m2 < 3);
142 print "Currency == 2.2 \n" if ($m2 == 2.2);
143  
144 # And strings
145 print "Currency gt \$2.00 \n" if ($m2 gt '$2.00');
146 print "Currency lt \$3.00 \n" if ($m2 lt '$3.00');
147 print "Currency eq \$2.20 \n" if ($m2 eq '$2.20');
148  
149 # and objects
150 print "Currency m2 > m3 \n" if ($m2 > $m3);
151 print "Currency m3 lt m2 \n" if ($m3 lt $m2);
152  
153 print $price->as_string('FMT_SYMBOL'); # $1.20
154  
155 =cut
156  
157 196 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
158 196 my ($self) = @_;
159  
160 196 my $exp = 0;
161 196 my $dec = $self->value->copy->bmod(1);
162 196 ▶ $exp = $dec->exponent->babs if ($dec);
Branch: 1 branch
BranchTrueFalseTotal
if $dec 115 81 196
163 196 my $prec = Math::BigInt->new($self->_decimal_precision);
164  
165 195 ▶ Data::Money::BaseException::ExcessivePrecision->throw if ($exp > $prec);
Branch: 1 branch
BranchTrueFalseTotal
if $exp > $prec 3 192 195
166 }
167  
168 =head1 METHODS
169  
170 =head2 name()
171  
172 Returns C<Data::Money> object currency name.
173  
174 =head2 code($currency_code)
175  
176 Gets/sets the three letter currency code for the current currency object.Defaults
177 to USD.
178  
179 =head2 value()
180  
181 Returns the amount. Defaults to 0.
182  
183 =head2 format($string)
184  
185 Gets/sets the format to be used when C<as_string()> is called. See L<Locale::Currency::Format>
186 for the available formatting options. Defaults to C<FMT_COMMON>.
187  
188 =cut
189  
190 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
191 0 my ($self) = @_;
192  
193 0 my $name = code2currency($self->code);
194 ## Fix for older Locale::Currency w/mispelled Candian
195 0 $name =~ s/Candian/Canadian/ms;
196  
197 0 return $name;
198 }
199  
200 =head2 clone(%params)
201  
202 Returns a clone (new instance) of this C<Data::Money> object. You may optionally
203 specify some of the attributes to overwrite.
204  
205 $currency->clone({ value => 100 }); # Clones all fields but changes value to 100
206  
207 =cut
208  
209 127 ▶sub clone {
Subroutine clone: called CC=7 SCAR=24.4
Pod documented
210 127 my ($self, %param) = @_;
211  
212 127 ▶ $param{code} = $self->code unless (exists $param{code} && defined $param{code});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"code"} and defined $param{"code"} 127 0 127
Condition33%1/3
exists $param{"code"} and defined $param{"code"}
!ll&&!rl&&r
12700
MC/DC0%0/2
exists $param{"code"} and defined $param{"code"}
exists $param{"code"} defined $param{"code"}
Truth table33%1/3
A and B
A= exists $param{"code"}B= defined $param{"code"}
ABresult
0X0
100
111
213 127 ▶ $param{format} = $self->format unless (exists $param{format} && defined $param{format});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"format"} and defined $param{"format"} 127 0 127
Condition33%1/3
exists $param{"format"} and defined $param{"format"}
!ll&&!rl&&r
12700
MC/DC0%0/2
exists $param{"format"} and defined $param{"format"}
exists $param{"format"} defined $param{"format"}
Truth table33%1/3
A and B
A= exists $param{"format"}B= defined $param{"format"}
ABresult
0X0
100
111
214 127 ▶ $param{value} = $self->value unless (exists $param{value} && defined $param{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{"value"} and defined $param{"value"} 0 127 127
Condition33%1/3
exists $param{"value"} and defined $param{"value"}
!ll&&!rl&&r
00127
MC/DC0%0/2
exists $param{"value"} and defined $param{"value"}
exists $param{"value"} defined $param{"value"}
Truth table33%1/3
A and B
A= exists $param{"value"}B= defined $param{"value"}
ABresult
0X0
100
111
215 127 return $self->new(\%param);
216 }
217  
218 =head2 as_float()
219  
220 Returns C<Data::Money> object value without any formatting.
221  
222 =cut
223  
224 # Liberally jacked from Math::Currency
225 30 ▶sub as_float {
Subroutine as_float: called CC=1 SCAR=0.0
Pod documented
226 30 my ($self) = @_;
227  
228 30 return $self->value->copy->bfround(0 - $self->_decimal_precision)->bstr;
229 }
230  
231 =head2 as_int()
232  
233 Returns the object's value "in pennies" (in the US at least). It strips the value
234 of formatting using C<as_float()> and of any decimals.
235  
236 =cut
237  
238 # Liberally jacked from Math::Currency
239 10 ▶sub as_int {
Subroutine as_int: called CC=2 SCAR=6.9
Pod documented
240 10 my ($self) = @_;
241  
242 10 (my $str = $self->as_float) =~ s/\.//omsx;
243 10 $str =~ s/^(\-?)0+/$1/omsx;
244 10 ▶ return $str eq '' ? '0' : $str;
Branch: 1 branch
BranchTrueFalseTotal
$str eq '' ? : 3 7 10
245 }
246  
247 =head2 absolute()
248  
249 Returns a new C<Data::Money> object with the value set to the absolute value of
250 the original.
251  
252 =cut
253  
254 24 ▶sub absolute {
Subroutine absolute: called CC=1 SCAR=0.0
Pod documented
255 24 my ($self) = @_;
256  
257 24 return $self->clone(value => abs $self->value);
258 }
259  
260 =head2 negate()
261  
262 Performs the negation operation, returning a new C<Data::Money> object with the
263 opposite value (1 to -1, -2 to 2, etc).
264  
265 =cut
266  
267 7 ▶sub negate {
Subroutine negate: called CC=2 SCAR=6.9
Pod documented
268 7 my ($self) = @_;
269  
270 7 ▶ return $self->absolute if ($self->value < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->value < 0 4 3 7
271  
272 3 my $val = 0 - $self->value;
273 3 return $self->clone(value => $val);
274 }
275  
276 =head2 add($num)
277  
278 Adds the specified amount to this C<Data::Money> object and returns a new C<Data::Money>
279 object. You can supply either a number or a C<Data::Money> object. Note that this B<does not>
280 modify the existing object.
281  
282 =cut
283  
284 9 ▶sub add {
Subroutine add: called CC=4 SCAR=13.9
Pod documented
285 9 my $self = shift;
286 9 ▶ my $num = shift || 0;
Condition50%1/2
shift() || 0
l!l
90
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
287  
288 9 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) 3 6 9
289 3 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 2 1 3
290 if ($self->code ne $num->code);
291  
292 1 return $self->clone(value => $self->value->copy->badd($num->value));
293 }
294  
295 6 return $self->clone(value => $self->value->copy->badd($self->clone(value => $num)->value))
296 }
297  
298 =head2 add_in_place($num)
299  
300 Adds the specified amount to this C<Data::Money> object, modifying its value. You
301 can supply either a number or a C<Data::Money> object. Note that this B<does>
302 modify the existing object.
303  
304 =cut
305  
306 3 ▶sub add_in_place {
Subroutine add_in_place: called CC=3 SCAR=11.0
Pod documented
307 3 my ($self, $num) = @_;
308  
309 3 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) { } 2 1 3
310 2 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 1 2
311 if ($self->code ne $num->code);
312  
313 1 $self->value($self->value->copy->badd($num->value));
314 } else {
315 1 $self->value($self->value->copy->badd($self->clone(value => $num)->value));
316 }
317  
318 2 return $self;
319 }
320  
321 =head2 as_string()
322  
323 Returns C<Data::Money> object as string.There is an alias C<stringify()> as well.
324  
325 =cut
326  
327 8 *as_string = \&stringify;
328 18 ▶sub stringify {
Subroutine stringify: called CC=5 SCAR=18.7
Pod undocumented
329 18 my $self = shift;
330 18 ▶ my $format = shift || $self->format;
Condition33%1/3
shift() || $self->format
l!l&&r!l&&!r
0180
MC/DC0%0/2
shift() || $self->format
shift() $self->format
Truth table33%1/3
A || B
A= shift()B= $self->format
ABresult
1X1
011
000
331  
332 ## funky eval to get string versions of constants back into the values
333 18 eval '$format = Locale::Currency::Format::' . $format;
334  
335 18 my $code = $self->code;
336 18 ▶ Data::Money::BaseException::InvalidCurrencyCode->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless _is_CurrencyCode($code) 0 18 18
Condition0%0/2
$code || 'undef'
l!l
00
MC/DC0%0/1
$code || 'undef'
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
337 {
338 error => 'Invalid currency code: ' . ($code || 'undef')
339 })
340 unless (_is_CurrencyCode($code));
341  
342 18 my $utf8 = _to_utf8(
343 currency_format($code, $self->absolute->as_float, $format)
344 );
345  
346 18 ▶ if ($self->value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->value < 0) { } 0 18 18
347 0 return "-$utf8";
348 } else {
349 18 return $utf8;
350 }
351 }
352  
353 =head2 substract($num)
354  
355 Subtracts the specified amount to this C<Data::Money> object and returns a new
356 C<Data::Money> object. You can supply either a number or a C<Data::Money> object.
357 Note that this B<does not> modify the existing object.
358  
359 =cut
360  
361 9 ▶sub subtract {
Subroutine subtract: called CC=5 SCAR=16.1
Pod undocumented
362 9 my ($self, $num, $swap) = @_;
363 9 ▶ $num //= 0;
Condition50%1/2
$num //= 0
l!l
90
MC/DC0%0/1
$num //= 0
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
364  
365 9 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) 4 5 9
366 4 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 3 4
367 if ($self->code ne $num->code);
368  
369 3 return $self->clone(value => $self->value->copy->bsub($num->value));
370 }
371  
372 5 my $result = $self->clone(value => $self->value->copy->bsub($self->clone(value => $num)->value));
373 5 ▶ $result = -$result if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 3 2 5
374 5 return $result;
375 }
376  
377 =head2 substract_in_place($num)
378  
379 Subtracts the specified amount to this C<Data::Money> object,modifying its value.
380 You can supply either a number or a C<Data::Money> object. Note that this B<does>
381 modify the existing object.
382  
383 =cut
384  
385 3 ▶sub subtract_in_place {
Subroutine subtract_in_place: called CC=3 SCAR=11.0
Pod undocumented
386 3 my ($self, $num) = @_;
387  
388 3 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) { } 2 1 3
389 2 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 1 2
390 if ($self->code ne $num->code);
391  
392 1 $self->value($self->value->copy->bsub($num->value));
393 } else {
394 1 $self->value($self->value->copy->bsub($self->clone(value => $num)->value));
395 }
396  
397 2 return $self;
398 }
399  
400 =head2 multiply($num)
401  
402 Multiplies the value of this C<Data::Money> object and returns a new C<Data::Money>
403 object. You dcan dsupply either a number or a C<Data::Money> object. Note that this
404 B<does not> modify the existing object.
405  
406 =cut
407  
408 5 ▶sub multiply {
Subroutine multiply: called CC=3 SCAR=11.0
Pod documented
409 5 my ($self, $num) = @_;
410  
411 5 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) 3 2 5
412 3 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 2 3
413 if ($self->code ne $num->code);
414  
415 2 return $self->clone(value => $self->value->copy->bmul($num->value));
416 }
417  
418 2 return $self->clone(value => $self->value->copy->bmul($self->clone(value => $num)->value))
419 }
420  
421 =head2 multiply_in_place($num)
422  
423 Multiplies the value of this C<Data::Money> object, modifying its value. You can
424 supply either a number or a C<Data::Money> object. Note that this B<does> modify
425 the existing object.
426  
427 =cut
428  
429 5 ▶sub multiply_in_place {
Subroutine multiply_in_place: called CC=3 SCAR=11.0
Pod documented
430 5 my ($self, $num) = @_;
431  
432 5 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) { } 3 2 5
433 3 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 2 3
434 if ($self->code ne $num->code);
435  
436 2 $self->value($self->value->copy->bmul($num->value));
437 } else {
438 2 $self->value($self->value->copy->bmul($self->clone(value => $num)->value));
439 }
440  
441 4 return $self;
442 }
443  
444 =head2 divide($num)
445  
446 Divides the value of this C<Data::Money> object and returns a new C<Data::Money>
447 object. You can supply either a number or a C<Data::Money> object. Note that this
448 B<does not> modify the existing object.
449  
450 =cut
451  
452 5 ▶sub divide {
Subroutine divide: called CC=3 SCAR=11.0
Pod documented
453 5 my ($self, $num) = @_;
454  
455 5 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) 3 2 5
456 3 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 2 3
457 if ($self->code ne $num->code);
458  
459 2 my $val = $self->value->copy->bdiv($num->value);
460 2 return $self->clone(value => $self->_round_up($val));
461 }
462  
463 2 my $val = $self->value->copy->bdiv($self->clone(value => $num)->value);
464 2 return $self->clone(value => $self->_round_up($val));
465 }
466  
467 =head2 divide_in_place($num)
468  
469 Divides the value of this C<Data::Money> object, modifying its value. You can
470 supply either a number or a C<Data::Money> object. Note that this B<does> modify
471 the existing object.
472  
473 =cut
474  
475 5 ▶sub divide_in_place {
Subroutine divide_in_place: called CC=3 SCAR=11.0
Pod documented
476 5 my ($self, $num) = @_;
477  
478 5 my $val;
479 5 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) { } 3 2 5
480 3 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 2 3
481 if ($self->code ne $num->code);
482  
483 2 $val = $self->value->copy->bdiv($num->value);
484 } else {
485 2 $val = $self->value->copy->bdiv($self->clone(value => $num));
486 }
487  
488 4 $self->value($self->_round_up($val));
489  
490 4 return $self;
491 }
492  
493 =head2 modulo($num)
494  
495 Performs the modulo operation on this C<Data::Money> object, returning a new C<Data::Money>
496 object with the value of the remainder.
497  
498 =cut
499  
500 1 ▶sub modulo {
Subroutine modulo: called CC=3 SCAR=15.0
Pod documented
501 1 my ($self, $num) = @_;
502  
503 1 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) 1 0 1
504 1 ▶ Data::Money::BaseException::MismatchCurrencyType->throw
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $num->code 1 0 1
505 if ($self->code ne $num->code);
506  
507 0 my $val = $self->value->copy->bmod($num->value);
508 0 return $self->clone(value => $val);
509 }
510  
511 0 my $val = $self->value->copy->bmod($self->clone(value => $num)->value);
512 0 return $self->clone(value => $val);
513 }
514  
515 =head2 three_way_compare($num)
516  
517 Compares a C<Data::Money> object to another C<Data::Money> object, or anything it
518 is capable of coercing - numbers, numerical strings, or L<Math::BigFloat> objects.
519 Both numerical and string comparators work.
520  
521 =cut
522  
523 72 ▶sub three_way_compare {
Subroutine three_way_compare: called CC=5 SCAR=16.1
Pod documented
524 72 my ($self, $num, $swap) = @_;
525 72 ▶ $num //= 0;
Condition50%1/2
$num //= 0
l!l
720
MC/DC0%0/1
$num //= 0
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
526  
527 72 my $other;
528 72 ▶ if (ref($num) eq ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $num eq ref $self) { } 16 56 72
529 16 $other = $num;
530 } else {
531 # we clone here to ensure that if we're comparing a number to
532 # an object, that the currency codes match (and we don't just
533 # get the default).
534 56 $other = $self->clone(value => $num);
535 }
536  
537 72 ▶ Data::Money::BaseException::MismatchCurrencyType->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $self->code ne $other->code 4 68 72
538 {
539 error => 'Unable to compare different currency types.'
540 })
541 if ($self->code ne $other->code);
542  
543 68 ▶ return $swap
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 6 62 68
544 ? $other->value->copy->bfround( 0 - $self->_decimal_precision )
545 <=> $self->value->copy->bfround( 0 - $self->_decimal_precision )
546 : $self->value->copy->bfround( 0 - $self->_decimal_precision )
547 <=> $other->value->copy->bfround( 0 - $self->_decimal_precision );
548 }
549  
550 #
551 #
552 # PRIVATE METHODS
553  
554 362 ▶sub _decimal_precision {
Subroutine _decimal_precision: called CC=5 SCAR=16.5
555 362 my ($self, $code) = @_;
556  
557 362 ▶ $code ||= $self->code;
Condition33%1/3
$code ||= $self->code
l!l&&r!l&&!r
03620
MC/DC0%0/2
$code ||= $self->code
$code $self->code
Truth table33%1/3
A ||= B
A= $codeB= $self->code
ABresult
1X1
011
000
558  
559 362 my $format;
560 ## funky eval to get string versions of constants back into the values
561 362 eval '$format = Locale::Currency::Format::' . $self->format;
562  
563 362 ▶ Data::Money::BaseException::InvalidCurrencyCode->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless _is_CurrencyCode($code) 1 361 362
Condition50%1/2
$code || 'undef'
l!l
10
MC/DC0%0/1
$code || 'undef'
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
564 {
565 error => 'Invalid currency code: ' . ($code || 'undef')
566 })
567 unless (_is_CurrencyCode($code));
568  
569 361 ▶ return Locale::Currency::Format::decimal_precision($code) || 0;
Condition100%2/2
Locale::Currency::Format::decimal_precision($code) || 0
l!l
3547
MC/DC100%1/1
Locale::Currency::Format::decimal_precision($code) || 0
Locale::Currency::Format::decimal_precision($code)
Truth table100%2/2
A
A= Locale::Currency::Format::decimal_precision($code)
Aresult
11
00
570 }
571  
572 8 ▶sub _round_up {
Subroutine _round_up: called CC=1 SCAR=0.0
573 8 my ($self, $val) = @_;
574  
575 8 my $prec = Locale::Currency::Format::decimal_precision($self->code);
576 8 return sprintf('%.0'.$prec.'f', _round($val, $prec*-1));
577 }
578  
579 18 ▶sub _to_utf8 {
Subroutine _to_utf8: called CC=2 SCAR=7.0
580 18 my $value = shift;
581  
582 18 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 18 0 18
583 18 utf8::decode($value);
584 };
585  
586 18 return $value;
587 };
588  
589 380 ▶sub _is_CurrencyCode {
Subroutine _is_CurrencyCode: called CC=2 SCAR=7.1
590 380 my ($code) = @_;
591  
592 380 ▶ return 0 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 380 380
593  
594 380 return defined code2currency($code, 'alpha');
595 }
596  
597 # http://www.perlmonks.org/?node_id=24335
598 8 ▶sub _round {
Subroutine _round: called CC=3 SCAR=12.0
599 8 my ($number, $places) = @_;
600  
601 8 ▶ my $sign = ($number < 0) ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$number < 0 ? : 0 8 8
602 8 my $abs = abs($number);
603  
604 8 ▶ if ($places < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($places < 0) { } 8 0 8
605 8 $places *= -1;
606 8 return $sign . substr($abs+("0." . "0" x $places . "5"), 0, $places+length(int($abs))+1);
607 } else {
608 0 my $p10 = 10**$places;
609 0 return $sign . int($abs/$p10 + 0.5)*$p10;
610 }
611 }
612  
613 =head1 OPERATOR OVERLOADING
614  
615 C<Data::Money> overrides some operators. It is important to note which operators
616 change the object's value and which return new ones.All operators accept either a
617 C<Data::Money> argument / a normal number via scalar and will die if the currency
618 types mismatch.
619  
620 C<Data::Money> overloads the following operators:
621  
622 =over 4
623  
624 =item +
625  
626 Handled by the C<add> method. Returns a new C<Data::Money> object.
627  
628 =item -
629  
630 Handled by the C<subtract> method. Returns a new C<Data::Money> object.
631  
632 =item S< >*
633  
634 Handled by the C<multiply> method. Returns a new C<Data::Money> object.
635  
636 =item /
637  
638 Handled by the C<divide> method. Returns a new C<Data::Money> object.
639  
640 =item +=
641  
642 Handled by the C<add_in_place()> method. Modifies the left-hand object's value.
643 Works with either a C<Data::Money> argument or a normal number.
644  
645 =item -=
646  
647 Handled by the C<subtract_in_place()> method. Modifies the left-hand object's
648 value. Works with either a C<Data::Money> argument or a normal number.
649  
650 =item *=
651  
652 Handled by the C<multiply_in_place()> method. Modifies the left-hand object's
653 value. Works with either a C<Data::Money> argument or a normal number.
654  
655 =item /=
656  
657 Handled by the C<divide_in_place()> method. Modifies the left-hand object's
658 value. Works with either a C<Data::Money> argument or a normal number.
659  
660 =item <=>
661  
662 Performs a three way comparsion. Works with either a Data::Money argument or a
663 normal number.
664  
665 =back
666  
667 =head1 SEE ALSO
668  
669 =over 4
670  
671 =item L<Locale::Currency>
672  
673 =item L<Locale::Currency::Format>
674  
675 =back
676  
677 =head1 ACKNOWLEDGEMENTS
678  
679 This module was originally based on L<Data::Currency> by Christopher H. Laco but I
680 I opted to fork and create a whole new module because my work was wildly different
681 from the original. I decided it was better to make a new module than to break back
682 compat and surprise users. Many thanks to him for the great module.
683  
684 Inspiration and ideas were also drawn from L<Math::Currency> and L<Math::BigFloat>.
685  
686 Major contributions (more overloaded operators, disallowing operations on mismatched
687 currences, absolute value, negation and unit tests) from Andrew Nelson C<< <anelson@cpan.org> >>.
688  
689 =head1 AUTHOR
690  
691 Cory G Watson, C<< <gphat at cpan.org> >>
692  
693 Currently maintained by Mohammad S Anwar (MANWAR) C<< <mohammad.anwar at yahoo.com> >>
694  
695 =head1 REPOSITORY
696  
697 L<https://github.com/manwar/Data-Money>
698  
699 =head1 LICENSE AND COPYRIGHT
700  
701 Copyright 2010 Cory Watson
702  
703 This program is free software; you can redistribute it and/or modify it under the
704 terms of either: the GNU General Public License as published by the Free Software
705 Foundation; or the Artistic License.
706  
707 See L<here|http://dev.perl.org/licenses> for more information.
708  
709 =cut
710  
711 8 1; # End of Data::Money
↑ Summary Money/BaseException/InvalidCurrencyCode.pm »