The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
# Perl version of morse.
# Matthew Bafford
# 2/28/1999

use strict;

my %translation = (
    '0'  => '-----',  '1'  => '.----',  '2'  => '..---',  '3'  => '...--',
    '4'  => '....-',  '5'  => '.....',  '6'  => '-....',  '7'  => '--...',
	'8'  => '---..',  '9'  => '----.',  'a'  => '.-',     'b'  => '-...',
	'c'  => '-.-.',   'd'  => '-..',    'e'  => '.',      'f'  => '..-.',
	'g'  => '--.',    'h'  => '....',   'i'  => '..',     'j'  => '.---',
	'k'  => '-.-',    'l'  => '.-..',   'm'  => '--',     'n'  => '-.',
	'o'  => '---',    'p'  => '.--.',   'q'  => '--.-',   'r'  => '.-.',
	's'  => '...',    't'  => '-',      'u'  => '..-',    'v'  => '...-',
	'w'  => '.--',    'x'  => '-..-',   'y'  => '-.--',   'z'  => '--..',

	# These aren't in the BSD source code...
	'.'  => '.-.-.-', ','  => '--..--', ':'  => '---...', '?'  => '..--..',
	"'"  => '.----.', '-'  => '-....-', '/'  => '-..-.',  '('  => '-.--.-',
	')'  => '-.--.-', '"'  => '.-..-.', ' '  => '-...-'
);

$|++;

my $opt_s = undef;

for ( @ARGV ) {
	if ( $_ eq '-s' ) {
		$opt_s = 1;
		next;
	}

	die "Unknown option: $_\n";
}

while ( <STDIN> ) {
	chomp;

	# Add a space on the end so it'll print BT at the end.
	for my $letter ( split('', $_), ' ' ) {
		next unless exists $translation{lc $letter};
	
		print ' ';
		
		if ( $opt_s ) {
			print $translation{lc $letter};
		} else {
			print join ' ', map   { $_ eq '.' ? 'dit' : 'daw' }
	              split //, $translation{lc $letter};
		}

		print ",\n";
	}
}	

__END__

=pod

=head1 NAME

morse - Translate standard input into morse code

=head1 SYNOPSIS

morse [B<-s>]

=head1 DESCRIPTION

I<morse> takes standard input and translates it into morse code.
Unkown characters are ignored.

=head2 OPTIONS

I<morse> only takes one option:

=over 4

=item B<-s>

Print the morse code as '.'s and '-'s, rather than 'dit's and 'daw's.

=back

=head1 ENVIRONMENT

I<morse> is not affected by any environment variables.

=head1 COMPATIBILITY

This version offers a slightly larger set of characters than the BSD version.

=head1 BUGS

I<morse> has no known bugs.

=head1 AUTHOR

This Perl version of I<morse> was written by
Matthew Bafford, I<dragons@scescape.net>.

=head1 COPYRIGHT and LICENSE

This program is copyright (c) Matthew Bafford 1999.

This program is free and open software.  You may use, modify, distribute,
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others from doing the same.