Currency.pm

filter: Statementstmt 92.1% 164 / 178 Branchbran 81.0% 81 / 100 Conditioncond 57.1% 36 / 63 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 100% 21 / 21 Podpod 92.3% 12 / 13 totaltotal 78.6% 321 / 408 SCAR 47.1
CC
78
Cov
82%
localize
63.9
new
17.0
format
17.0
CRAP 111.1
↑ Summary Currency/USD.pm »
1 #!/usr/bin/perl -w
2 # vim:ts=4:sw=4:et:at:
3 #
4 # PROGRAM: Math::Currency.pm # - 04/26/00 9:10:AM
5 # PURPOSE: Perform currency calculations without floating point
6 #
7 #------------------------------------------------------------------------------
8 # Copyright (c) 2001-2008 John Peacock
9 #
10 # You may distribute under the terms of either the GNU General Public
11 # License or the Artistic License, as specified in the Perl README file,
12 # with the exception that it cannot be placed on a CD-ROM or similar media
13 # for commercial distribution without the prior approval of the author.
14 #------------------------------------------------------------------------------
15  
16 package Math::Currency;
17 14 $Math::Currency::VERSION = '0.52';
18 # ABSTRACT: Exact Currency Math with Formatting and Rounding
19  
20 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 ▶use base qw(Exporter Math::BigFloat);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 14 ▶use Math::BigFloat 1.60;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 14 ▶use Encode::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 14 ▶use overload '""' => \&bstr;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 14 our $LC_MONETARY = {
31 en_US => {
32 INT_CURR_SYMBOL => 'USD ',
33 CURRENCY_SYMBOL => '$',
34 MON_DECIMAL_POINT => '.',
35 MON_THOUSANDS_SEP => ',',
36 MON_GROUPING => '3',
37 POSITIVE_SIGN => '',
38 NEGATIVE_SIGN => '-',
39 INT_FRAC_DIGITS => '2',
40 FRAC_DIGITS => '2',
41 P_CS_PRECEDES => '1',
42 P_SEP_BY_SPACE => '0',
43 N_CS_PRECEDES => '1',
44 N_SEP_BY_SPACE => '0',
45 P_SIGN_POSN => '1',
46 N_SIGN_POSN => '1',
47 },
48 };
49 14 $LC_MONETARY->{USD} = $LC_MONETARY->{en_US};
50  
51 14 ▶our $FORMAT = $LC_MONETARY->{en_US} unless localize();
Branch: 1 branch
BranchTrueFalseTotal
unless localize() 0 14 14
52  
53 14 our @EXPORT_OK = qw(
54 $LC_MONETARY
55 $FORMAT
56 Money
57 );
58  
59 # Set class constants
60 14 our $round_mode = 'even'; # Banker's rounding obviously
61 14 our $accuracy = undef;
62 14 ▶our $precision = $FORMAT->{FRAC_DIGITS} > 0 ? -$FORMAT->{FRAC_DIGITS} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Math::Currency::FORMAT->{'FRAC_DIGITS'} > 0 ? : 0 14 14
63 14 our $div_scale = 40;
64 14 our $use_int = 0;
65 14 our $always_init = 0; # should the localize() happen every time?
66  
67  
68 sub new {
69 144 ▶ my $proto = shift;
Subroutine new: called CC=16 SCAR=28.3
Pod documented
70 144 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
31410
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
71 144 ▶ my $parent = $proto if ref($proto);
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 3 141 144
72  
73 144 ▶ my $value = shift || 0;
Condition100%2/2
shift() || 0
l!l
1431
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
74  
75 144 ▶ if (eval("'1.01' ne 1.01")) { # this might be a comma locale
Branch: 1 branch
BranchTrueFalseTotal
if (eval "'1.01' ne 1.01") 0 144 144
76 0 $value =~ tr/,/./;
77 }
78 144 $value =~ tr/-()0-9.//cd; #strip any formatting characters
79 144 ▶ $value = "-$value" if $value =~ s/(^\()|(\)$)//g; # handle parens
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ s/(^\()|(\)$)//g 4 140 144
80  
81 144 ▶ if ( (caller)[0] =~ /Math\::BigInt/ ) # only when called from objectify()
Branch: 1 branch
BranchTrueFalseTotal
if ((caller)[0] =~ /Math\::BigInt/) 43 101 144
82 {
83 43 return Math::BigFloat->new($value);
84 }
85  
86 101 my $self;
87 101 my $currency = shift;
88 101 my $format;
89  
90 101 ▶ if ( not defined $currency and $class->isa('Math::Currency') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $currency and $class->isa("Math::Currency")) 92 9 101
Condition66%2/3
not defined $currency and $class->isa("Math::Currency")
!ll&&!rl&&r
9092
MC/DC50%1/2
not defined $currency and $class->isa("Math::Currency")
not defined $currency $class->isa("Math::Currency")
Truth table66%2/3
not(A) and B
A= not defined $currencyB= $class->isa("Math::Currency")
ABresult
0X0
100
111
91 # must be one of our subclasses
92 92 ▶ $currency = $1 if ($class =~ /Math::Currency::(\w+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /Math::Currency::(\w+)/ 12 80 92
93 }
94  
95 101 ▶ if ( defined $currency ) #override default currency type
Branch: 1 branch
BranchTrueFalseTotal
if (defined $currency) 21 80 101
96 {
97 21 ▶ unless ( defined $LC_MONETARY->{$currency} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Math::Currency::LC_MONETARY->{$currency}) 12 9 21
98 12 eval "require Math::Currency::$currency";
99 12 ▶ unknown_currency($currency) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 12 0 12
100 }
101 21 $format = $LC_MONETARY->{$currency};
102 }
103  
104 101 ▶ if ($format) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format) { } 9 92 101
elsif ($parent and defined $parent->{'format'}) { } 0 92 92
105 $self =
106 9 Math::BigFloat->new( $value, undef, -( $format->{FRAC_DIGITS} + 2 ) );
107 9 bless $self, $class;
108 9 $self->format($format);
109 }
110 ▶ elsif ( $parent
Condition66%2/3
$parent and defined $parent->{"format"}
!ll&&!rl&&r
9020
MC/DC0%0/2
$parent and defined $parent->{"format"}
$parent defined $parent->{"format"}
Truth table66%2/3
A and B
A= $parentB= defined $parent->{"format"}
ABresult
0X0
100
111
111 and defined $parent->{format} ) # if we are cloning an existing instance
112 {
113 $self =
114 Math::BigFloat->new( $value, undef,
115 0 -( $parent->format->{FRAC_DIGITS} + 2 ) );
116 0 bless $self, $class;
117 0 $self->format( $parent->format );
118 }
119 else {
120 $self =
121 92 Math::BigFloat->new( $value, undef, -( $FORMAT->{FRAC_DIGITS} + 2 ) );
122 92 bless $self, $class;
123 }
124 101 return $self;
125 } ##new
126  
127  
128 sub Money {
129 2 ▶ return __PACKAGE__->new(@_);
Subroutine Money: called CC=1 SCAR=0.0
Pod documented
130 }
131  
132 sub bstr {
133 57 ▶ my $self = shift;
Subroutine bstr: called CC=9 SCAR=22.1
Pod documented
134 57 my $myformat = $self->format();
135 57 my $value = $self->as_float();
136 57 my $neg = ( $value =~ tr/-//d );
137 57 my $dp = index( $value, "." );
138 my $sign = $neg
139 ? $myformat->{NEGATIVE_SIGN}
140 57 ▶ : $myformat->{POSITIVE_SIGN};
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 15 42 57
141 my $curr = $use_int
142 ? $myformat->{INT_CURR_SYMBOL}
143 57 ▶ : $myformat->{CURRENCY_SYMBOL};
Branch: 1 branch
BranchTrueFalseTotal
$Math::Currency::use_int ? : 10 47 57
144 my $digits = $use_int
145 ? $myformat->{INT_FRAC_DIGITS}
146 57 ▶ : $myformat->{FRAC_DIGITS};
Branch: 1 branch
BranchTrueFalseTotal
$Math::Currency::use_int ? : 10 47 57
147 57 my $formtab = [
148 [
149 [ '($value$curr)', '($value $curr)', '($value $curr)' ],
150 [ '$sign$value$curr', '$sign$value $curr', '$sign$value $curr' ],
151 [ '$value$curr$sign', '$value $curr$sign', '$value$curr $sign' ],
152 [ '$value$sign$curr', '$value $sign$curr', '$value$sign $curr' ],
153 [ '$value$curr$sign', '$value $curr$sign', '$value$curr $sign' ],
154 ],
155 [
156 [ '($curr$value)', '($curr $value)', '($curr $value)' ],
157 [ '$sign$curr$value', '$sign$curr $value', '$sign $curr$value' ],
158 [ '$curr$value$sign', '$curr $value$sign', '$curr$value $sign' ],
159 [ '$sign$curr$value', '$sign$curr $value', '$sign $curr$value' ],
160 [ '$curr$sign$value', '$curr$sign $value', '$curr $sign$value' ],
161 ],
162 ];
163  
164 57 ▶ if ( $dp < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dp < 0) { } 12 45 57
elsif (length($value) - $dp - 1 < $digits) { } 0 45 45
165 12 $value .= '.' . '0' x $digits;
166 }
167 elsif ( ( length($value) - $dp - 1 ) < $digits ) {
168 0 $value .= '0' x ( $digits - $dp );
169 }
170  
171 57 ( $value = reverse "$value" ) =~ s/\+//;
172  
173 # make sure there is a leading 0 for values < 1
174 57 ▶ if ( substr( $value, -1, 1 ) eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($value, -1, 1) eq ".") 0 57 57
175 0 $value .= "0";
176 }
177 57 $value =~ s/\./$myformat->{MON_DECIMAL_POINT}/;
178 57 $value =~
179 s/(\d{$myformat->{MON_GROUPING}})(?=\d)(?!\d*\.)/$1$myformat->{MON_THOUSANDS_SEP}/g;
180 57 $value = reverse $value;
181  
182 eval '$value = "'
183 . (
184 $neg
185 ? $formtab->[ $myformat->{N_CS_PRECEDES} ][ $myformat->{N_SIGN_POSN} ]
186 [ $myformat->{N_SEP_BY_SPACE} ]
187 : $formtab->[ $myformat->{P_CS_PRECEDES} ][ $myformat->{P_SIGN_POSN} ]
188 57 ▶ [ $myformat->{P_SEP_BY_SPACE} ]
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 15 42 57
189 )
190 . '"';
191  
192 57 ▶ if ( substr( $value, -1, 1 ) eq '.' ) { # trailing bare decimal
Branch: 1 branch
BranchTrueFalseTotal
if (substr($value, -1, 1) eq ".") 12 45 57
193 12 chop($value);
194 }
195  
196 57 return $value;
197 } ##stringify
198  
199  
200  
201 sub format {
202 364 ▶ my $self = shift;
Subroutine format: called CC=17 SCAR=28.3
Pod documented
203 364 my $key = shift; # do they want to display or set?
204 364 my $value = shift; # did they supply a value?
205 364 ▶ localize() if $always_init; # always reset the global format?
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Currency::always_init 0 364 364
206 364 my $source = \$FORMAT; # default format rules
207  
208 364 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 286 78 364
209 286 ▶ if ( defined $self->{format} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'format'}) { } 46 240 286
elsif (defined $key) { } 37 203 240
210 46 ▶ if ( defined $key and $key eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and $key eq '') { } 2 44 46
Condition100%3/3
defined $key and $key eq ""
!ll&&!rl&&r
4042
MC/DC100%2/2
defined $key and $key eq ""
defined $key $key eq ""
Truth table100%3/3
A and B
A= defined $keyB= $key eq ""
ABresult
0X0
100
111
211 2 delete $self->{format};
212 2 $source = \$FORMAT;
213 }
214 else {
215 44 $source = \$self->{format};
216 }
217 }
218 elsif ( defined $key ) # get/set a parameter
219 {
220 37 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value or ref $key eq "HASH") 35 2 37
Condition100%3/3
defined $value or ref $key eq "HASH"
l!l&&r!l&&!r
4312
MC/DC100%2/2
defined $value or ref $key eq "HASH"
defined $value ref $key eq "HASH"
Truth table100%3/3
A or B
A= defined $valueB= ref $key eq "HASH"
ABresult
1X1
011
000
221 or ref($key) eq "HASH" ) # have to copy global format
222 {
223 35 ▶ while ( my ( $k, $v ) = each %{$FORMAT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$FORMAT;}) 525 35 560
224 525 $self->{format}{$k} = $v;
225 }
226 35 $source = \$self->{format};
227 }
228 }
229 }
230 else { # called as class method to set the default currency
231 78 ▶ if ( defined $key && not exists $FORMAT->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and not exists $Math::Currency::FORMAT->{$key}) 18 60 78
Condition66%2/3
defined $key and not exists $Math::Currency::FORMAT->{$key}
!ll&&!rl&&r
06018
MC/DC50%1/2
defined $key and not exists $Math::Currency::FORMAT->{$key}
defined $key not exists $Math::Currency::FORMAT->{$key}
Truth table66%2/3
A and not(B)
A= defined $keyB= not exists $Math::Currency::FORMAT->{$key}
ABresult
0X0
100
111
232 18 ▶ unless ( defined $LC_MONETARY->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Math::Currency::LC_MONETARY->{$key}) 10 8 18
233 10 eval "require Math::Currency::$key";
234 10 ▶ unknown_currency($key) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 3 10
235 }
236 18 $FORMAT = $LC_MONETARY->{$key};
237 18 return $FORMAT;
238 }
239 }
240  
241  
242 346 ▶ if ( defined $key ) # otherwise just return
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 103 243 346
243 {
244 103 ▶ if ( ref($key) eq "HASH" ) # must be trying to replace all
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'HASH') { } 31 72 103
245 {
246 31 $$source = $key;
247 }
248 else # get/set just one parameter
249 {
250 72 ▶ return $$source->{$key} unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 66 6 72
251 6 $$source->{$key} = $value;
252 }
253 }
254 280 return $$source;
255 } ##format
256  
257  
258 sub as_float {
259 186 ▶ my $self = shift;
Subroutine as_float: called CC=1 SCAR=0.0
Pod documented
260 186 my $format = $self->format;
261 186 my $string = $self->copy->bfround( -$format->{FRAC_DIGITS} )->SUPER::bstr();
262 186 return $string;
263 }
264  
265  
266 sub copy {
267 203 ▶ my $self = shift;
Subroutine copy: called CC=3 SCAR=11.0
Pod documented
268  
269 # grab the builtin formatting
270 203 ▶ my $myformat = ( defined $self->{format} ? $self->{format} : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'format'} ? : 22 181 203
271  
272 # let Math::BigFloat do it's thing
273 203 my $new = $self->SUPER::copy(@_);
274  
275 203 ▶ if ($myformat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($myformat) 22 181 203
276  
277 # make sure we keep the original formatting
278 22 $new->format($myformat);
279 }
280  
281 # done...
282 203 return $new;
283 }
284  
285  
286  
287 sub as_int {
288 4 ▶ my $self = shift;
Subroutine as_int: called CC=2 SCAR=7.0
Pod documented
289 4 (my $str = $self->as_float) =~ s/\.//o;
290 4 $str =~ s/^(\-?)0+/$1/o;
291 4 ▶ return $str eq '' ? '0' : $str;
Branch: 1 branch
BranchTrueFalseTotal
$str eq '' ? : 0 4 4
292 }
293  
294 # we override the default here because we only want to compare the precision of
295 # the currency we're dealing with, not the precision of the underlying object
296 sub bcmp {
297 # See RT #115247, #115761
298 # bcmp() might get called in comparison overoad as an object method with
299 # one arg, or, as a class method with two args depending on the version of
300 # Math::BigInt that is installed. Workaround is to check if @_ has three
301 # args, and if so, the first one is the class name. Otherwise, the first
302 # arg is the left side is an object that bcmp() is called on.
303 # An alternate solution to this is to require Math::BigInt >= 1.999718
304 # which always uses bcmp() as an object method.
305 61 ▶ my $class = (@_ == 3) ? shift : __PACKAGE__;
Subroutine bcmp: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 12 49 61
306  
307 # make sure we're dealing with two Math::Currency objects
308 my ( $x, $y ) =
309 61 ▶ map { ref $_ ne $class ? $class->new($_) : $_ } @_[ 0, 1 ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne $class ? : 44 78 122
310  
311 61 return $x->as_float <=> $y->as_float;
312 }
313  
314  
315 sub localize {
316 14 ▶ my $self = shift;
Subroutine localize: called CC=23 SCAR=41.6
Pod documented
317 14 ▶ my $format = shift || \$FORMAT;
Condition50%1/2
shift() || \$FORMAT
l!l
014
MC/DC0%0/1
shift() || \$FORMAT
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
318  
319 14 my $localeconv = POSIX::localeconv();
320  
321 # localeconv()'s character encoding depends on the current locale setting,
322 # so it is necessary to decode the currency symbol.
323 14 Encode::Locale::reinit(); # neede in case locale was changed with setlocale()
324  
325 14 for my $key (keys %$localeconv) {
326 # POSIX::localeconv() changed behaviour between 5.20 and 5.22. As of
327 # 5.22, it sets the UTF-8 flag for localeconv() returned data if hte
328 # data is in UTF-8 format, so if POSIX::localeconv() already turned on
329 # the UTF-8 flag, we should not decode the data.
330 336 ▶ unless (utf8::is_utf8($$localeconv{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($localeconv->{$key})) 336 0 336
331 336 $$localeconv{$key} = Encode::decode(locale => $$localeconv{$key});
332 }
333 }
334  
335 # so you can test to see if locale was effective
336 14 ▶ return 0 if ! exists $localeconv->{'currency_symbol'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $localeconv->{"currency_symbol"} 0 14 14
337  
338 $$format = {
339 INT_CURR_SYMBOL => $localeconv->{'int_curr_symbol'} || '',
340 CURRENCY_SYMBOL => $localeconv->{'currency_symbol'} || '',
341 MON_DECIMAL_POINT => $localeconv->{'mon_decimal_point'} || '',
342 MON_THOUSANDS_SEP => $localeconv->{'mon_thousands_sep'} || '',
343 MON_GROUPING => (
344 exists $localeconv->{'mon_grouping'}
345 and defined $localeconv->{'mon_grouping'}
346 and ord( $localeconv->{'mon_grouping'} ) < 47
347 ? ord( $localeconv->{'mon_grouping'} )
348 : $localeconv->{'mon_grouping'}
349 )
350 || 0,
351 POSITIVE_SIGN => $localeconv->{'positive_sign'} || '',
352 NEGATIVE_SIGN => $localeconv->{'negative_sign'} || '-',
353 INT_FRAC_DIGITS => $localeconv->{'int_frac_digits'} || 0,
354 FRAC_DIGITS => $localeconv->{'frac_digits'} || 0,
355 P_CS_PRECEDES => $localeconv->{'p_cs_precedes'} || 0,
356 P_SEP_BY_SPACE => $localeconv->{'p_sep_by_space'} || 0,
357 N_CS_PRECEDES => $localeconv->{'n_cs_precedes'} || 0,
358 N_SEP_BY_SPACE => $localeconv->{'n_sep_by_space'} || 0,
359 P_SIGN_POSN => $localeconv->{'p_sign_posn'} || 1,
360 14 ▶ N_SIGN_POSN => $localeconv->{'n_sign_posn'} || 0,
Branch: 1 branch
BranchTrueFalseTotal
ord $localeconv->{'mon_grouping'} < 47 ? : 14 0 14
Condition47%17/36
$localeconv->{'int_curr_symbol'} || ''
l!l
014
$localeconv->{'currency_symbol'} || ''
l!l
014
$localeconv->{'mon_decimal_point'} || ''
l!l
014
$localeconv->{'mon_thousands_sep'} || ''
l!l
014
exists $localeconv->{'mon_grouping'} && defined $localeconv->{'mon_grouping'} && (ord $localeconv->{'mon_grouping'} < 47 ? ord $localeconv->{'mon_grouping'} : $localeconv->{'mon_grouping'}) || 0
l!l
014
exists $localeconv->{"mon_grouping"} and defined $localeconv->{"mon_grouping"} and ord $localeconv->{"mon_grouping"} < 47 ? ord $localeconv->{"mon_grouping"} : $localeconv->{"mon_grouping"}
!ll&&!rl&&r
0140
exists $localeconv->{"mon_grouping"} and defined $localeconv->{"mon_grouping"}
!ll&&!rl&&r
0014
$localeconv->{'positive_sign'} || ''
l!l
014
$localeconv->{'negative_sign'} || '-'
l!l
014
$localeconv->{'int_frac_digits'} || 0
l!l
140
$localeconv->{'frac_digits'} || 0
l!l
140
$localeconv->{'p_cs_precedes'} || 0
l!l
140
$localeconv->{'p_sep_by_space'} || 0
l!l
140
$localeconv->{'n_cs_precedes'} || 0
l!l
140
$localeconv->{'n_sep_by_space'} || 0
l!l
140
$localeconv->{'p_sign_posn'} || 1
l!l
140
$localeconv->{'n_sign_posn'} || 0
l!l
140
MC/DC0%0/1
$localeconv->{'int_curr_symbol'} || ''
$localeconv->{'int_curr_symbol'}
MC/DC0%0/1
$localeconv->{'currency_symbol'} || ''
$localeconv->{'currency_symbol'}
MC/DC0%0/1
$localeconv->{'mon_decimal_point'} || ''
$localeconv->{'mon_decimal_point'}
MC/DC0%0/1
$localeconv->{'mon_thousands_sep'} || ''
$localeconv->{'mon_thousands_sep'}
MC/DC0%0/3
exists $localeconv->{'mon_grouping'} && defined $localeconv->{'mon_grouping'} && (ord $localeconv->{'mon_grouping'} < 47 ? ord $localeconv->{'mon_grouping'} : $localeconv->{'mon_grouping'}) || 0
exists $localeconv->{"mon_grouping"} defined $localeconv->{"mon_grouping"} ord $localeconv->{"mon_grouping"} < 47 ? ord $localeconv->{"mon_grouping"} : $localeconv->{"mon_grouping"}
MC/DC0%0/1
$localeconv->{'positive_sign'} || ''
$localeconv->{'positive_sign'}
MC/DC0%0/1
$localeconv->{'negative_sign'} || '-'
$localeconv->{'negative_sign'}
MC/DC0%0/1
$localeconv->{'int_frac_digits'} || 0
$localeconv->{'int_frac_digits'}
MC/DC0%0/1
$localeconv->{'frac_digits'} || 0
$localeconv->{'frac_digits'}
MC/DC0%0/1
$localeconv->{'p_cs_precedes'} || 0
$localeconv->{'p_cs_precedes'}
MC/DC0%0/1
$localeconv->{'p_sep_by_space'} || 0
$localeconv->{'p_sep_by_space'}
MC/DC0%0/1
$localeconv->{'n_cs_precedes'} || 0
$localeconv->{'n_cs_precedes'}
MC/DC0%0/1
$localeconv->{'n_sep_by_space'} || 0
$localeconv->{'n_sep_by_space'}
MC/DC0%0/1
$localeconv->{'p_sign_posn'} || 1
$localeconv->{'p_sign_posn'}
MC/DC0%0/1
$localeconv->{'n_sign_posn'} || 0
$localeconv->{'n_sign_posn'}
Truth table50%1/2
A
A= $localeconv->{'int_curr_symbol'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'currency_symbol'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'mon_decimal_point'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'mon_thousands_sep'}
Aresult
11
00
Truth table25%1/4
A and B and C
A= exists $localeconv->{"mon_grouping"}B= defined $localeconv->{"mon_grouping"}C= ord $localeconv->{"mon_grouping"} < 47 ? ord $localeconv->{"mon_grouping"} : $localeconv->{"mon_grouping"}
ABCresult
1111
0XX0
10X0
1100
Truth table50%1/2
A
A= $localeconv->{'positive_sign'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'negative_sign'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'int_frac_digits'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'frac_digits'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'p_cs_precedes'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'p_sep_by_space'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'n_cs_precedes'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'n_sep_by_space'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'p_sign_posn'}
Aresult
11
00
Truth table50%1/2
A
A= $localeconv->{'n_sign_posn'}
Aresult
11
00
361 };
362  
363 14 return 1;
364 }
365  
366  
367 {
368 14 my $locales;
369 sub available_locales {
370 19 ▶ return if $^O =~ /Win32/; # cant run locale -a on windows
Subroutine available_locales: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /Win32/ 0 19 19
371  
372 19 ▶ unless (defined $locales) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $locales) 2 17 19
373 2 $locales = [];
374  
375 2 ▶ open my $fh, '-|', 'locale -a' or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "locale -a" or die $! 0 2 2
376  
377 2 ▶ while (my $locale = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $locale = readline $fh)) 6 2 8
378 6 chomp $locale;
379 6 push @$locales, $locale;
380 }
381  
382 2 close $fh;
383 }
384  
385 19 return @$locales;
386 }
387 }
388  
389 # if no currency module exists for the requested currency, this function tries
390 # to find the currency settings from the available locales
391 14 sub unknown_currency {
392 19 ▶ my ($currency) = @_;
Subroutine unknown_currency: called CC=6 SCAR=24.1
Pod undocumented
393  
394 # remember current locale
395 19 my $original_locale = setlocale( LC_ALL );
396  
397 # we need to save a copy of $VERSION here becuase the effective locale can
398 # render $VERSION as X,YY instead of Y.YY for exmaple
399 19 my $version = "$Math::Currency::VERSION";
400  
401 19 for my $LOCALE (available_locales()) {
402 57 setlocale( LC_ALL, $LOCALE );
403 57 my $localeconv = POSIX::localeconv();
404 57 ▶ if ( $LOCALE eq $currency or
Branch: 1 branch
BranchTrueFalseTotal
if ($LOCALE eq $currency or ($localeconv->{"int_curr_symbol"} || "") =~ /$currency/) 0 57 57
Condition40%2/5
$LOCALE eq $currency or ($localeconv->{"int_curr_symbol"} || "") =~ /$currency/
l!l&&r!l&&!r
0057
$localeconv->{'int_curr_symbol'} || ''
l!l
057
MC/DC0%0/2
$LOCALE eq $currency or ($localeconv->{"int_curr_symbol"} || "") =~ /$currency/
$LOCALE eq $currency $localeconv->{'int_curr_symbol'}
Truth table33%1/3
A or B
A= $LOCALE eq $currencyB= $localeconv->{'int_curr_symbol'}
ABresult
1X1
011
000
405 ($localeconv->{int_curr_symbol} || '') =~ /$currency/ )
406 {
407 0 my $format = \$LC_MONETARY->{$currency};
408 0 Math::Currency->localize($format);
409 0 (my $int_curr = $$format->{INT_CURR_SYMBOL}) =~ s/ //g;
410 $LC_MONETARY->{$int_curr} = $LC_MONETARY->{$currency}
411 0 ▶ unless exists $LC_MONETARY->{$int_curr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Math::Currency::LC_MONETARY->{$int_curr} 0 0 0
412 0 eval <<"EOP";
413 package Math::Currency::${LOCALE};
414  
415 use base 'Math::Currency';
416 our \$VERSION = $version;
417 our \$LANG = '$LOCALE';
418  
419 1;
420  
421 package Math::Currency::${int_curr};
422  
423 use base 'Math::Currency';
424  
425 our \$VERSION = $version;
426 our \$LANG = '$LOCALE';
427  
428 1;
429 EOP
430 0 last;
431 }
432 }
433  
434 # restore the original locale
435 19 setlocale( LC_ALL, $original_locale );
436 }
437  
438 # additional methods needed to get/set package globals
439  
440  
441 sub always_init {
442 1 ▶ my $class = shift;
Subroutine always_init: called CC=2 SCAR=7.6
Pod documented
443  
444 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
445 0 $always_init = shift;
446 }
447  
448 1 return $always_init;
449 }
450  
451  
452 sub use_int {
453 1 ▶ my $class = shift;
Subroutine use_int: called CC=2 SCAR=7.6
Pod documented
454  
455 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
456 0 $use_int = shift;
457 }
458  
459 1 return $use_int;
460 }
461  
462 14 1;
463  
464 __END__
↑ Summary Currency/USD.pm »