| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # .- -.-. -- . ---... ---... -- --- .-. ... . -.-. --- -- -- . -. - ... .-.-.- ..-. ..- -. .-.. .. - - .-.. . -- --- -.. ..- .-.. . - .... .- - .-- .. .-.. .-.. ..-. .. -. -.. .- .-.. .-.. - .... . -.-. --- -- -- . -. - ... .. -. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # -.-- --- ..- .-. ... --- ..-. - .-- .- .-. . .- -. -.. .-. . .--. .-.. .- -.-. . - .... . -- .-- .. - .... -- --- .-. ... . -.-. --- -.. . .-.-.- | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | package Acme::MorseComments; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | $VERSION = "1.00"; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use PPI::Tokenizer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | my %morse_code = ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | a => '.-', b => '-...', c => '-.-.', d => '-..', | ||||||||||||||||||||||||||||||||||||||||||
| 14 | e => '.', f => '..-.', g => '--.', h => '....', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | i => '..', j => '.---', k => '-.-', l => '.-..', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | m => '--', n => '-.', o => '---', p => '.--.', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | q => '--.-', r => '.-.', s => '...', t => '-', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | u => '..-', v => '...-', w => '.--', x => '-..-', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | y => '-.--', z => '--..', 0 => '-----', 1 => '.----', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 => '..---', 3 => '...--', 4 => '....-', 5 => '.....', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 => '-....', 7 => '--...', 8 => '---..', 9 => '----.', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | '.' => '.-.-.-', ',' => '--..--', '?' => '..--..', '@' => '.--.-.', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | q{'} => '.----.', '!' => '-.-.--', '/' => '-..-.', '(' => '-.--.-', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ')' => '-.--.-', '&' => '. ...', ':' => '---...', ';' => '-.-.-.', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | '=' => '-...-', '/' => '-..-.', '-' => '-....-', '_' => '..-- .-', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | '$' => '...-..-', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | die "Not parseable!\n" unless -r $0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | my $content = do { open my $fh, '<', $0; local $/; <$fh>; }; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | my $tokenizer = PPI::Tokenizer->new( \$content ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $new_file = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | TOKEN: | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | while (my $token = $tokenizer->get_token) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 46 | ▶ | if ($token->isa('PPI::Token::Comment') && $token->content !~ /^#!/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$token->isa("PPI::Token::Comment") and not $token->content =~ /^#!/ and $token->content =~ /[A-Za-z0-9]/
$token->isa("PPI::Token::Comment") and not $token->content =~ /^#!/
MC/DC66%2/3
$token->isa("PPI::Token::Comment") and not $token->content =~ /^#!/ and $token->content =~ /[A-Za-z0-9]/
$token->isa("PPI::Token::Comment")
not $token->content =~ /^#!/
$token->content =~ /[A-Za-z0-9]/
Truth table75%3/4
A and not(B) and C
A= $token->isa("PPI::Token::Comment")B= not $token->content =~ /^#!/C= $token->content =~ /[A-Za-z0-9]/
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | && $token->content =~ /[A-Za-z0-9]/) { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | foreach my $char (split q{}, $token->content) { | |||||||||||||||||||||||||||||||||||||||||
| 41 | $new_file .= exists $morse_code{lc $char} | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 396 | ▶ | ? $morse_code{lc $char} . ' ' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | : $char; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 40 | $new_file .= $token->content; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | $new_file =~ s/^ \s* (?:use|require) \s+ Acme::MorseComments \s* ;//xms; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ( write_file("$0.orig", $content) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | open 0, ">$0"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | print {0} $new_file; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | print $new_file; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub write_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my ($filename, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return 0 if -e $filename; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | open my $fh, '>', $filename or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | print $fh $content; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | close $fh or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | __END__ | ||||||||||||||||||||||||||||||||||||||||||