blib/lib/Lingua/Num2Word.pm

filter: Statementstmt 49.6% 67 / 135 Branchbran 12.9% 8 / 62 Conditioncond 11.1% 5 / 45 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 80.9% 17 / 21 Podpod 62.5% 5 / 8 totaltotal 35.2% 104 / 295 CC 42 SCAR 40.1
CC
42
Cov
33%
capabilities
132.0
ordinal
110.0
cardinal
54.9
CRAP 55.0
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2 #
3 # Copyright (c) PetaMem, s.r.o. 2002-present
4 #
5 package Lingua::Num2Word;
6 # ABSTRACT: Number to word conversion
7  
8 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # {{{ use block
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # }}}
19 # {{{ var block
20 1 our $VERSION = '0.2603270';
21 1 our %known;
22  
23 # }}}
24  
25 # {{{ templates for functional and object interface
26  
27 1 my $template_func = q{ use __PACKAGE_WITH_VERSION__ ();
28 $result = __PACKAGE__::__FUNCTION__($number);
29 };
30  
31 1 my $template_obj = q{ use __PACKAGE_WITH_VERSION__ ();
32 my $tmp_obj = new __PACKAGE__;
33 $result = $tmp_obj->__FUNCTION__($number);
34 };
35  
36 # ISO 639-1 to 639-3 mapping for supported languages
37 # {{{ ISO 639-1 to 639-3 mapping
38 1 my %iso1_to_3 = (
39 af => 'afr', ar => 'ara', bg => 'bul', ca => 'cat',
40 cs => 'ces', da => 'dan', de => 'deu', el => 'ell',
41 en => 'eng', es => 'spa', et => 'est', eu => 'eus',
42 fa => 'fas', fi => 'fin', fr => 'fra', he => 'heb',
43 hi => 'hin', hr => 'hrv', hu => 'hun', id => 'ind',
44 is => 'isl', it => 'ita', ja => 'jpn', ko => 'kor',
45 lt => 'lit', lv => 'lav', nl => 'nld', 'no' => 'nor',
46 pl => 'pol', pt => 'por', ro => 'ron', ru => 'rus',
47 sk => 'slk', sv => 'swe', sw => 'swa', th => 'tha',
48 be => 'bel', cy => 'cym', ga => 'gle', gl => 'glg',
49 lb => 'ltz', mk => 'mkd', mt => 'mlt', oc => 'oci',
50 sc => 'srd',
51 sl => 'slv', sq => 'sqi', sr => 'srp', tr => 'tur',
52 uk => 'ukr', vi => 'vie', zh => 'zho',
53 );
54 # }}}
55  
56 # {{{ %known — auto-discovered from lib/Lingua/*/Num2Word.pm
57  
58 # Override table for legacy modules with non-standard API
59 # Override table: only for modules with non-default limits or legacy function names.
60 # All modules now live in Num2Word.pm with num2XXX_cardinal as canonical function.
61 1 my %n2w_override = (
62 afr => { limit_hi => 99_999_999_999 },
63 eng => { limit_lo => 1, limit_hi => 999_999_999_999_999 },
64 eus => { limit_hi => 999_999_999_999 },
65 fra => { limit_hi => 999_999_999_999_999 },
66 ind => { limit_hi => 999_999_999_999_999 },
67 ita => { limit_hi => 999_999_999_999 },
68 jpn => { limit_lo => 1, limit_hi => 999_999_999_999_999,
69 code => q{ use __PACKAGE_WITH_VERSION__ ();
70 my @words = __PACKAGE__::to_string($number);
71 $result = join ' ', @words;
72 },
73 },
74 nor => { function => 'num2no_cardinal', code => $template_obj },
75 pol => { limit_hi => 9_999_999_999_999 },
76 por => { limit_hi => 999_999_999_999_999 },
77 rus => { limit_hi => 999_999_999_999_999 },
78 spa => { limit_hi => 999_999_999_999_999 },
79 swe => { function => 'num2sv_cardinal' },
80 zho => { limit_lo => 1, limit_hi => 999_999_999_999_999,
81 code => q{ use Lingua::ZHO::Num2Word qw(traditional);
82 $result = Lingua::ZHO::Num2Word::number_to_zh($number);
83 },
84 },
85 );
86  
87 # auto-discover: scan for Lingua::*::Num2Word modules
88 {
89 1 my $lingua_dir;
90 1 for my $inc (@INC) {
91 2 my $try = "$inc/Lingua";
92 2 ▶ if (-d $try) { $lingua_dir = $try; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $try) 1 1 2
93 }
94  
95 1 ▶ if ($lingua_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lingua_dir) 1 0 1
96 1 for my $dir (glob "$lingua_dir/*/") {
97 0 my ($lang) = $dir =~ m{/([A-Z]{3})/\z};
98 0 ▶ next unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 0 0
99 0 $lang = lc $lang;
100  
101 # standard pattern: Num2Word.pm with num2XXX_cardinal function
102 0 ▶ if (-e "$dir/Num2Word.pm" || exists $n2w_override{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}) 0 0 0
Condition0%0/3
-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}
-e "$dir/Num2Word.pm" exists $n2w_override{$lang}
Truth table0%0/3
A or B
A= -e "$dir/Num2Word.pm"B= exists $n2w_override{$lang}
ABresult
1X1
011
000
103 0 ▶ my $ov = $n2w_override{$lang} // {};
Condition0%0/2
$n2w_override{$lang} // {}
l!l
00
MC/DC0%0/1
$n2w_override{$lang} // {}
$n2w_override{$lang}
Truth table0%0/2
A
A= $n2w_override{$lang}
Aresult
11
00
104 $known{$lang} = {
105 package => $ov->{package} // 'Num2Word',
106 version => $ov->{version} // '',
107 limit_lo => $ov->{limit_lo} // 0,
108 limit_hi => $ov->{limit_hi} // 999_999_999,
109 function => $ov->{function} // "num2${lang}_cardinal",
110 0 ▶ code => $ov->{code} // $template_func,
Condition0%0/13
$ov->{"package"} // "Num2Word"
l!l
00
$ov->{"version"} // ""
l!l
00
$ov->{"limit_lo"} // 0
l!l
00
$ov->{"limit_hi"} // 999999999
l!l
00
$ov->{"function"} // "num2${lang}_cardinal"
l!l
00
$ov->{"code"} // $template_func
l!l&&r!l&&!r
000
MC/DC0%0/1
$ov->{"package"} // "Num2Word"
$ov->{"package"}
MC/DC0%0/1
$ov->{"version"} // ""
$ov->{"version"}
MC/DC0%0/1
$ov->{"limit_lo"} // 0
$ov->{"limit_lo"}
MC/DC0%0/1
$ov->{"limit_hi"} // 999999999
$ov->{"limit_hi"}
MC/DC0%0/1
$ov->{"function"} // "num2${lang}_cardinal"
$ov->{"function"}
MC/DC0%0/2
$ov->{"code"} // $template_func
$ov->{"code"} $template_func
Truth table0%0/2
A
A= $ov->{"package"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"version"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"limit_lo"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"limit_hi"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"function"}
Aresult
11
00
Truth table0%0/3
A // B
A= $ov->{"code"}B= $template_func
ABresult
1X1
011
000
111 };
112 }
113 }
114 }
115 }
116  
117 # }}}
118 # {{{ default capabilities
119  
120 1 my %default_capabilities = (
121 cardinal => 1,
122 ordinal => 0,
123 negative => 0,
124 decimal => 0,
125 currency => 0,
126 );
127  
128 # }}}
129 # {{{ capabilities query what a language module can do
130  
131 sub capabilities :Export {
132 0 ▶ my $self = ref($_[0]) ? shift : undef;
Subroutine capabilities: not called CC=11 SCAR=48.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
133 0 ▶ my $lang = shift // return;
Condition0%0/2
shift() // (return)
l!l
00
MC/DC0%0/1
shift() // (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
134  
135 0 $lang = lc $lang;
136 0 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 0 0
137  
138 0 ▶ return if !exists $known{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Lingua::Num2Word::known{$lang} 0 0 0
139  
140 # try to load the module's capabilities() if it has one
141 0 my $pkg = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package};
142 0 my $caps;
143 0 eval {
144 0 (my $file = $pkg) =~ s{::}{/}g;
145 0 require "$file.pm";
146 0 ▶ if ($pkg->can('capabilities')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->can("capabilities")) 0 0 0
147 0 $caps = $pkg->capabilities();
148 }
149 };
150  
151 # merge with defaults — module caps override defaults
152 0 my %result = %default_capabilities;
153 0 ▶ if ($caps && ref $caps eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($caps and ref $caps eq "HASH") 0 0 0
Condition0%0/3
$caps and ref $caps eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$caps and ref $caps eq "HASH"
$caps ref $caps eq "HASH"
Truth table0%0/3
A and B
A= $capsB= ref $caps eq "HASH"
ABresult
0X0
100
111
154 0 $result{$_} = $caps->{$_} for keys %{$caps};
155 }
156  
157 # add range from %known
158 0 ▶ $result{range} = [$known{$lang}{limit_lo} // 0, $known{$lang}{limit_hi} // 999_999_999];
Condition0%0/4
$Lingua::Num2Word::known{$lang}{"limit_lo"} // 0
l!l
00
$Lingua::Num2Word::known{$lang}{"limit_hi"} // 999999999
l!l
00
MC/DC0%0/1
$Lingua::Num2Word::known{$lang}{"limit_lo"} // 0
$Lingua::Num2Word::known{$lang}{"limit_lo"}
MC/DC0%0/1
$Lingua::Num2Word::known{$lang}{"limit_hi"} // 999999999
$Lingua::Num2Word::known{$lang}{"limit_hi"}
Truth table0%0/2
A
A= $Lingua::Num2Word::known{$lang}{"limit_lo"}
Aresult
11
00
Truth table0%0/2
A
A= $Lingua::Num2Word::known{$lang}{"limit_hi"}
Aresult
11
00
159  
160 0 return \%result;
161 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 # }}}
164 # {{{ has_capability check if a language supports a feature
165  
166 sub has_capability :Export {
167 0 ▶ my $self = ref($_[0]) ? shift : undef;
Subroutine has_capability: not called CC=6 SCAR=37.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
168 0 ▶ my $lang = shift // return 0;
Condition0%0/2
shift() // (return 0)
l!l
00
MC/DC0%0/1
shift() // (return 0)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
169 0 ▶ my $feature = shift // return 0;
Condition0%0/2
shift() // (return 0)
l!l
00
MC/DC0%0/1
shift() // (return 0)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
170  
171 0 my $caps = capabilities($lang);
172 0 ▶ return 0 unless $caps;
Branch: 1 branch
BranchTrueFalseTotal
unless $caps 0 0 0
173 0 ▶ return $caps->{$feature} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$caps->{$feature} ? : 0 0 0
174 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 # }}}
177 # {{{ ordinal convert number to ordinal text
178  
179 sub ordinal :Export {
180 0 ▶ my $self = ref($_[0]) ? shift : Lingua::Num2Word->new();
Subroutine ordinal: not called CC=10 SCAR=47.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
181 0 my $result = '';
182 0 ▶ my $lang = shift // return $result;
Condition0%0/2
shift() // (return $result)
l!l
00
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
183 0 ▶ my $number = shift // return $result;
Condition0%0/2
shift() // (return $result)
l!l
00
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
184  
185 0 $lang = lc $lang;
186 0 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 0 0
187  
188 0 ▶ return $result if !exists $known{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Lingua::Num2Word::known{$lang} 0 0 0
189 0 ▶ return $result if !has_capability($lang, 'ordinal');
Branch: 1 branch
BranchTrueFalseTotal
unless has_capability($lang, "ordinal") 0 0 0
190  
191 0 my $pkg = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package};
192 # derive ordinal function name from cardinal (handles legacy names like num2sv_)
193 0 ▶ my $cardinal_func = $known{$lang}{function} // "num2${lang}_cardinal";
Condition0%0/2
$Lingua::Num2Word::known{$lang}{"function"} // "num2${lang}_cardinal"
l!l
00
MC/DC0%0/1
$Lingua::Num2Word::known{$lang}{"function"} // "num2${lang}_cardinal"
$Lingua::Num2Word::known{$lang}{"function"}
Truth table0%0/2
A
A= $Lingua::Num2Word::known{$lang}{"function"}
Aresult
11
00
194 0 my $func;
195 0 ▶ if ($cardinal_func =~ /_cardinal$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinal_func =~ /_cardinal$/u) { } 0 0 0
196 0 ($func = $cardinal_func) =~ s/_cardinal$/_ordinal/;
197 }
198 else {
199 0 $func = "num2${lang}_ordinal"; # fallback for OO/legacy modules
200 }
201  
202 0 eval "use $pkg (); \$result = ${pkg}::${func}(\$number);"; ## no critic
203 0 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
204  
205 0 return $result;
206 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
207  
208 # }}}
209 # {{{ new constructor
210  
211 sub new {
212 5 ▶ return bless {}, shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
213 }
214  
215 # }}}
216 # {{{ known_langs list of currently supported languages
217  
218 sub known_langs :Export {
219 0 ▶ return wantarray ? sort keys %known : [ sort keys %known ];
Subroutine known_langs: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
220 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 # }}}
223 # {{{ get_interval get minimal and maximal supported number
224  
225 # Return:
226 # undef for unsupported language
227 # list or list reference (depending to calling context) with
228 # minimal and maximal supported number
229 #
230 sub get_interval :Export {
231 2 ▶ my $self = ref($_[0]) ? shift : Lingua::Num2Word->new();
Subroutine get_interval: called CC=6 SCAR=21.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
232 2 ▶ my $lang = shift // return;
Condition100%2/2
shift() // (return)
l!l
11
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
233 1 $lang = lc $lang;
234 1 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 1 1
235  
236 1 ▶ return if (!defined $known{$lang});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Num2Word::known{$lang} 1 0 1
237  
238 0 my @limits = ($known{$lang}{limit_lo}, $known{$lang}{limit_hi});
239  
240 0 ▶ return wantarray ? @limits : \@limits;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
241 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 # }}}
244 # {{{ cardinal convert number to text
245  
246 sub cardinal :Export {
247 2 ▶ my $self = ref($_[0]) ? shift : Lingua::Num2Word->new();
Subroutine cardinal: called CC=9 SCAR=40.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
248 2 my $result = '';
249 2 ▶ my $lang = shift // return $result;
Condition50%1/2
shift() // (return $result)
l!l
02
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
250 0 ▶ my $number = shift // return $result;
Condition0%0/2
shift() // (return $result)
l!l
00
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
251  
252 0 $lang = lc $lang;
253 0 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 0 0
254  
255 0 ▶ return $result if (!defined $known{$lang});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Num2Word::known{$lang} 0 0 0
256  
257 0 ▶ if (defined $known{$lang}{lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Lingua::Num2Word::known{$lang}{'lang'}) { } 0 0 0
258 0 eval $self->preprocess_code($known{$lang}{lang}); ## no critic
259 0 ▶ carp $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
260 }
261 else {
262 0 eval $self->preprocess_code($lang); ## no critic
263 0 ▶ carp $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
264 }
265  
266 0 return $result;
267 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
268  
269 # }}}
270 # {{{ preprocess_code prepare code for evaluation
271  
272 sub preprocess_code :Export {
273 2 ▶ my $self = shift;
Subroutine preprocess_code: called CC=4 SCAR=21.2
Pod documented
274 2 ▶ my $lang = shift // return;
Condition100%2/2
shift() // (return)
l!l
11
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
275  
276 1 ▶ return if !exists $known{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Lingua::Num2Word::known{$lang} 1 0 1
277  
278 0 my $result = $known{$lang}{code};
279 0 my $pkg_name = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package};
280 0 ▶ my $pkg_name_with_version = $known{$lang}{version} ne ''
Branch: 1 branch
BranchTrueFalseTotal
$Lingua::Num2Word::known{$lang}{'version'} ne '' ? : 0 0 0
281 ? "$pkg_name $known{$lang}{version}"
282 : $pkg_name
283 ;
284  
285 0 my $function = $known{$lang}{function};
286  
287 0 $result =~ s/__PACKAGE_WITH_VERSION__/$pkg_name_with_version/g;
288 0 $result =~ s/__PACKAGE__/$pkg_name/g;
289 0 $result =~ s/__FUNCTION__/$function/g;
290  
291 0 return $result;
292 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
293 # }}}
294  
295 1 1;
296 __END__
↑ Summary