line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CLDR::Number::Format::Currency; |
2
|
|
|
|
|
|
|
|
3
|
11
|
|
|
11
|
|
34824
|
use v5.8.1; |
|
11
|
|
|
|
|
39
|
|
4
|
11
|
|
|
11
|
|
57
|
use utf8; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
82
|
|
5
|
11
|
|
|
11
|
|
254
|
use Carp; |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
2020
|
|
6
|
11
|
|
|
11
|
|
541
|
use CLDR::Number::Constant qw( $C ); |
|
11
|
|
|
|
|
122
|
|
|
11
|
|
|
|
|
1222
|
|
7
|
11
|
|
|
11
|
|
7926
|
use CLDR::Number::Data::Currency; |
|
11
|
|
|
|
|
32
|
|
|
11
|
|
|
|
|
623
|
|
8
|
|
|
|
|
|
|
|
9
|
11
|
|
|
11
|
|
1275
|
use Moo; |
|
11
|
|
|
|
|
12974
|
|
|
11
|
|
|
|
|
88
|
|
10
|
11
|
|
|
11
|
|
5687
|
use namespace::clean; |
|
11
|
|
|
|
|
11314
|
|
|
11
|
|
|
|
|
96
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
with qw( CLDR::Number::Role::Format ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has currency_code => ( |
17
|
|
|
|
|
|
|
is => 'rw', |
18
|
|
|
|
|
|
|
isa => sub { |
19
|
|
|
|
|
|
|
croak 'uninitialized value' if !defined $_[0]; |
20
|
|
|
|
|
|
|
croak qq{invalid value "$_[0]"} if $_[0] !~ m{ ^ [A-Z]{3} $ }x; |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
coerce => sub { defined $_[0] ? uc $_[0] : $_[0] }, |
23
|
|
|
|
|
|
|
trigger => 1, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has currency_sign => ( |
27
|
|
|
|
|
|
|
is => 'rw', |
28
|
|
|
|
|
|
|
isa => sub { |
29
|
|
|
|
|
|
|
croak "currency_sign is not defined" if !defined $_[0]; |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# TODO: accounting NYI |
34
|
|
|
|
|
|
|
has accounting => ( |
35
|
|
|
|
|
|
|
is => 'rw', |
36
|
|
|
|
|
|
|
coerce => sub { $_[0] ? 1 : 0 }, |
37
|
|
|
|
|
|
|
default => 0, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has cash => ( |
41
|
|
|
|
|
|
|
is => 'rw', |
42
|
|
|
|
|
|
|
coerce => sub { $_[0] ? 1 : 0 }, |
43
|
|
|
|
|
|
|
trigger => 1, |
44
|
|
|
|
|
|
|
default => 0, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has _pattern_type => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
default => 'currency', |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
after _trigger_locale => sub { |
53
|
|
|
|
|
|
|
my ($self) = @_; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
if ($self->currency_code) { |
56
|
|
|
|
|
|
|
$self->_build_currency_sign; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if (my $decimal = $self->_get_data(symbol => 'currency_decimal')) { |
60
|
|
|
|
|
|
|
$self->decimal_sign($decimal); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub BUILD {} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _trigger_currency_code { |
67
|
86
|
|
|
86
|
|
1043
|
my ($self, $currency_code) = @_; |
68
|
|
|
|
|
|
|
|
69
|
86
|
|
|
|
|
210
|
$self->_build_currency_sign; |
70
|
86
|
|
|
|
|
954
|
$self->_trigger_cash; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _build_currency_sign { |
74
|
168
|
|
|
168
|
|
230
|
my ($self) = @_; |
75
|
168
|
|
|
|
|
312
|
my $data = $CLDR::Number::Data::Currency::LOCALES; |
76
|
168
|
|
|
|
|
226
|
my $currency_sign; |
77
|
|
|
|
|
|
|
|
78
|
168
|
100
|
|
|
|
496
|
return if $self->_has_init_arg('currency_sign'); |
79
|
|
|
|
|
|
|
|
80
|
166
|
|
|
|
|
265
|
for my $locale (@{$self->_locale_inheritance}) { |
|
166
|
|
|
|
|
430
|
|
81
|
169
|
100
|
66
|
|
|
3481
|
next if !exists $data->{$locale} || !exists $data->{$locale}{$self->currency_code}; |
82
|
80
|
|
|
|
|
1895
|
$currency_sign = $data->{$locale}{$self->currency_code}; |
83
|
80
|
|
|
|
|
468
|
last; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
166
|
|
66
|
|
|
3076
|
$self->currency_sign($currency_sign || $self->currency_code); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _trigger_cash { |
90
|
86
|
|
|
86
|
|
123
|
my ($self) = @_; |
91
|
86
|
|
|
|
|
134
|
my $currencies = $CLDR::Number::Data::Currency::CURRENCIES; |
92
|
|
|
|
|
|
|
my $currency_data |
93
|
|
|
|
|
|
|
= $self->currency_code && exists $currencies->{$self->currency_code} |
94
|
|
|
|
|
|
|
? $currencies->{$self->currency_code} |
95
|
86
|
100
|
66
|
|
|
1633
|
: $currencies->{DEFAULT}; |
96
|
|
|
|
|
|
|
|
97
|
86
|
50
|
33
|
|
|
4058
|
if ($self->cash && exists $currency_data->{cash_digits}) { |
98
|
0
|
|
|
|
|
0
|
$self->minimum_fraction_digits($currency_data->{cash_digits}); |
99
|
0
|
|
|
|
|
0
|
$self->maximum_fraction_digits($currency_data->{cash_digits}); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
86
|
|
|
|
|
6623
|
$self->minimum_fraction_digits($currency_data->{digits}); |
103
|
86
|
|
|
|
|
4183
|
$self->maximum_fraction_digits($currency_data->{digits}); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
86
|
50
|
33
|
|
|
4733
|
if ($self->cash && exists $currency_data->{cash_rounding}) { |
107
|
0
|
|
|
|
|
0
|
$self->rounding_increment($currency_data->{cash_rounding}); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
86
|
|
|
|
|
714
|
$self->rounding_increment($currency_data->{rounding}); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub format { |
115
|
111
|
|
|
111
|
1
|
8361
|
my ($self, $num) = @_; |
116
|
|
|
|
|
|
|
|
117
|
111
|
|
|
|
|
346
|
$num = $self->_validate_number(format => $num); |
118
|
111
|
100
|
|
|
|
258
|
return undef unless defined $num; |
119
|
|
|
|
|
|
|
|
120
|
110
|
100
|
|
|
|
2204
|
croak 'Missing required attribute: currency_code' |
121
|
|
|
|
|
|
|
unless $self->currency_code; |
122
|
|
|
|
|
|
|
|
123
|
109
|
|
|
|
|
2448
|
my $sign = $self->currency_sign; |
124
|
109
|
|
|
|
|
753
|
my $format = $self->_format_number($num); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# spacing before currency sign |
127
|
109
|
100
|
100
|
|
|
743
|
if ($sign =~ m{ ^ \PS }x && $format =~ m{ \d $C }x) { |
128
|
4
|
|
|
|
|
11
|
$sign = ' ' . $sign; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# spacing after currency sign |
132
|
109
|
100
|
100
|
|
|
644
|
if ($sign =~ m{ \PS $ }x && $format =~ m{ $C \d }x) { |
133
|
14
|
|
|
|
|
31
|
$sign .= ' '; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
109
|
|
|
|
|
488
|
$format =~ s{$C}{$sign}; |
137
|
|
|
|
|
|
|
|
138
|
109
|
|
|
|
|
619
|
return $format; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
__END__ |