blib/lib/Data/HexDump/XXD.pm

filter: Statementstmt 100% 45 / 45 Branchbran 70.0% 7 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 95.7% 67 / 70 CC 9 SCAR 10.6
CC
9
Cov
95%
xxd_r
4.1
_xxd_line
4.0
xxd
3.0
CRAP 2.9
↑ Summary
1 package Data::HexDump::XXD;
2  
3 3 ▶use version; our $VERSION = qv('0.1.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our @EXPORT_OK = qw( xxd_pack xxd_r xxd_unpack xxd );
12  
13 # Other recommended modules (uncomment to use):
14 # use IO::Prompt;
15 # use Perl6::Export;
16 # use Perl6::Slurp;
17 # use Perl6::Say;
18 # use Regexp::Autoflags;
19 # use Readonly;
20  
21 # Module implementation here
22 32 ▶sub _xxd_line { # format a hex dump a-la xxd
Subroutine _xxd_line: called CC=4 SCAR=13.9
23 32 my ($counter, @octets) = @_;
24 32 $counter = sprintf '%07x:', $counter;
25 32 my ($hex, $dump);
26 32 my @sep = ('', ' ');
27 32 for my $i (0 .. $#octets) {
28 507 $hex .= unpack('H*', $octets[$i]) . $sep[$i % 2];
29 507 my $code = ord $octets[$i];
30 507 ▶ $dump .= ($code >= 0x20 && $code < 0x7F) ? $octets[$i] : '.';
Branch: 1 branch
BranchTrueFalseTotal
$code >= 32 && $code < 127 ? : 190 317 507
Condition100%3/3
$code >= 32 and $code < 127
!ll&&!rl&&r
64253190
MC/DC100%2/2
$code >= 32 and $code < 127
$code >= 32 $code < 127
Truth table100%3/3
A and B
A= $code >= 32B= $code < 127
ABresult
0X0
100
111
31 }
32  
33 32 $hex .= ' ' x (40 - length $hex);
34 32 return join ' ', $counter, $hex, $dump;
35 } ## end sub _xxd_line
36  
37 2 ▶sub xxd {
Subroutine xxd: called CC=3 SCAR=11.0
Pod documented
38 2 my $length = length $_[0];
39 2 my $offset = 0;
40 2 my @retval;
41 2 ▶ while ($offset < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 32 2 34
42 32 push @retval,
43 _xxd_line($offset, split //, substr $_[0], $offset, 16);
44 32 $offset += 16;
45 }
46 2 ▶ return @retval if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
47 2 return join "\n", @retval;
48 } ## end sub xxd
49 3 *xxd_unpack = \&xxd;
50  
51 2 ▶sub xxd_r {
Subroutine xxd_r: called CC=4 SCAR=14.1
Pod documented
52 2 my @retval;
53 2 ▶ for my $line (scalar(@_) > 1 ? @_ : split /\n/, $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 0 2 2
54 32 my ($payload) = $line =~ m{\A\S+:\s (.*?) \s\s}xms;
55 32 ▶ next unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 32 32
56 32 $payload =~ s/\s//g;
57 32 push @retval, pack 'H*', $payload;
58 }
59 2 return join '', @retval;
60 } ## end sub xxd_r
61 3 *xxd_pack = \&xxd_r;
62  
63 3 1; # Magic true value required at end of module
64 __END__
↑ Summary