line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBR::Config::Trans::Dollars; |
2
|
|
|
|
|
|
|
|
3
|
18
|
|
|
18
|
|
111
|
use strict; |
|
18
|
|
|
|
|
32
|
|
|
18
|
|
|
|
|
797
|
|
4
|
18
|
|
|
18
|
|
102
|
use base 'DBR::Config::Trans'; |
|
18
|
|
|
|
|
74
|
|
|
18
|
|
|
|
|
5658
|
|
5
|
|
|
|
|
|
|
|
6
|
0
|
|
|
0
|
0
|
0
|
sub new { die "Should not get here" } |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub forward{ |
10
|
4
|
|
|
4
|
0
|
9
|
my $self = shift; |
11
|
4
|
|
|
|
|
9
|
my $cents = shift; |
12
|
4
|
|
|
|
|
29
|
return bless( [$cents] , 'DBR::_DOLLARS'); |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub backward{ |
16
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
17
|
0
|
|
|
|
|
0
|
my $value = shift; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
0
|
|
|
0
|
return undef unless defined($value) && length($value); |
20
|
|
|
|
|
|
|
|
21
|
0
|
0
|
|
|
|
0
|
if( ref($value) eq 'DBR::_DOLLARS' ){ # looks like it's a dollar object, yay! |
22
|
0
|
|
|
|
|
0
|
return $value->cents; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
0
|
$value =~ tr/0-9.-//cd; # the items listed are ALLOWED values |
26
|
0
|
0
|
|
|
|
0
|
unless(length($value)){ |
27
|
0
|
|
|
|
|
0
|
$self->_error('invalid value specified'); |
28
|
0
|
|
|
|
|
0
|
return (); |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
|
|
|
0
|
return sprintf("%.0f", ($value * 100) ); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package DBR::_DOLLARS; |
34
|
|
|
|
|
|
|
|
35
|
18
|
|
|
18
|
|
137
|
use strict; |
|
18
|
|
|
|
|
43
|
|
|
18
|
|
|
|
|
636
|
|
36
|
18
|
|
|
18
|
|
96
|
use Carp; |
|
18
|
|
|
|
|
35
|
|
|
18
|
|
|
|
|
9830
|
|
37
|
|
|
|
|
|
|
use overload |
38
|
|
|
|
|
|
|
#values |
39
|
20
|
|
|
20
|
|
1880
|
'""' => sub { $_[0]->format }, |
40
|
4
|
|
|
4
|
|
77
|
'0+' => sub { $_[0]->dollars }, |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# comparisons |
43
|
0
|
|
|
0
|
|
0
|
'==' => sub { $_[0]->dollars == $_[1] }, |
44
|
0
|
|
|
0
|
|
0
|
'!=' => sub { $_[0]->dollars != $_[1] }, |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#operators |
47
|
8
|
|
|
8
|
|
32
|
'+' => sub { new($_[0]->cents + _getcents($_[1])) }, |
48
|
|
|
|
|
|
|
'-' => sub { |
49
|
0
|
|
|
0
|
|
0
|
my ($a,$b) = ($_[0]->cents, _getcents($_[1])); |
50
|
0
|
0
|
|
|
|
0
|
new ($_[2] ? $b - $a : $a - $b); |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
0
|
|
0
|
'*' => sub { new($_[0]->cents * $_[1]) }, |
54
|
|
|
|
|
|
|
'/' => sub { |
55
|
0
|
|
|
0
|
|
0
|
my ($a,$b) = ($_[0]->cents, $_[1] ); |
56
|
0
|
0
|
|
|
|
0
|
new ($_[2] ? $b / $a : $a / $b); |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
|
59
|
18
|
|
|
18
|
|
133
|
'fallback' => 1; |
|
18
|
|
|
|
|
45
|
|
|
18
|
|
|
|
|
521
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
*TO_JSON = \&dollars; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub cents { |
64
|
19
|
100
|
|
19
|
|
11355
|
return '' unless defined($_[0][0]); |
65
|
15
|
|
|
|
|
54
|
return $_[0][0] |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
sub dollars { |
68
|
21
|
50
|
|
21
|
|
61
|
return '' unless defined($_[0][0]); |
69
|
21
|
|
|
|
|
160
|
return sprintf("%.02f",$_[0][0]/100) |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub format { |
73
|
20
|
100
|
|
20
|
|
67
|
return '' unless defined($_[0][0]); |
74
|
17
|
|
|
|
|
34
|
my $dollars = shift->dollars; |
75
|
17
|
|
|
|
|
56
|
$dollars =~ s/\G(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1,/g; |
76
|
17
|
|
|
|
|
214
|
return '$' . $dollars; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#utilities |
80
|
8
|
|
33
|
8
|
|
61
|
sub new{ bless([ $_[1] || $_[0] ],'DBR::_DOLLARS') } # will work OO or functional |
81
|
|
|
|
|
|
|
sub _getcents{ |
82
|
8
|
|
33
|
8
|
|
28
|
my $val = $_[1] || $_[0]; # can be OO or functional |
83
|
8
|
100
|
|
|
|
31
|
return $val->cents if ref($val) eq __PACKAGE__; |
84
|
5
|
|
|
|
|
31
|
return sprintf("%.0f", ($val * 100) ) |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |