lib/Convert/Morse.pm

filter: Statementstmt 100% 70 / 70 Branchbran 83.3% 15 / 18 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 90.9% 111 / 122 CC 17 SCAR 11.7
CC
17
Cov
93%
tokens
5.0
as_ascii
4.0
_convert
4.0
CRAP 3.2
↑ Summary
1 #############################################################################
2 # Convert/Morse.pm -- package to convert between ASCII and MORSE code.
3 #
4 #############################################################################
5  
6 # TODO:
7  
8 # German umlaute etc (how to represent in ASCII?).
9 # see: http://member.nifty.ne.jp/je1trv/CW_J_e.htm
10 # see: http://burks.brighton.ac.uk/burks/foldoc/61/75.htm
11  
12 package Convert::Morse;
13 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 $VERSION = 0.06; # Current version of this package
15 2 ▶use 5.008001; # requires this Perl version or later
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 @ISA = qw(Exporter);
19 2 @EXPORT_OK = qw( as_morse as_ascii is_morse is_morsable);
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #############################################################################
23 # global variables
24  
25 2 my $morse_ascii; # hash of morse symbols (morse => ascii)
26 my $ascii_morse; # hash of ascii symbols (ascii => morse)
27 2 my $regexp_ascii_morse; # compiled regexp
28 2 my $regexp_morse_ascii; # compiled regexp
29 2 my $error; # last error message
30  
31 sub as_morse
32 19 ▶ {
Subroutine as_morse: called CC=3 SCAR=11.0
Pod documented
33 # convert ASCII text into morse code
34 19 my $ascii = shift; # no || "" because fail for '0'
35 19 ▶ return "" if !defined $ascii || $ascii eq "";
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ascii or $ascii eq "" 1 18 19
Condition66%2/3
not defined $ascii or $ascii eq ""
l!l&&r!l&&!r
0118
MC/DC50%1/2
not defined $ascii or $ascii eq ""
not defined $ascii $ascii eq ""
Truth table66%2/3
not(A) or B
A= not defined $asciiB= $ascii eq ""
ABresult
1X1
011
000
36 18 undef $error;
37 18 $ascii = uc($ascii); # 'Helo' => 'HELO'
38 18 $ascii =~ s/\G$regexp_ascii_morse/_convert($1,$ascii_morse);/ge;
39 18 $ascii =~ s/\s\z//; # remove last space
40 18 $ascii;
41 }
42  
43 sub as_ascii
44 20 ▶ {
Subroutine as_ascii: called CC=4 SCAR=13.9
Pod documented
45 # convert morse text into ascii code
46 20 my $morse = shift;
47 20 ▶ return "" if !defined $morse || $morse eq "";
Branch: 1 branch
BranchTrueFalseTotal
if not defined $morse or $morse eq "" 1 19 20
Condition66%2/3
not defined $morse or $morse eq ""
l!l&&r!l&&!r
0119
MC/DC50%1/2
not defined $morse or $morse eq ""
not defined $morse $morse eq ""
Truth table66%2/3
not(A) or B
A= not defined $morseB= $morse eq ""
ABresult
1X1
011
000
48 # because regexps expects a space (to avoid testing for \s|$)
49 19 ▶ $morse .= ' ' if substr($morse,-1,1) ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
if substr($morse, -1, 1) ne " " 19 0 19
50 19 undef $error;
51 19 $morse =~ s/\G$regexp_morse_ascii/_convert($1,$morse_ascii);/ge;
52 19 $morse =~ s/ +/ /; # collapse multiple spaces
53 19 $morse =~ s/\s\z//; # remove last space
54 19 $morse;
55 }
56  
57 sub _convert
58 88 ▶ {
Subroutine _convert: called CC=4 SCAR=13.9
59 88 my ($token,$hash) = @_;
60 88 ▶ return '' if !defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 88 88
61 88 ▶ $token =~ s/\s$// if length($token) > 1; # remove trailing space if not ' '
Branch: 1 branch
BranchTrueFalseTotal
if length $token > 1 35 53 88
62 88 my $sym = $hash->{$token};
63 88 ▶ if (!defined $sym)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sym) 9 79 88
64 {
65 9 $error = "Undefined token '$token'"; return $token;
66 }
67 #print "'",quotemeta($token),"' => '",quotemeta($sym),"'\n";
68 79 $sym;
69 }
70  
71 sub is_morsable
72 2 ▶ {
Subroutine is_morsable: called CC=3 SCAR=11.1
Pod documented
73 # returns true wether input can be completely expressed as morse
74 2 ▶ my $text = shift || "";
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
75 2 my $morse = as_morse($text);
76 2 ▶ error() ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
error() ? : 1 1 2
77 }
78  
79 sub is_morse
80 2 ▶ {
Subroutine is_morse: called CC=3 SCAR=11.1
Pod documented
81 # returns true wether input is valid Morse code
82 2 ▶ my $text = shift || "";
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83 2 my $ascii = as_ascii($text);
84 2 ▶ error() ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
error() ? : 1 1 2
85 }
86  
87 sub error
88 4 ▶ {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
89 # return last parse error or undef for ok
90 4 $error;
91 }
92  
93 #############################################################################
94 # self initalization
95  
96 sub tokens
97 2 ▶ {
Subroutine tokens: called CC=5 SCAR=16.1
Pod documented
98 # set/return hash of valid/invalid tokens (in form of ascii => morse)
99 2 my $tokens = shift;
100 2 ▶ if (defined $tokens)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tokens) 2 0 2
101 {
102 2 $morse_ascii = {}; $ascii_morse = {};
103 2 foreach (keys %$tokens)
104 {
105 114 $ascii_morse->{$_} = $tokens->{$_}.' ';
106 114 $morse_ascii->{$tokens->{$_}} = $_;
107 }
108 # fix space handling
109 2 foreach (" ")
110 {
111 2 $ascii_morse->{$_} = $_;
112 2 $morse_ascii->{$_} = $_;
113 }
114 # preserve spaces
115 # compile a big regexp for token parsing
116 $regexp_ascii_morse = '(' .
117 2 join('|', map { quotemeta } keys %$ascii_morse)
118 . '|.|[\n\r\t])';
119 $regexp_morse_ascii = '(' .
120 2 join('\s|', map { quotemeta } keys %$morse_ascii)
121 . '\s|.|[\n\r\t])';
122 #print STDERR "$regexp_ascii_morse\n";
123 #print STDERR "$regexp_morse_ascii\n";
124 #foreach (keys %$ascii_morse)
125 # {
126 # print "'$_' => '$ascii_morse->{$_}'\n";
127 # }
128 }
129 # return current token set
130 2 my $copy = {};
131 2 foreach (keys %$ascii_morse) { $copy->{$_} = $ascii_morse->{$_}; }
132 2 $copy;
133 }
134  
135 BEGIN
136 2 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
137 2 tokens( {
138 '.' => '.-.-.-',
139 ',' => '--..--',
140 ':' => '---...',
141 '?' => '..--..',
142 "'" => '.----.',
143 '-' => '-....-',
144 ';' => '-.-.-',
145 '/' => '-..-.',
146 '(' => '-.--.',
147 ')' => '-.--.-',
148 '"' => '.-..-.',
149 '_' => '..--.-',
150 '=' => '-...-',
151 '+' => '.-.-.',
152 '!' => '-.-.--',
153 '@' => '.--.-.',
154 qw(
155 A .-
156 B -...
157 C -.-.
158 D -..
159 E .
160 F ..-.
161 G --.
162 H ....
163 I ..
164 J .---
165 K -.-
166 L .-..
167 M --
168 N -.
169 O ---
170 P .--.
171 Q --.-
172 R .-.
173 S ...
174 T -
175 U ..-
176 V ...-
177 W .--
178 X -..-
179 Y -.--
180 Z --..
181 0 -----
182 1 .----
183 2 ..---
184 3 ...--
185 4 ....-
186 5 .....
187 6 -....
188 7 --...
189 8 ---..
190 9 ----.),
191 # russian
192 qw(
193 EH ..-..
194 YU ..--
195 YA .-.-
196 CHEH ---.
197 SHA ----
198 ),
199 # japanese (WABUN) (not done, needs support for
200 # escaping sequences DO & SN
201 qw(
202 )
203 } );
204 # Ä .-.-
205 # Ö ---.
206 # Ü ..--
207 # adash .--.-
208 # angstroem .--.- (same as adash? huh?)
209 # ch ----
210 # Edash ..-..
211 # N ntilde --.--
212 }
213
214 #############################################################################
215 2 1;
216  
217 __END__
↑ Summary