lib/DR/Money.pm

filter: Statementstmt 95.2% 119 / 125 Branchbran 86.9% 40 / 46 Conditioncond 75.7% 25 / 33 MC/DCmcdc 57.8% 11 / 19 Subroutinesub 96.3% 26 / 27 Podpod 100% 3 / 3 totaltotal 88.5% 224 / 253 CC 36 SCAR 17.6
CC
36
Cov
90%
_assign
14.1
_div
9.3
_mul
3.5
CRAP 5.8
↑ Summary
1 package DR::Money;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our %EXPORT_TAGS = ( 'all' => [ qw(Money) ] );
10 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
11 1 our @EXPORT = qw(Money);
12  
13 1 our $VERSION = '0.02';
14  
15 use overload
16 1 ▶ '""' => \&value,
Subroutine BEGIN: called CC=1 SCAR=0.0
17 '0+' => \&value,
18 27 ▶ 'bool' => sub { $_[0][1] || $_[0][2] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0][1] or $_[0][2] 20 7 27
19 'cmp' => \&_cmp,
20 '<=>' => \&_dcmp,
21 0 ▶ '=' => sub { $_[0]->new($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 1 'int' => \&_int,
23 '+' => \&_add,
24 '*' => \&_mul,
25 '-' => \&_sub,
26 '/' => \&_div,
27  
28 '++' => \&_inc,
29 '--' => \&_dec,
30 '+=' => \&_addself,
31 '-=' => \&_subself,
32 '*=' => \&_mulself,
33 '/=' => \&_divself,
34 1 ;
35  
36  
37 =head1 NAME
38  
39 DR::Money - module to manipulate by money in perl scripts
40  
41 =head1 SYNOPSIS
42  
43 my $m = Money(2.3);
44 print $m; # prints 2.30
45 $m += 2.3; # 4.60
46 $m += Money(4.2); # 8.80
47  
48 The module supports negative moneys.
49  
50 =head1 Functions
51  
52 =head2 Money
53  
54 Functional constructor.
55  
56 my $money = Money(0.1);
57 printf '%s', $money; # prints 0.10
58  
59 =cut
60  
61 95 ▶sub Money($) { __PACKAGE__->new($_[0]) }
Subroutine Money: called CC=1 SCAR=0.0
Pod documented
62  
63  
64 =head1 Methods
65  
66 =head2 new
67  
68 Class or instance's method. Construct new instance.
69  
70 =cut
71  
72 96 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
73 96 my ($class, $value) = @_;
74  
75 96 my $self;
76  
77 96 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 96 96
78 0 $self = bless [ '+', 0, 0, '0.00' ] => ref $class;
79 0 $self->_assign( $class );
80 } else {
81 96 $self = bless [ '+', 0, 0, '0.00' ] => $class;
82 }
83  
84 96 ▶ $self->_assign($value) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 95 1 96
85 96 return $self;
86 }
87  
88  
89 =head2 value
90  
91 Returns value (string).
92  
93 my $money = Money(0.1);
94 $v = $money->value; # 0.10
95 $v = "$money"; # the same
96  
97 =cut
98  
99 9 ▶sub value { $_[0][3] }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
100  
101  
102  
103 =head1 Private (overload methods)
104  
105 =head2 _assign($value)
106  
107 Private method. Assigns new value to instance. Returns instance.
108  
109 my $money = Money(0.1);
110  
111 $money->_assign( 0.2 );
112 $money = 0.2; # the same
113  
114 =cut
115  
116 95 ▶sub _assign {
Subroutine _assign: called CC=14 SCAR=26.4
117 95 my ($self, $value) = @_;
118 95 ▶ croak 'usage $money->_assign($value)' unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 95 95
119  
120 95 ▶ if (!$value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 9 86 95
121 9 @$self = ('+', 0, 0, '0.00');
122 9 return $self;
123 }
124  
125 86 ▶ if (ref $value and $value->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa(__PACKAGE__)) 20 66 86
Condition66%2/3
ref $value and $value->isa(__PACKAGE__)
!ll&&!rl&&r
66020
MC/DC50%1/2
ref $value and $value->isa(__PACKAGE__)
ref $value $value->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $valueB= $value->isa(__PACKAGE__)
ABresult
0X0
100
111
126 20 @$self = @{ $value }[0, 1, 2, 3];
127 20 return $self;
128 }
129  
130 66 $value =~ s/\s+//g;
131  
132 66 my ($sign, $r, $k, $s);
133  
134 66 ▶ if ($value =~ /^(-)?0*(\d+)[,\.]?$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^(-)?0*(\d+)[,\.]?$/) { } 7 59 66
elsif ($value =~ /^(-)?0*(\d*)[,\.](\d+)$/) { } 59 0 59
135 7 ▶ $sign = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
16
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
136 7 $r = int $2;
137 7 $k = 0;
138 7 ▶ $s = sprintf '%s%d.00', ($sign eq '-' ? '-' : ''), $r;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 1 6 7
139 } elsif ($value =~ /^(-)?0*(\d*)[,\.](\d+)$/) {
140 59 ▶ $sign = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
1247
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
141 59 ▶ $r = int($2 || 0);
Condition100%2/2
$2 || 0
l!l
4316
MC/DC100%1/1
$2 || 0
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
142 59 $k = substr($3 . '0', 0, 2);
143 59 $k =~ s/^0//;
144 59 $k = int $k;
145 59 ▶ $sign = '+' unless $r or $k;
Branch: 1 branch
BranchTrueFalseTotal
unless $r or $k 3 56 59
Condition100%3/3
$r or $k
l!l&&r!l&&!r
43133
MC/DC100%2/2
$r or $k
$r $k
Truth table100%3/3
A or B
A= $rB= $k
ABresult
1X1
011
000
146 59 ▶ $s = sprintf '%s%d.%02d', ($sign eq '-' ? '-' : ''), $r, $k;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 11 48 59
147 } else {
148 0 croak "wrong money value: $value";
149 }
150  
151 66 @$self = ($sign, $r, $k, $s);
152  
153 66 return $self;
154 }
155  
156 =head2 _cmp
157  
158 Private method. Compares two instances as string.
159  
160 my $money1 = Money(0.1);
161 my $money2 = Money(0.01);
162  
163 $money1->_cmp($money2);
164  
165 $money1 cmp $money2; # the same
166  
167 =cut
168  
169 19 ▶sub _cmp {
Subroutine _cmp: called CC=2 SCAR=8.1
170 19 my ($self, $cv, $flip) = @_;
171 19 ▶ return $self->[3] cmp Money($cv)->[3] unless $flip;
Branch: 1 branch
BranchTrueFalseTotal
unless $flip 19 0 19
172 0 return Money($cv)->[3] cmp $self->[3];
173 }
174  
175  
176 =head2 _dcmp
177  
178 Private method. Compares two instances as digit.
179  
180 my $money1 = Money(0.1);
181 my $money2 = Money(0.01);
182  
183 $money1->_dcmp($money2);
184  
185 $money1 <=> $money2; # the same
186  
187 =cut
188  
189 7 ▶sub _dcmp {
Subroutine _dcmp: called CC=2 SCAR=6.9
190 7 my ($self, $cv, $flip) = @_;
191 7 ▶ return Money($self)->_kop <=> Money($cv)->_kop unless $flip;
Branch: 1 branch
BranchTrueFalseTotal
unless $flip 6 1 7
192 1 return Money($cv)->_kop <=> Money($self)->_kop;
193 }
194  
195  
196 44 ▶sub _kop {
Subroutine _kop: called CC=2 SCAR=6.9
197 44 my ($self) = @_;
198 44 my ($sign, $r, $k) = @$self;
199 44 ▶ return -($r * 100 + $k) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 6 38 44
200 38 return $r * 100 + $k;
201 }
202  
203 18 ▶sub _from_kop {
Subroutine _from_kop: called CC=3 SCAR=11.1
204 18 my ($class, $kop) = @_;
205 18 my ($sign, $r, $k, $s);
206  
207 18 $k = abs($kop) % 100;
208 18 $r = (abs($kop) - $k) / 100;
209  
210 18 ▶ if ($kop < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kop < 0) { } 4 14 18
211 4 $sign = '-';
212 4 $s = sprintf '-%d.%02d', $r, $k;
213 } else {
214 14 $sign = '+';
215 14 $s = sprintf '%d.%02d', $r, $k;
216 }
217  
218 18 ▶ return bless [ $sign, $r, $k, $s ] => ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
1800
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
219 }
220  
221 =head2 _add
222  
223 Private method. Add two instances (or instance and number).
224  
225 my $money1 = Money(1.23);
226 my $money2 = Money(2.34);
227 my $money3 = $money1->_add($money2);
228  
229 $money3 = $money1 + $money2; # the same
230  
231 =cut
232  
233 3 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
234 3 my ($self, $value) = @_;
235 3 return $self->_from_kop($self->_kop + Money($value)->_kop);
236  
237 }
238  
239  
240 =head2 _mul
241  
242 Private method. Multiplicate money to number
243  
244 my $money = Money(1.23);
245
246 $money = $money->_mul(234);
247  
248 $money = $money * 234; # the same
249  
250 =cut
251  
252 5 ▶sub _mul {
Subroutine _mul: called CC=3 SCAR=12.5
253 5 my ($self, $mul) = @_;
254 5 ▶ croak "Can't multiply money to money"
Branch: 1 branch
BranchTrueFalseTotal
if ref $mul and $mul->isa(__PACKAGE__) 0 5 5
Condition33%1/3
ref $mul and $mul->isa(__PACKAGE__)
!ll&&!rl&&r
500
MC/DC0%0/2
ref $mul and $mul->isa(__PACKAGE__)
ref $mul $mul->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= ref $mulB= $mul->isa(__PACKAGE__)
ABresult
0X0
100
111
255 if ref $mul and $mul->isa(__PACKAGE__);
256 5 return $self->_from_kop(int($self->_kop * $mul));
257 }
258  
259  
260 =head2 _sub
261  
262 Private method. Substract money.
263  
264 my $money = Money(1.23);
265  
266 $money = $money->_sub(1.22);
267  
268 $money = $money - 1.22; # the same
269  
270 =cut
271  
272 3 ▶sub _sub {
Subroutine _sub: called CC=2 SCAR=6.9
273 3 my ($self, $sv, $flip) = @_;
274 3 my $v = Money($sv);
275  
276 3 ▶ return $self->_from_kop( $self->_kop - $v->_kop ) unless $flip;
Branch: 1 branch
BranchTrueFalseTotal
unless $flip 2 1 3
277 1 return $self->_from_kop( $v->_kop - $self->_kop );
278 }
279  
280  
281 =head2 _div
282  
283 Private method. Divide money.
284  
285 my $money = Money(1.22);
286  
287 $money = $money / 2;
288 $number = $money / Money(2.54);
289 $number = 1.23 / $money;
290  
291 =cut
292  
293 10 ▶sub _div {
Subroutine _div: called CC=9 SCAR=22.3
294 10 my ($self, $div, $flip) = @_;
295  
296 10 ▶ croak "Division by zero"
Branch: 1 branch
BranchTrueFalseTotal
if $div == 0 and not $flip or $flip and not $self 3 7 10
Condition88%8/9
$div == 0 and not $flip or $flip and not $self
l!l&&r!l&&!r
217
$div == 0 and not $flip
!ll&&!rl&&r
802
$flip and not $self
!ll&&!rl&&r
521
MC/DC75%3/4
$div == 0 and not $flip or $flip and not $self
$div == 0 not $flip $flip not $self
Truth table57%4/7
A and not(B) or C and not(D)
A= $div == 0B= not $flipC= $flipD= not $self
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
297 if (($div == 0 and !$flip) or ($flip and !$self));
298  
299 7 ▶ if (ref $div and $div->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $div and $div->isa(__PACKAGE__)) 2 5 7
Condition66%2/3
ref $div and $div->isa(__PACKAGE__)
!ll&&!rl&&r
502
MC/DC50%1/2
ref $div and $div->isa(__PACKAGE__)
ref $div $div->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $divB= $div->isa(__PACKAGE__)
ABresult
0X0
100
111
300  
301 2 ▶ return $self->_kop / $div->_kop unless $flip;
Branch: 1 branch
BranchTrueFalseTotal
unless $flip 2 0 2
302 0 return $div->_kop / $self->_kop;
303 }
304  
305 5 ▶ return $self->_from_kop( int($self->_kop / $div) ) unless $flip;
Branch: 1 branch
BranchTrueFalseTotal
unless $flip 3 2 5
306 2 return 100 * $div / $self->_kop;
307 }
308
309  
310 =head2 _inc
311  
312 Private method. Increment money.
313  
314 my $money = Money(1.22);
315 $money->_inc; # 1.23
316  
317 $money++; # the same
318  
319 =cut
320  
321 2 ▶sub _inc {
Subroutine _inc: called CC=1 SCAR=0.0
322 2 my ($self) = @_;
323 2 @$self = @{ $self->_from_kop($self->_kop + 1) };
324 2 return $self;
325 }
326  
327  
328 =head2 _dec
329  
330 Private method. Decrement money.
331  
332 my $money = Money(2.54);
333 $money->_dec; # 2.53
334  
335 $money--; # the same
336  
337 =cut
338  
339 2 ▶sub _dec {
Subroutine _dec: called CC=1 SCAR=0.0
340 2 my ($self) = @_;
341 2 @$self = @{ $self->_from_kop($self->_kop - 1) };
342 2 return $self;
343 }
344  
345  
346  
347 =head2 _addself
348  
349 Private method. Add value to money.
350  
351 $money->_addself(23);
352  
353 $money += 23; # the same
354  
355 =cut
356  
357 1 ▶sub _addself {
Subroutine _addself: called CC=1 SCAR=0.0
358 1 my ($self, $av) = @_;
359 1 @$self = @{ $self->_add($av) };
360 1 return $self;
361 }
362  
363  
364 =head2 _mulself
365  
366 Private method. Mull value to money.
367  
368 $money->_mulself(23);
369 $money *= 23; # the same
370  
371 =cut
372  
373 1 ▶sub _mulself {
Subroutine _mulself: called CC=1 SCAR=0.0
374 1 my ($self, $av) = @_;
375 1 @$self = @{ $self->_mul($av) };
376 1 return $self;
377 }
378  
379  
380 =head2 _subself
381  
382 Private method. Substract value from money.
383  
384 $money->_subself(12.34);
385 $money -= 12.34; # the same
386  
387 =cut
388  
389 1 ▶sub _subself {
Subroutine _subself: called CC=1 SCAR=0.0
390 1 my ($self, $av) = @_;
391 1 @$self = @{ $self->_sub($av) };
392 1 return $self;
393 }
394  
395 =head2 _divself
396  
397 Private method. Divide value by number.
398  
399 $money->_subself(12.34);
400 $money -= 12.34; # the same
401  
402 =cut
403  
404 2 ▶sub _divself {
Subroutine _divself: called CC=3 SCAR=11.0
405 2 my ($self, $av) = @_;
406 2 ▶ croak "Can't divide money to money in-place"
Branch: 1 branch
BranchTrueFalseTotal
if ref $av and $av->isa(__PACKAGE__) 1 1 2
Condition66%2/3
ref $av and $av->isa(__PACKAGE__)
!ll&&!rl&&r
101
MC/DC50%1/2
ref $av and $av->isa(__PACKAGE__)
ref $av $av->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $avB= $av->isa(__PACKAGE__)
ABresult
0X0
100
111
407 if ref $av and $av->isa(__PACKAGE__);
408 1 @$self = @{ $self->_div($av) };
409 1 return $self;
410 }
411  
412  
413 =head2 _int
414  
415 Private method.
416 my $money = Money(100.11);
417 my $r = $money->_int; # 100
418 my $r = int Money; # the same
419  
420 =cut
421  
422 2 ▶sub _int {
Subroutine _int: called CC=2 SCAR=6.9
423 2 my ($self) = @_;
424 2 ▶ return $self->[1] if $self->[0] eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $self->[0] eq "+" 1 1 2
425 1 return -$self->[1];
426 }
427  
428  
429 =head1 COPYRIGHT AND LICENSE
430  
431 This library is free software; you can redistribute it and/or modify
432 it under the same terms as Perl itself, either Perl version 5.8.8 or,
433 at your option, any later version of Perl 5 you may have available
434  
435 =cut
436  
437 1 1;
↑ Summary