blib/lib/Lingua/MLT/Num2Word.pm

filter: Statementstmt 78.5% 77 / 98 Branchbran 55.0% 22 / 40 Conditioncond 40.0% 6 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 68.7% 119 / 173 CC 26 SCAR 36.6
CC
26
Cov
69%
_convert
62.7
num2mlt_cardinal
6.2
BEGIN
1.0
CRAP 38.9
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2 package Lingua::MLT::Num2Word;
3 # ABSTRACT: Number to word conversion in Maltese
4  
5 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # {{{ use block
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # }}}
16 # {{{ variable declarations
17  
18 1 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
19 1 our $VERSION = '0.2603270';
20  
21 # }}}
22  
23 # {{{ num2mlt_cardinal convert number to text
24  
25 sub num2mlt_cardinal :Export {
26 27 ▶ my $positive = shift;
Subroutine num2mlt_cardinal: called CC=6 SCAR=18.3
Pod documented
27  
28 27 ▶ croak 'You should specify a number from interval [0, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999 0 27 27
Condition33%3/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
l!l&&r!l&&!r
0027
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
l!l&&r!l&&!r
0027
defined $positive && $positive =~ /\A\d+\z/msux
!ll&&!rl&&r
0027
MC/DC0%0/4
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
defined $positive $positive =~ /\A\d+\z/msux $positive < 0 $positive > 999999999
Truth table20%1/5
not(A && B) or C or D
A= defined $positiveB= $positive =~ /\A\d+\z/msuxC= $positive < 0D= $positive > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
29 if !defined $positive
30 || $positive !~ m{\A\d+\z}xms
31 || $positive < 0
32 || $positive > 999_999_999;
33  
34 # Units (standalone / counting forms)
35 27 my @ones = (
36 "żero", # 0
37 "wieħed", # 1
38 "tnejn", # 2
39 "tlieta", # 3
40 "erbgħa", # 4
41 "ħamsa", # 5
42 "sitta", # 6
43 "sebgħa", # 7
44 "tmienja", # 8
45 "disgħa", # 9
46 );
47  
48 # Tens
49 27 my @tens_word = (
50 q{}, # 0 (unused)
51 "għaxra", # 10
52 "għoxrin", # 20
53 "tletin", # 30
54 "erbgħin", # 40
55 "ħamsin", # 50
56 "sittin", # 60
57 "sebgħin", # 70
58 "tmenin", # 80
59 "disgħin", # 90
60 );
61  
62 # Teens (11-19)
63 27 my @teens;
64 27 $teens[11] = "ħdax";
65 27 $teens[12] = "tnax";
66 27 $teens[13] = "tlettax";
67 27 $teens[14] = "erbatax";
68 27 $teens[15] = "ħmistax";
69 27 $teens[16] = "sittax";
70 27 $teens[17] = "sbatax";
71 27 $teens[18] = "tmintax";
72 27 $teens[19] = "dsatax";
73  
74 # Connector
75 27 my $u = " u ";
76  
77 # Hundred forms — construct-state prefixes for 3-9
78 27 my $mija = "mija";
79 27 my $mitejn = "mitejn";
80 27 my @hund_prefix = (
81 q{}, # 0
82 q{}, # 1
83 q{}, # 2
84 "tliet", # 3
85 "erba'", # 4 erba' mija
86 "ħames", # 5 ħames mija (alt: hames)
87 "sitt", # 6
88 "seba'", # 7
89 "tminn", # 8
90 "disa'", # 9
91 );
92  
93 # Thousand forms — construct-state prefixes for 3-10
94 27 my $elf = "elf";
95 27 my $elfejn = "elfejn";
96 27 my @thou_prefix = (
97 q{}, # 0
98 q{}, # 1
99 q{}, # 2
100 "tlitt", # 3 tlitt elef
101 "erbat", # 4
102 "ħamest", # 5
103 "sitt", # 6
104 "sebat", # 7
105 "tmint", # 8
106 "disat", # 9
107 "għaxart", # 10 għaxart elef
108 );
109 27 my $elef = "elef"; # plural form for 3-10
110  
111 # Million forms
112 27 my $miljun = "miljun";
113 27 my $miljunejn = "żewġ miljuni"; # 2 million
114  
115 27 ▶ return $ones[0] if $positive == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 0 2 25 27
116  
117 25 return _convert($positive, \@ones, \@tens_word, \@teens,
118 $u, $mija, $mitejn, \@hund_prefix,
119 $elf, $elfejn, $elef, \@thou_prefix,
120 $miljun, $miljunejn);
121 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 # }}}
124 # {{{ _convert recursive number-to-word engine
125  
126 sub _convert {
127 27 ▶ my ($n, $ones, $tens_word, $teens,
Subroutine _convert: called CC=21 SCAR=41.4
128 $u, $mija, $mitejn, $hund_prefix,
129 $elf, $elfejn, $elef, $thou_prefix,
130 $miljun, $miljunejn) = @_;
131  
132 27 my @params = ($ones, $tens_word, $teens,
133 $u, $mija, $mitejn, $hund_prefix,
134 $elf, $elfejn, $elef, $thou_prefix,
135 $miljun, $miljunejn);
136  
137 27 ▶ return q{} if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 27 27
138  
139 # --- Millions ---
140 27 ▶ if ($n >= 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1000000) 0 27 27
141 0 my $millions = int($n / 1_000_000);
142 0 my $remain = $n % 1_000_000;
143  
144 0 my $out;
145 0 ▶ if ($millions == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($millions == 1) { } 0 0 0
elsif ($millions == 2) { } 0 0 0
146 0 $out = $miljun;
147 }
148 elsif ($millions == 2) {
149 0 $out = $miljunejn;
150 }
151 else {
152 0 $out = _convert($millions, @params) . " $miljun";
153 }
154  
155 0 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 0 0 0
156 0 $out .= "${u}" . _convert($remain, @params);
157 }
158 0 return $out;
159 }
160  
161 # --- Thousands ---
162 27 ▶ if ($n >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1000) 0 27 27
163 0 my $thousands = int($n / 1000);
164 0 my $remain = $n % 1000;
165  
166 0 my $out;
167 0 ▶ if ($thousands == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($thousands == 1) { } 0 0 0
elsif ($thousands == 2) { } 0 0 0
elsif ($thousands >= 3 and $thousands <= 10) { } 0 0 0
168 0 $out = $elf;
169 }
170 elsif ($thousands == 2) {
171 0 $out = $elfejn;
172 }
173 ▶ elsif ($thousands >= 3 && $thousands <= 10) {
Condition0%0/3
$thousands >= 3 and $thousands <= 10
!ll&&!rl&&r
000
MC/DC0%0/2
$thousands >= 3 and $thousands <= 10
$thousands >= 3 $thousands <= 10
Truth table0%0/3
A and B
A= $thousands >= 3B= $thousands <= 10
ABresult
0X0
100
111
174 0 $out = $thou_prefix->[$thousands] . " $elef";
175 }
176 else {
177 # 11+ thousands: compound number + elf
178 0 $out = _convert($thousands, @params) . " $elf";
179 }
180  
181 0 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 0 0 0
182 0 $out .= "${u}" . _convert($remain, @params);
183 }
184 0 return $out;
185 }
186  
187 # --- Hundreds ---
188 27 ▶ if ($n >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 100) 6 21 27
189 6 my $hundreds = int($n / 100);
190 6 my $remain = $n % 100;
191  
192 6 my $out;
193 6 ▶ if ($hundreds == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hundreds == 1) { } 2 4 6
elsif ($hundreds == 2) { } 2 2 4
194 2 $out = $mija;
195 }
196 elsif ($hundreds == 2) {
197 2 $out = $mitejn;
198 }
199 else {
200 2 $out = $hund_prefix->[$hundreds] . " $mija";
201 }
202  
203 6 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 2 4 6
204 2 $out .= "${u}" . _convert($remain, @params);
205 }
206 6 return $out;
207 }
208  
209 # --- Teens (11-19) ---
210 21 ▶ if ($n >= 11 && $n <= 19) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 11 and $n <= 19) 5 16 21
Condition100%3/3
$n >= 11 and $n <= 19
!ll&&!rl&&r
795
MC/DC100%2/2
$n >= 11 and $n <= 19
$n >= 11 $n <= 19
Truth table100%3/3
A and B
A= $n >= 11B= $n <= 19
ABresult
0X0
100
111
211 5 return $teens->[$n];
212 }
213  
214 # --- Ten ---
215 16 ▶ if ($n == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 10) 1 15 16
216 1 return $tens_word->[1];
217 }
218  
219 # --- Tens with units (21-99) ---
220 15 ▶ if ($n >= 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 20) 9 6 15
221 9 my $ten_idx = int($n / 10);
222 9 my $unit = $n % 10;
223  
224 9 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 4 5 9
225 4 return $ones->[$unit] . "${u}" . $tens_word->[$ten_idx];
226 }
227 5 return $tens_word->[$ten_idx];
228 }
229  
230 # --- Units (1-9) ---
231 6 return $ones->[$n];
232 }
233  
234 # }}}
235  
236 # {{{ capabilities declare supported features
237  
238 sub capabilities {
239 return {
240 1 ▶ cardinal => 1,
Subroutine capabilities: called CC=1 SCAR=0.0
Pod documented
241 ordinal => 0,
242 };
243 }
244  
245 # }}}
246 1 1;
247  
248 __END__
↑ Summary