#!/usr/bin/perl

# IO::Radioize - translate a string into the NATO phonetic alphabet
# Copyright 2005 Toby Inkster.

package Radioize;

BEGIN {

	use strict;
	use warnings;
	
	my ($VERSION, @EXPORT);

	$VERSION	= 1.00;
	@EXPORT		= qw(&new &translate_string);

	our (%letters, %numbers, %punctuation);

	%letters = (
		'a'        => 'alpha',
		'b'        => 'bravo',
		'c'        => 'charlie',
		'd'        => 'delta',
		'e'        => 'echo',
		'f'        => 'foxtrot',
		'g'        => 'golf',
		'h'        => 'hotel',
		'i'        => 'india',
		'j'        => 'juliet',
		'k'        => 'kilo',
		'l'        => 'lima',
		'm'        => 'mike',
		'n'        => 'november',
		'o'        => 'oscar',
		'p'        => 'papa',
		'q'        => 'quebec',
		'r'        => 'romeo',
		's'        => 'sierra',
		't'        => 'tango',
		'u'        => 'uniform',
		'v'        => 'victor',
		'w'        => 'whisky',
		'x'        => 'x-ray',
		'y'        => 'yankee',
		'z'        => 'zulu'
	);

	%numbers = (
		'0'        => 'naught',
		'1'        => 'one',
		'2'        => 'two',
		'3'        => 'three',
		'4'        => 'four',
		'5'        => 'five',
		'6'        => 'six',
		'7'        => 'seven',
		'8'        => 'eight',
		'9'        => 'niner'
	);

	%punctuation = (
		'.'        => 'stop',
		','        => 'comma',
		'?'        => 'question',
		'!'        => 'bang',
		'#'        => 'hash',
		':'        => 'colon',
		';'        => 'semicolon',
		'@'        => 'at',
		'&'        => 'ampersand',
		'%'        => 'percent',
		'$'        => 'dollar',
		'£'        => 'pound',
		'^'        => 'caret',
		'*'        => 'asterisk',
		'('        => 'open parenthesis',
		')'        => 'close parenthesis',
		'['        => 'open bracket',
		']'        => 'close bracket',
		'{'        => 'open brace',
		'}'        => 'close brace',
		'~'        => 'tilde',
		'|'        => 'pipe',
		"'"        => 'single quote',
		'"'        => 'double quote',
		'`'        => 'backtick',
		'-'        => 'dash',
		'+'        => 'plus',
		'='        => 'equals',
		'_'        => 'underscore',
		'/'        => 'slash',
		"\\"       => 'backslash',
		'<'        => 'less than',
		'>'        => 'more than',
		"\n"       => 'new line',
		"\t"       => 'tab',
		' '        => 'space'
	);


	sub new {
		my $self = {};
		bless $self;
		return $self;
	}

	sub translate_string {
		my $self = shift;
		my $in = shift;
		my ($char, $expand, $out);		
		while ($in ne '') {
			$char = substr($in,0,1);
			$in = substr($in,1);
			$expand = '';
			if ($char =~ m/[A-Z]/) {
				$expand = 'capital ';
				$char =~ tr/[A-Z]/[a-z]/;
			}
			if ($char =~ /[a-z]/) {
				$expand .= $letters{$char};
			} elsif ($char =~ /[0-9]/) {
				$expand  = $numbers{$char};
			} else {
				$expand  = $punctuation{$char};
			}
			if ($in ne '') {
				$expand .= ', ';
			} else {
				$expand .= '.';
			}
			$out .= $expand;
		} # while ($in ne '')
		$out =~ tr/[a-z]/[A-Z]/;
		return $out;
	} # sub translate_string

} #BEGIN

1;

__END__

=head1 NAME

IO::Radioize - translate a string into the NATO phonetic alphabet

=head1 SYNOPSIS

use IO::Radioize;
$r = Radioize->new;
$t = $r->translate_string("Hello");
print "$t\n";

=head1 DESCRIPTION

The C<IO::Radioize> module provides a way to translate from plain strings into
the NATO phonetic alphabet. That is A is "alpha", B is "bravo", C is "charlie",
etc. This provides a method of speaking alphanumeric strings over a radio with
poor sound quality without being mistaken.

In terms of practical applications to computers, it can be useful when printing
random alphanumeric sequences, such as passwords, where a lowercase l, a
number 1 and an capital letter I can be easily confused.

=head1 BUGS

No known bugs, but the module needs reverse translation and more characters
to be properly complete.

=head1 COPYRIGHT

Copyright 2005 Toby Inkster.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

=cut
