blib/lib/Lingua/Klingon/Recode.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 85.2% 29 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 93.3% 98 / 105 CC 20 SCAR 28.5
CC
20
Cov
93%
recode
20.5
BEGIN
1.0
BEGIN
1.0
CRAP 17.3
↑ Summary
1 package Lingua::Klingon::Recode;
2 # vim:set tw=72 sw=2 cin cink-=0#:
3
4 3 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 require Exporter;
9 3 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 @ISA = qw(Exporter);
11
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15
16 # This allows declaration use Lingua::Klingon::Recode ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19 3 %EXPORT_TAGS = ( 'all' => [ qw(
20 recode
21 ) ] );
22
23 3 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24
25 3 @EXPORT = qw(
26
27 );
28 3 $VERSION = '1.02';
29
30 # convert from encoding FOO to uhmal gnj
31 3 my %from = (
32 'tlhIngan' => {
33 'a' => 'a',
34 'b' => 'b',
35 'ch' => 'c',
36 'D' => 'd',
37 'e' => 'e',
38 'gh' => 'f',
39 'H' => 'g',
40 'I' => 'h',
41 'j' => 'i',
42 'l' => 'j',
43 'm' => 'k',
44 'n' => 'l',
45 'ng' => 'm',
46 'o' => 'n',
47 'p' => 'o',
48 'q' => 'p',
49 'Q' => 'q',
50 'r' => 'r',
51 'S' => 's',
52 't' => 't',
53 'tlh' => 'u',
54 'u' => 'v',
55 'v' => 'w',
56 'w' => 'x',
57 'y' => 'y',
58 "'" => 'z',
59 },
60 'tlhingan' => {
61 'a' => 'a',
62 'b' => 'b',
63 'ch' => 'c',
64 'd' => 'd',
65 'e' => 'e',
66 'gh' => 'f',
67 'h' => 'g',
68 'i' => 'h',
69 'j' => 'i',
70 'l' => 'j',
71 'm' => 'k',
72 'n' => 'l',
73 'ng' => 'm',
74 'o' => 'n',
75 'p' => 'o',
76 'q' => 'p',
77 # 'q' => 'q', # this gets lost :(
78 'r' => 'r',
79 's' => 's',
80 't' => 't',
81 'tlh' => 'u',
82 'u' => 'v',
83 'v' => 'w',
84 'w' => 'x',
85 'y' => 'y',
86 "'" => 'z',
87 },
88 'XIFAN' => {
89 'A' => 'a',
90 'B' => 'b',
91 'C' => 'c',
92 'D' => 'd',
93 'E' => 'e',
94 'G' => 'f',
95 'H' => 'g',
96 'I' => 'h',
97 'J' => 'i',
98 'L' => 'j',
99 'M' => 'k',
100 'N' => 'l',
101 'F' => 'm',
102 'O' => 'n',
103 'P' => 'o',
104 'K' => 'p',
105 'Q' => 'q',
106 'R' => 'r',
107 'S' => 's',
108 'T' => 't',
109 'X' => 'u',
110 'U' => 'v',
111 'V' => 'w',
112 'W' => 'x',
113 'Y' => 'y',
114 'Z' => 'z',
115 "'" => 'z',
116 },
117 );
118
119 # the same mapping serves both
120 3 $from{'XIFANZ'} = $from{'XIFAN'};
121
122 # convert from uhmal gnj to encoding FOO
123 3 my %to;
124 3 $to{'tlhIngan'} = { reverse %{$from{'tlhIngan'}} };
125 3 $to{'tlhingan'} = { reverse %{$from{'tlhingan'}} };
126 3 $to{'tlhingan'}{'q'} = 'q'; # map both 'Q' and 'q' to 'q'
127
128 3 $to{'XIFAN'} = { reverse %{$from{'XIFAN'}} };
129 3 $to{'XIFAN'}{'z'} = "'";
130
131 3 $to{'XIFANZ'} = { reverse %{$from{'XIFAN'}} };
132 3 $to{'XIFANZ'}{'z'} = 'Z';
133
134 # map names to internal form
135 3 my %internal = (
136 'tlhIngan' => 'tlhIngan',
137 'tlhIngan Hol' => 'tlhIngan',
138 "tlhIngan Hol Dajalh'a'" => 'tlhIngan',
139
140 'tlhingan' => 'tlhingan',
141 'tlhingan hol' => 'tlhingan',
142 "tlhingan hol dajatlh'a'" => 'tlhingan',
143
144 'TLHINGAN' => 'TLHINGAN',
145 'TLHINGAN HOL' => 'TLHINGAN',
146 "TLHINGAN HOL DAJATLH'A'" => 'TLHINGAN',
147
148 'xifan' => 'xifan',
149 'xifan hol' => 'xifan',
150 "xifan hol dajax'a'" => 'xifan',
151
152 'xifan hol dajaxzaz' => 'xifanz',
153 'xifanz' => 'xifanz',
154
155 'XIFAN' => 'XIFAN',
156 'XIFAN HOL' => 'XIFAN',
157 "XIFAN HOL DAJAX'A'" => 'XIFAN',
158
159 'XIFAN HOL DAJAXZAZ' => 'XIFANZ',
160 'XIFANZ' => 'XIFANZ',
161
162 'uhmal' => 'uhmal',
163 'uhmal gnj' => 'uhmal',
164 'uhmal gnj daiauzaz' => 'uhmal',
165
166 'UHMAL' => 'UHMAL',
167 'UHMAL GNJ' => 'UHMAL',
168 'UHMAL GNJ DAIAUZAZ' => 'UHMAL',
169 );
170
171 # regular expression that defines a letter
172 3 my %letter = (
173 'uhmal' => qr/[a-z]/,
174 # "ng(?!h)" since the sequence "ngh" must be n+gh
175 'tlhIngan' => qr/tlh|[cg]h|ng(?!h)|[abDeHIjlmnopqQrStuvwy']/,
176 # Can't do that here, unfortunately, since "ngh" could be either
177 # n+gh or ng+H = ng+h. The disadvantage of case-smashing.
178 'tlhingan' => qr/tlh|[cg]h|ng|[abdehijlmnopqrstuvwy']/,
179 'XIFAN' => qr/[A-Y']/,
180 'XIFANZ' => qr/[A-Z]/,
181 );
182
183 # based on
184 3 my %base = (
185 'uhmal' => 'uhmal',
186 'UHMAL' => 'uhmal',
187 'tlhIngan' => 'tlhIngan',
188 'tlhingan' => 'tlhingan',
189 'TLHINGAN' => 'tlhingan',
190 'xifan' => 'XIFAN',
191 'xifanz' => 'XIFANZ',
192 'XIFAN' => 'XIFAN',
193 'XIFANZ' => 'XIFANZ',
194 );
195
196
197 # The subroutine itself
198
199 532 ▶sub recode {
Subroutine recode: called CC=20 SCAR=30.2
Pod documented
200 532 my $from = shift;
201 532 my $to = shift;
202 532 my @input;
203 my @result;
204
205 532 ▶ if (not exists $internal{$from}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $internal{$from}) { } 0 532 532
elsif (not exists $internal{$to}) { } 0 532 532
206 0 croak "Can't recognise 'from' encoding '$from'";
207 } elsif (not exists $internal{$to}) {
208 0 croak "Can't recognise 'to' encoding '$to'";
209 } else {
210 532 $from = $internal{$from};
211 532 $to = $internal{$to};
212
213 532 ▶ if($to eq $from) {
Branch: 3 branches
BranchTrueFalseTotal
if ($to eq $from) { } 59 473 532
elsif ($to eq lc $from) { } 33 440 473
elsif ($to eq uc $from) { } 33 407 440
214 59 ▶ return +(wantarray ? @_ : $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 59 59
215 } elsif($to eq lc $from) {
216 33 ▶ return +(wantarray ? map lc, @_ : lc $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 33 33
217 } elsif($to eq uc $from) {
218 33 ▶ return +(wantarray ? map uc, @_ : uc $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 33 33
219 } else {
220 # transform from initial encoding to uhmal
221 407 ▶ if($from eq 'UHMAL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($from eq 'UHMAL') { } 49 358 407
elsif ($from eq 'uhmal') { } 49 309 358
222 49 @input = map lc, @_;
223 } elsif($from eq 'uhmal') {
224 49 @input = @_;
225 } else {
226 309 @input = @_;
227 309 for(@input) {
228 311 ▶ $_ = lc $_ if $base{$from} eq lc $from;
Branch: 1 branch
BranchTrueFalseTotal
if $base{$from} eq lc $from 70 241 311
229 311 ▶ $_ = uc $_ if $base{$from} eq uc $from;
Branch: 1 branch
BranchTrueFalseTotal
if $base{$from} eq uc $from 196 115 311
230 # Can't use /o since $base{$from} will change
231 311 s/($letter{$base{$from}})/$from{$base{$from}}{$1}/g;
232 }
233 }
234
235 # transform from uhmal to final encoding
236 407 ▶ if($to eq 'UHMAL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($to eq 'UHMAL') { } 45 362 407
elsif ($to eq 'uhmal') { } 45 317 362
237 45 @result = map uc, @input;
238 } elsif($to eq 'uhmal') {
239 45 @result = @input;
240 } else {
241 317 @result = @input;
242 317 for(@result) {
243 319 s/($letter{'uhmal'})/$to{$base{$to}}{$1}/go;
244 319 ▶ $_ = lc $_ if $to eq lc $base{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $to eq lc $base{$to} 132 187 319
245 319 ▶ $_ = uc $_ if $to eq uc $base{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $to eq uc $base{$to} 135 184 319
246 }
247 }
248 }
249 }
250
251 407 ▶ return +(wantarray ? @result : $result[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 406 407
252 }
253
254
255 3 1;
256 __END__
↑ Summary