#!/usr/bin/env perl # ch-2.pl - Read binary morse code # # Ryan Thompson use 5.016; # fc() use warnings; use autodie; use strict; use utf8; no warnings 'uninitialized'; binmode STDOUT, ':utf8'; # Include the morse code table generated by ../extras/fetch-morse.pl use lib qw< ../extras >; eval { require 'morse_code.pl' }; die "Run fetch-morse.pl in ../extras first" if $@; my %rmorse = reverse_morse( morse_hash() ); chomp, say morse_bin_to_utf8($_) while $_ = <<>>; # Take morse binary and turn it (back) into utf8 text sub morse_bin_to_utf8 { my $r = ''; for (split /0{7}/, shift) { $r .= join '', map { $rmorse{$_} } split /000/; $r .= ' '; } $r; } # Reverse the morse hash. We could just use reverse(), but that would give # inconsistent results for duplicate values. Plus we may as well convert # the . and - to 1 and 0 at this stage. sub reverse_morse { my %morse = @_; map { my $morse = $morse{$_}; $morse =~ s/\./10/g; $morse =~ s/\-/1110/g; $morse =~ s/0$//; # Remove trailing intra-char gap $morse => fc $_ } keys %morse; }