| 1 | package Lingua::TLH::Numbers; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Regexp::Common qw( number ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT_OK = qw( num2tlh num2tlh_ordinal ); | |||||||||||||||||||||||||||
| 11 | 3 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # up to 9 million supported | ||||||||||||||||||||||||||||
| 16 | 3 | Readonly my $MAX_INT_DIGITS => 7; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | Readonly my $EMPTY_STR => q{}; | |||||||||||||||||||||||||||
| 19 | 3 | Readonly my $SPACE => q{ }; | |||||||||||||||||||||||||||
| 20 | 3 | Readonly my $MINUS => q{-}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | Readonly my $POINT => q{vI'}; | |||||||||||||||||||||||||||
| 23 | 3 | Readonly my $ORDINAL_SUFFIX => q{DIch}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | Readonly my @NAMES1 => qw< pagh wa' cha' wej loS vagh jav Soch chorgh Hut >; | |||||||||||||||||||||||||||
| 26 | 3 | Readonly my @NAMES2 => $EMPTY_STR, qw< maH vatlh SaD netlh bIp 'uy' >; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # convert number to words | ||||||||||||||||||||||||||||
| 29 | 84 | ▶ | sub num2tlh { | ||||||||||||||||||||||||||
|
Subroutine
num2tlh: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 84 | my ($number) = @_; | |||||||||||||||||||||||||||
| 31 | 84 | my $digit_count = 0; | |||||||||||||||||||||||||||
| 32 | 84 | my @names; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 84 | ▶ | return unless defined $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # NaN and inf not supported | ||||||||||||||||||||||||||||
| 37 | 83 | ▶ | return if $number eq 'NaN' || $number =~ m{^ [-+]? inf $}ix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$number eq "NaN" or $number =~ /^ [-+]? inf $/xi
MC/DC100%2/2
$number eq "NaN" or $number =~ /^ [-+]? inf $/xi
$number eq "NaN"
$number =~ /^ [-+]? inf $/xi
Truth table100%3/3
A or B
A= $number eq "NaN"B= $number =~ /^ [-+]? inf $/xi
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 79 | ▶ | return if $number !~ m{^ $RE{num}{real}{-keep} $}x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 73 | my ($sign, $int, $frac) = ($2, $4, $6); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # negatives not supported | ||||||||||||||||||||||||||||
| 43 | 73 | ▶ | return if $sign eq $MINUS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 66 | ▶ | return if length $int > $MAX_INT_DIGITS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # integer | ||||||||||||||||||||||||||||
| 48 | DIGIT: | ||||||||||||||||||||||||||||
| 49 | 66 | for my $digit (reverse split $EMPTY_STR, $int) { | |||||||||||||||||||||||||||
| 50 | # skip zero unless it is the only digit | ||||||||||||||||||||||||||||
| 51 | 198 | ▶ | next DIGIT if $digit == 0 && $int != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$digit == 0 and $int != 0
MC/DC100%2/2
$digit == 0 and $int != 0
$digit == 0
$int != 0
Truth table100%3/3
A and B
A= $digit == 0B= $int != 0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 92 | unshift @names, $NAMES1[$digit] . $NAMES2[$digit_count]; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | continue { | ||||||||||||||||||||||||||||
| 56 | 198 | $digit_count++; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # fraction | ||||||||||||||||||||||||||||
| 60 | 66 | ▶ | if (defined $frac && $frac ne $EMPTY_STR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $frac and $frac ne $EMPTY_STR
MC/DC100%2/2
defined $frac and $frac ne $EMPTY_STR
defined $frac
$frac ne $EMPTY_STR
Truth table100%3/3
A and B
A= defined $fracB= $frac ne $EMPTY_STR
| |||||||||||||||||||||||||||||
| 61 | 12 | push @names, $POINT, map { $NAMES1[$_] } split $EMPTY_STR, $frac; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 66 | return join $SPACE, @names; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # convert number to ordinal words | ||||||||||||||||||||||||||||
| 68 | 23 | ▶ | sub num2tlh_ordinal { | ||||||||||||||||||||||||||
|
Subroutine
num2tlh_ordinal: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 23 | my ($number) = @_; | |||||||||||||||||||||||||||
| 70 | 23 | my $name = num2tlh($number); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 23 | ▶ | return unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 23 | return $name . $ORDINAL_SUFFIX; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | 1; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||