blib/lib/Lingua/ITA/Numbers.pm

filter: Statementstmt 68.6% 103 / 150 Branchbran 48.3% 30 / 62 Conditioncond 66.6% 10 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 68.1% 15 / 22 Podpod 100% 13 / 13 totaltotal 64.3% 175 / 272 CC 38 SCAR 26.5
CC
38
Cov
63%
number_to_it
37.1
convert_to_string
16.8
div
12.0
CRAP 14.2
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2  
3 package Lingua::ITA::Numbers;
4 # ABSTRACT: Number 2 Word conversion in ITA.
5  
6 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # {{{ use block
11  
12 use overload (
13 1 '""' => \&get_string,
14 '0+' => \&get_number,
15 '=' => \&clone,
16 '+' => \&add,
17 '-' => \&minus,
18 '*' => \&mult,
19 '/' => \&div,
20 1 ▶ fallback => 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # }}}
26 # {{{ variables declaration
27 1 our $VERSION = '0.2603260';
28  
29 1 our %SIGN_NAMES = ('-' => 'meno',
30 '+' => '');
31  
32 1 our %OUTPUT_DECIMAL_DELIMITERS = (normal => 'virgola',
33 fract => 'e');
34  
35 1 our %NUMBER_NAMES = (
36 0 => 'zero',
37 1 => 'un',
38 2 => 'due',
39 3 => 'tre',
40 4 => 'quattro',
41 5 => 'cinque',
42 6 => 'sei',
43 7 => 'sette',
44 8 => 'otto',
45 9 => 'nove',
46 10 => 'dieci',
47 11 => 'undici',
48 12 => 'dodici',
49 13 => 'tredici',
50 14 => 'quattordici',
51 15 => 'quindici',
52 16 => 'sedici',
53 17 => 'diciassette',
54 18 => 'diciotto',
55 19 => 'diciannove',
56 20 => 'venti',
57 30 => 'trenta',
58 40 => 'quaranta',
59 50 => 'cinquanta',
60 60 => 'sessanta',
61 70 => 'settanta',
62 80 => 'ottanta',
63 90 => 'novanta',
64 100 => 'cento');
65  
66 1 our @PART_NAMES = qw(_ mila milioni miliardi);
67 1 our @UNITS_NAMES = qw(_ mille milione miliardo);
68 1 our @FRACT_NAMES = qw(decim centesim millesim decimillesim centomillesim
69 milionesim decimilionesim centomilionesim
70 miliardesim);
71  
72 1 our %DEFAULT_OPTS = (
73 decimal => 0,
74 decmode => 'normal',
75 name => "",
76 );
77  
78 # }}}
79 # {{{ number_to_it
80  
81 sub number_to_it :Export {
82 17 ▶ my ($number,$opts) = @_;
Subroutine number_to_it: called CC=11 SCAR=36.1
Pod documented
83 17 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 8 9 17
84 17 $opts = {%DEFAULT_OPTS,%$opts};
85 17 my $parsed = parse_num_string($number);
86 17 my @parts = ();
87 17 push @parts,$SIGN_NAMES{$parsed->{sign}};
88 17 my $intpart = convert_to_string($parsed->{intpart});
89 17 my $one = $NUMBER_NAMES{1};
90 17 $intpart =~ s/($one)$/$1o/;
91 17 push @parts,$intpart;
92 17 ▶ if ($opts->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"name"}) 0 17 17
93 my $name = ! ref($opts->{name}) ? $opts->{name} :
94 0 ▶ ($intpart eq $NUMBER_NAMES{1} ? $opts->{name}[0] : $opts->{name}[1]);
Branch: 2 branches
BranchTrueFalseTotal
!ref($opts->{'name'}) ? : 0 0 0
$intpart eq $Lingua::ITA::Numbers::NUMBER_NAMES{'1'} ? : 0 0 0
95  
96 0 push @parts,$name;
97 }
98  
99 17 ▶ if ($parsed->{fracpart} || $opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"fracpart"} or $opts->{"decimal"}) 0 17 17
Condition33%1/3
$parsed->{"fracpart"} or $opts->{"decimal"}
l!l&&r!l&&!r
0017
MC/DC0%0/2
$parsed->{"fracpart"} or $opts->{"decimal"}
$parsed->{"fracpart"} $opts->{"decimal"}
Truth table33%1/3
A or B
A= $parsed->{"fracpart"}B= $opts->{"decimal"}
ABresult
1X1
011
000
100 0 push @parts,$OUTPUT_DECIMAL_DELIMITERS{$opts->{decmode}};
101 0 ▶ if ($opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"decimal"}) 0 0 0
102 0 ▶ if (length($parsed->{fracpart}) < $opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parsed->{"fracpart"} < $opts->{"decimal"}) 0 0 0
103 $parsed->{fracpart} .= "0" x ($opts->{decimal} -
104 0 length($parsed->{fracpart}));
105 }
106 }
107 0 my $fractpart = convert_to_string($parsed->{fracpart});
108 0 ▶ if ($opts->{decmode} eq 'fract') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'decmode'} eq 'fract') { } 0 0 0
109 0 ▶ if ($fractpart eq $NUMBER_NAMES{1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fractpart eq $Lingua::ITA::Numbers::NUMBER_NAMES{'1'}) { } 0 0 0
110 0 push @parts,$fractpart,$FRACT_NAMES[length($parsed->{fracpart}) - 1] . "o";
111 }
112 else {
113 0 push @parts,$fractpart,$FRACT_NAMES[length($parsed->{fracpart}) - 1] . "i";
114 }
115 }
116 else {
117 0 my $one = $NUMBER_NAMES{1};
118 0 $fractpart =~ s/($one)$/$1o/;
119 0 push @parts,$fractpart;
120 }
121 }
122 17 my $result = join(" ",@parts);
123 17 $result =~ s/^\s*//;
124 17 return $result;
125 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 # }}}
128 # {{{ convert_short
129  
130 sub convert_short {
131 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 22 ▶ my $num = shift; # 1 < num < 1000
Subroutine convert_short: called CC=8 SCAR=20.8
Pod documented
133  
134 22 my $hundreds = $num / 100;
135 22 my $tens = $num % 100;
136 22 my @parts = ();
137 22 ▶ if ($hundreds == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hundreds == 1) { } 5 17 22
elsif ($hundreds > 1) { } 8 9 17
138 5 push @parts,$NUMBER_NAMES{100};
139 }
140 elsif ($hundreds > 1) {
141 8 push @parts,$NUMBER_NAMES{$hundreds},$NUMBER_NAMES{100};
142 }
143 22 ▶ if ($tens == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tens == 0) { } 2 20 22
elsif ($tens <= 20) { } 6 14 20
144 #nothing
145 ;
146 }
147 elsif ($tens <= 20) {
148 6 push @parts,$NUMBER_NAMES{$tens};
149 }
150 else {
151 14 my $units = $tens % 10;
152 14 $tens = $tens - $units;
153 14 my $tenstr = $NUMBER_NAMES{$tens};
154 14 ▶ $tenstr =~ s/.$// if ($units == 1) or ($units == 8);
Branch: 1 branch
BranchTrueFalseTotal
if $units == 1 or $units == 8 7 7 14
Condition100%3/3
$units == 1 or $units == 8
l!l&&r!l&&!r
437
MC/DC100%2/2
$units == 1 or $units == 8
$units == 1 $units == 8
Truth table100%3/3
A or B
A= $units == 1B= $units == 8
ABresult
1X1
011
000
155 14 push @parts,$tenstr;
156 14 ▶ if ($units >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units >= 1) 14 0 14
157 14 push @parts,$NUMBER_NAMES{$units};
158 }
159 }
160 22 return join("",@parts);
161 }
162  
163 # }}}
164 # {{{ convert_to_string
165  
166 sub convert_to_string {
167 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 17 ▶ my $number = shift; #$number >= 0 and integer
Subroutine convert_to_string: called CC=15 SCAR=28.2
Pod documented
169 17 ▶ return $NUMBER_NAMES{0} if !$number || $number !~ m/[1-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $number and $number =~ /[1-9]/u 1 16 17
Condition66%2/3
$number and $number =~ /[1-9]/u
!ll&&!rl&&r
1016
MC/DC50%1/2
$number and $number =~ /[1-9]/u
$number $number =~ /[1-9]/u
Truth table66%2/3
A and B
A= $numberB= $number =~ /[1-9]/u
ABresult
0X0
100
111
170 16 ▶ return $NUMBER_NAMES{1} if "$number" eq "1" ;
Branch: 1 branch
BranchTrueFalseTotal
if "$number" eq 1 2 14 16
171 14 ▶ if (my $r = length($number) % 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = length($number) % 3) 9 5 14
172 9 $number = "0" x (3 - $r) . $number;
173 }
174 14 my @blocks = ($number =~ m!(\d\d\d)!g);
175 14 @blocks = reverse @blocks;
176 14 ▶ if (@blocks > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks > 4) 0 14 14
177 0 carp "Numbers bigger than 1e10-1 not handled in version $VERSION";
178 0 return;
179 }
180 14 my @name_parts = ();
181 14 my $firstpart = "";
182 14 ▶ if ($blocks[0] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blocks[0] == 1) { } 2 12 14
elsif ($blocks[0] > 1) { } 12 0 12
183 #nb one of the following blocks is != 0, since the whole number
184 #is greater than one
185 2 $firstpart = $NUMBER_NAMES{1};
186 }
187 elsif ($blocks[0] > 1) {
188 12 $firstpart = convert_short($blocks[0]);
189 }
190 14 ▶ if ($#blocks >= 1 && $blocks[1] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#blocks >= 1 and $blocks[1] == 1) { } 0 14 14
elsif ($#blocks >= 1 and $blocks[1] > 1) { } 6 16 22
Condition66%2/3
$#blocks >= 1 and $blocks[1] == 1
!ll&&!rl&&r
860
MC/DC0%0/2
$#blocks >= 1 and $blocks[1] == 1
$#blocks >= 1 $blocks[1] == 1
Truth table66%2/3
A and B
A= $#blocks >= 1B= $blocks[1] == 1
ABresult
0X0
100
111
191 0 $firstpart = $UNITS_NAMES[1] . $firstpart;
192 }
193 ▶ elsif ($#blocks >= 1 && $blocks[1] > 1) {
Condition66%2/3
$#blocks >= 1 and $blocks[1] > 1
!ll&&!rl&&r
806
MC/DC50%1/2
$#blocks >= 1 and $blocks[1] > 1
$#blocks >= 1 $blocks[1] > 1
Truth table66%2/3
A and B
A= $#blocks >= 1B= $blocks[1] > 1
ABresult
0X0
100
111
194 6 $firstpart = convert_short($blocks[1]) . $PART_NAMES[1] . $firstpart;
195 }
196 14 push @name_parts,$firstpart;
197 14 foreach my $pos (2..$#blocks) {
198 4 ▶ next unless $blocks[$pos];
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks[$pos] 0 4 4
199 4 push @name_parts," ";
200 4 ▶ if ($blocks[$pos] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks[$pos] == 1) { } 0 4 4
201 0 push @name_parts,$NUMBER_NAMES{1} . " " . $UNITS_NAMES[$pos];
202 }
203 else {
204 4 my $part = convert_short($blocks[$pos]);
205 4 push @name_parts,$part. " " . $PART_NAMES[$pos];
206 }
207 }
208 14 my $tmp = join("",reverse(@name_parts));
209 14 $tmp =~ s/^\s*//;
210 14 $tmp =~ s/\s*$//;
211 14 $tmp =~ s!\s+! !g;
212 14 return $tmp;
213 }
214  
215 # }}}
216 # {{{ parse_num_string
217  
218 sub parse_num_string {
219 17 ▶ my $string = shift;
Subroutine parse_num_string: called CC=1 SCAR=0.0
Pod documented
220  
221 17 return { intpart => $string,
222 sign => '+',
223 fracpart => 0,
224 };
225 }
226  
227 # }}}
228  
229 # OO Methods
230 # {{{ new
231 sub new {
232 9 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
233 9 my $number = shift;
234 9 my @a = @_;
235 9 my %opts = (%DEFAULT_OPTS,@a);
236 9 return bless { number => $number,
237 opts => \%opts}, $class;
238 }
239  
240 # }}}
241 # {{{ get_string
242  
243 sub get_string {
244 9 ▶ my $self = shift;
Subroutine get_string: called CC=1 SCAR=0.0
Pod documented
245 9 return number_to_it($self->{number},$self->{opts});
246 }
247  
248 # }}}
249 # {{{ get_number
250  
251 sub get_number {
252 0 ▶ my $self = shift;
Subroutine get_number: not called CC=1 SCAR=6.9
Pod documented
253 return $self->{number}
254 0 }
255  
256 # }}}
257 # {{{ set_number
258  
259 sub set_number {
260 0 ▶ my $self = shift;
Subroutine set_number: not called CC=1 SCAR=6.9
Pod documented
261 0 $self->{number} = shift;
262 0 return $self;
263 }
264  
265 # }}}
266 # {{{ add
267  
268 sub add {
269 0 ▶ my $self = shift;
Subroutine add: not called CC=2 SCAR=17.9
Pod documented
270 0 my $num = shift;
271 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
272 0 my $tmp = $self->{number} + $num;
273 return bless {number => $tmp,
274 0 opts => $self->{opts}},ref($self);
275 }
276  
277 # }}}
278 # {{{ mult
279  
280 sub mult {
281 0 ▶ my $self = shift;
Subroutine mult: not called CC=2 SCAR=17.9
Pod documented
282 0 my $num = shift;
283 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
284 return bless {number => $self->{number} * $num,
285 0 opts => $self->{opts}},ref($self);
286 }
287  
288 # }}}
289 # {{{ div
290  
291 sub div {
292 0 ▶ my $self = shift;
Subroutine div: not called CC=3 SCAR=24.8
Pod documented
293 0 my $num = shift;
294 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
295 0 my $inverted = shift;
296 my $tmp =
297 0 ▶ ($inverted) ? $num / $self->{number} : $self->{number} / $num;
Branch: 1 branch
BranchTrueFalseTotal
$inverted ? : 0 0 0
298 return bless {number => $tmp,
299 0 opts => $self->{opts}},ref($self);
300 }
301  
302 # }}}
303 # {{{ minus
304  
305 sub minus {
306 0 ▶ my $self = shift;
Subroutine minus: not called CC=3 SCAR=24.8
Pod documented
307 0 my $num = shift;
308 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
309 0 my $inverted = shift;
310 my $tmp =
311 0 ▶ ($inverted) ? $num - $self->{number} : $self->{number} - $num;
Branch: 1 branch
BranchTrueFalseTotal
$inverted ? : 0 0 0
312 return bless {number => $tmp,
313 0 opts => $self->{opts}},ref($self);
314 }
315  
316 # }}}
317 # {{{ clone
318  
319 sub clone {
320 0 ▶ my $self = shift;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
321 0 my $class = ref($self);
322 0 return bless {%$self},$class;
323 }
324  
325 # }}}
326  
327 1 1;
328 __END__
↑ Summary