blib/lib/Lingua/pt_BR/Nums2Words.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 97.0% 33 / 34 Conditioncond 87.5% 7 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 96.4% 134 / 139 CC 22 SCAR 18.3
CC
22
Cov
98%
_solve_triads
10.0
_solve_triad
7.1
num2word
4.0
CRAP 6.2
↑ Summary
1 package Lingua::pt_BR::Nums2Words;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.01';
10 1 our @EXPORT_OK = qw/num2word/;
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Lingua::pt_BR::Nums2Words - Takes a number and gives back its written
17 form in Brazilian Portuguese
18  
19 =head1 SYNOPSIS
20  
21 use Lingua::pt_BR::Nums2Words ('num2word');
22  
23 print num2word(91) # prints 'noventa e um'
24 print num2word('19') # prints 'dezenove'
25  
26 print num2word(1000) # prints 'mil'
27 print num2word(1001) # prints 'mil e um'
28 print num2word(1_001_001) # prints 'um milhão, mil e um'
29 print num2word(1_001_250) # prints 'um milhão, mil duzentos e cinquenta'
30  
31 =head1 DESCRIPTION
32  
33 Takes a number and gives back its written form in Brazilian
34 Portuguese.
35  
36 B<Note>: 1000 will produce 'mil', and not 'um mil'.
37  
38 =head1 METHODS
39  
40 =cut
41  
42 1 my %cardinals;
43  
44 1 $cardinals{units} = [undef, qw/um dois três quatro cinco seis sete oito
45 nove/];
46  
47 1 $cardinals{first_tens} = [undef, qw/onze doze treze quatorze quinte dezesseis
48 dezesete dezoito dezenove/];
49  
50 1 $cardinals{tens} = [undef, qw/dez vinte trinta quarenta cinquenta sessenta
51 setenta oitenta noventa/];
52  
53 1 $cardinals{hundreds} = [undef, qw/cento duzentos trezentos quatrocentos
54 quinhentos seiscentos setecentos oitocentos novecentos/];
55  
56 1 $cardinals{megas} = [undef, qw/mil milh bilh trilh quadrilh quintilh/];
57  
58 =head2 num2word( $number )
59  
60 Receives a number and returns it written in words.
61  
62 my $written_number = nums2words(991);
63 print $written_number # prints 'novecentos e noventa e um'
64  
65 =cut
66  
67 sub num2word {
68 36 ▶ my $number = shift;
Subroutine num2word: called CC=4 SCAR=13.9
Pod documented
69 36 ▶ croak 'No argument provided' unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 1 35 36
70 35 ▶ croak "Not a workable number: $number" unless $number =~ /^\d{1,19}$/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /^\d{1,19}$/x 4 31 35
71 31 ▶ if ($number == 0) { return 'zero' }
Branch: 1 branch
BranchTrueFalseTotal
if ($number == 0) 2 29 31
72  
73 29 return _solve_triads( _make_triads($number) );
74 }
75  
76 =head1 INTERNALS
77  
78 These methods should not be used directly (unless you know what you're
79 doing). They are documented here just for the sake of completeness.
80  
81 =head2 _make_triads( $number )
82  
83 Receives a number, splits it in triads (according to the following
84 examples) and returns a list of triads. Examples: 123 turns to the
85 list (123). 12345 turns to the list (12, 345). 1234567 turns to the
86 list (1, 234, 567).
87  
88 =cut
89  
90 sub _make_triads {
91 29 ▶ my $number = shift;
Subroutine _make_triads: called CC=4 SCAR=13.9
92 29 my @triads;
93 29 ▶ my $offset = (length $number) % 3 || 3;
Condition100%2/2
length($number) % 3 || 3
l!l
227
MC/DC100%1/1
length($number) % 3 || 3
length($number) % 3
Truth table100%2/2
A
A= length($number) % 3
Aresult
11
00
94  
95 29 ▶ while (my $triad = substr $number, 0, $offset, '') {
Branch: 1 branch
BranchTrueFalseTotal
if (my $triad = substr($number, 0, $offset, '')) 79 29 108
96 79 push @triads, $triad;
97 79 ▶ if ($offset != 3) { $offset = 3 }
Branch: 1 branch
BranchTrueFalseTotal
if ($offset != 3) 22 57 79
98 }
99  
100 29 return @triads;
101 }
102  
103 =head2 _solve_triads( @triads )
104  
105 Receives a list of triads, calls the function _solve_triad in each of
106 them and apply the "megas" (millions, billions, trillions).
107  
108 =cut
109  
110 sub _solve_triads {
111 29 ▶ my @triads = @_;
Subroutine _solve_triads: called CC=10 SCAR=23.0
112 29 my $megas_counter = $#triads;
113 29 my @triads_str;
114  
115 29 for my $triad (@triads) {
116 79 ▶ if ($triad == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($triad == 0) 15 64 79
117 15 $megas_counter--;
118 15 next;
119 }
120  
121 64 my $triad_str = _solve_triad($triad);
122  
123 64 ▶ if ($megas_counter > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($megas_counter > 0) 36 28 64
124 36 my $mega = $cardinals{megas}->[$megas_counter];
125 36 ▶ if ($megas_counter > 1) { $mega .= $triad == 1 ? 'ão' : 'ões' }
Branch: 2 branches
BranchTrueFalseTotal
if ($megas_counter > 1) 20 16 36
$triad == 1 ? : 10 10 20
126 36 $triad_str .= " $mega";
127  
128 36 ▶ if ($triad_str eq 'um mil') { $triad_str = 'mil' }
Branch: 1 branch
BranchTrueFalseTotal
if ($triad_str eq "um mil") 6 30 36
129  
130 36 $megas_counter--;
131 }
132  
133 64 push @triads_str, $triad_str;
134 }
135  
136 29 my $resp_str;
137  
138 29 ▶ if (@triads_str == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triads_str == 1) { } 8 21 29
139 8 $resp_str = $triads_str[0];
140 }
141 else {
142 21 $resp_str .= join ', ', @triads_str[0 .. $#triads_str - 1];
143  
144 21 my $last_triad = $triads[-1];
145 21 my $last_triad_str;
146  
147 21 ▶ if ($last_triad % 100 == 0 || $last_triad < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_triad % 100 == 0 or $last_triad < 100) { } 13 8 21
Condition100%3/3
$last_triad % 100 == 0 or $last_triad < 100
l!l&&r!l&&!r
498
MC/DC100%2/2
$last_triad % 100 == 0 or $last_triad < 100
$last_triad % 100 == 0 $last_triad < 100
Truth table100%3/3
A or B
A= $last_triad % 100 == 0B= $last_triad < 100
ABresult
1X1
011
000
148 13 $last_triad_str = "e $triads_str[-1]";
149 }
150 else {
151 8 $last_triad_str = $triads_str[-1];
152 }
153  
154 21 $resp_str .= " $last_triad_str";
155 }
156  
157 29 return $resp_str;
158 }
159  
160 =head2 _solve_triad( $number )
161  
162 Receives a number with one to three digits (a triad) and returns it
163 written in words.
164  
165 =cut
166  
167 sub _solve_triad {
168 64 ▶ my $number = shift;
Subroutine _solve_triad: called CC=7 SCAR=19.5
169  
170 64 ▶ if ($number == 100) { return 'cem' }
Branch: 1 branch
BranchTrueFalseTotal
if ($number == 100) 1 63 64
171  
172 63 my $padded_number = sprintf "%03d", $number;
173 63 my ($hundreds, $tens, $units) = split '', $padded_number;
174  
175 63 my @resp;
176  
177 63 ▶ if ($hundreds) { push @resp, $cardinals{hundreds}->[$hundreds] }
Branch: 1 branch
BranchTrueFalseTotal
if ($hundreds) 21 42 63
178  
179 63 my $first_tens = $tens . $units;
180  
181 63 ▶ if ($first_tens > 10 and $first_tens < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_tens > 10 and $first_tens < 20) { } 0 63 63
Condition66%2/3
$first_tens > 10 and $first_tens < 20
!ll&&!rl&&r
5580
MC/DC0%0/2
$first_tens > 10 and $first_tens < 20
$first_tens > 10 $first_tens < 20
Truth table66%2/3
A and B
A= $first_tens > 10B= $first_tens < 20
ABresult
0X0
100
111
182 0 push @resp, $cardinals{first_tens}->[$units];
183 }
184 else {
185 63 ▶ if ($tens) { push @resp, $cardinals{tens}->[$tens] }
Branch: 1 branch
BranchTrueFalseTotal
if ($tens) 8 55 63
186 63 ▶ if ($units) { push @resp, $cardinals{units}->[$units] }
Branch: 1 branch
BranchTrueFalseTotal
if ($units) 48 15 63
187 }
188  
189 63 return join ' e ', @resp;
190 }
191  
192 1 1;
193  
194 =head1 SEE ALSO
195  
196 Lingua::PT::Nums2Words for pt_PT Portuguese.
197  
198 =head1 AUTHOR
199  
200 Gil Magno E<lt>gils@cpan.orgE<gt>
201  
202 =head1 COPYRIGHT AND LICENSE
203  
204 Copyright (C) 2015 by Gil Magno
205  
206 This library is free software; you can redistribute it and/or modify
207 it under the same terms as Perl itself, either Perl version 5.20.1 or,
208 at your option, any later version of Perl 5 you may have available.
209  
210 =cut
↑ Summary