blib/lib/Acme/MorseComments.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 68.7% 11 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 2 / 2 Podpod 0.0% 0 / 1 totaltotal 84.4% 49 / 58 CC 4 SCAR 12.8
CC
4
Cov
87%
write_file
4.2
BEGIN
1.0
CRAP 3.6
↑ Summary
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
BranchTrueFalseTotal
unless -r $0 0 1 1
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
BranchTrueFalseTotal
if (my $token = $tokenizer->get_token) 46 1 47
38 46 ▶ if ($token->isa('PPI::Token::Comment') && $token->content !~ /^#!/
Branch: 1 branch
BranchTrueFalseTotal
if ($token->isa('PPI::Token::Comment') and not $token->content =~ /^#!/ and $token->content =~ /[A-Za-z0-9]/) { } 6 40 46
Condition83%5/6
$token->isa("PPI::Token::Comment") and not $token->content =~ /^#!/ and $token->content =~ /[A-Za-z0-9]/
!ll&&!rl&&r
3826
$token->isa("PPI::Token::Comment") and not $token->content =~ /^#!/
!ll&&!rl&&r
3808
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]/
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
exists $morse_code{lc $char} ? : 315 81 396
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
BranchTrueFalseTotal
if (write_file("$0.orig", $content)) { } 1 0 1
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
BranchTrueFalseTotal
if -e $filename 0 1 1
65  
66 1 ▶ open my $fh, '>', $filename or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or return 0 1 0 1
67 1 print $fh $content;
68 1 ▶ close $fh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return 0 1 0 1
69  
70 1 return 1;
71 }
72  
73 1 1;
74  
75 __END__
↑ Summary