blib/lib/Table/Readable.pm

filter: Statementstmt 100% 128 / 128 Branchbran 91.0% 51 / 56 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 75.0% 3 / 4 totaltotal 97.0% 195 / 201 SCAR 36.1
CC
37
Cov
97%
read_table
17.0
write_table
14.0
read_table_hash
5.0
CRAP 37.0
↑ Summary
1 package Table::Readable;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 require Exporter;
5 6 our @ISA = qw(Exporter);
6 6 our @EXPORT_OK = qw/read_table write_table read_table_hash/;
7 6 our %EXPORT_TAGS = (all => \@EXPORT_OK);
8 6 our $VERSION = '0.05';
9 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub read_file
12 {
13 15 ▶ my ($file) = @_;
Subroutine read_file: called CC=4 SCAR=13.9
Pod undocumented
14 15 my @rv;
15 15 ▶ open my $in, "<:encoding(utf8)", $file or croak "Error opening '$file': $!";
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:encoding(utf8)", $file or croak("Error opening '${file}': $!") 1 14 15
16 14 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 143 14 157
17 143 push @rv, $_;
18 }
19 14 ▶ close $in or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $in or croak($!) 0 14 14
20 14 return @rv;
21 }
22  
23 sub read_table
24 {
25 21 ▶ my ($list_file, %options) = @_;
Subroutine read_table: called CC=17 SCAR=28.3
Pod documented
26 21 my @table;
27 21 my $row = {};
28 21 push @table, $row;
29 21 my $mode = "single-line";
30 21 my $mkey;
31  
32 my @lines;
33 21 ▶ if ($options{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'scalar'}) { } 6 15 21
34 6 @lines = split /\n/, $list_file;
35 6 for (@lines) {
36 12 $_ .= "\n";
37 }
38 6 $lines[-1] =~ s/\n$//;
39 }
40 else {
41 15 @lines = read_file ($list_file);
42 }
43 20 my $count = 0;
44 20 for (@lines) {
45  
46 154 $count++;
47  
48 154 ▶ if ($mode ne 'multi-line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "multi-line") 112 42 154
49  
50 # Detect the first line of a cell of the table whose
51 # information spans several lines of the input file.
52  
53 112 ▶ if (/^%%\s*([^:]+):\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%\s*([^:]+):\s*$/) 6 106 112
54 6 $mode = "multi-line";
55 6 $mkey = $1;
56 6 next;
57 }
58 }
59  
60 # Continue to process a table cell whose information spans
61 # several lines of the input file.
62  
63 148 ▶ if ($mode eq "multi-line") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "multi-line") 42 106 148
64 42 ▶ if (/^%%\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%\s*$/) 6 36 42
65 6 $mode = "single-line";
66 6 ▶ if ($row->{$mkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{$mkey}) 6 0 6
67 # Strip leading and trailing whitespace
68 6 $row->{$mkey} =~ s/^\s+|\s+$//g;
69 # Strip leading and trailing slashes
70 6 $row->{$mkey} =~ s/^\\|\\$//g;
71 }
72 6 $mkey = undef;
73 6 next;
74 }
75 36 $row->{$mkey} .= $_;
76 36 next;
77 }
78 106 ▶ if (/^\s*#.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*#.*/) 2 104 106
79  
80 # Skip comments.
81  
82 2 next;
83 }
84 104 ▶ if (/([^:]+):\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/([^:]+):\s*(.*?)\s*$/) 76 28 104
85  
86 # Key / value pair on a single line.
87  
88 76 my $key = $1;
89 76 my $value = $2;
90  
91 # If there are any spaces in the key, substitute them with
92 # underscores.
93  
94 76 $key =~ s/\s/_/g;
95 76 ▶ if ($row->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{$key}) 1 75 76
96 1 croak "$list_file:$count: duplicate for key $key\n";
97 }
98 # Strip leading and trailing slashes
99 75 $value =~ s/^\\|\\$//g;
100 75 $row->{$key} = $value;
101 75 next;
102 }
103 28 ▶ if (/^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/) 27 1 28
104  
105 # A blank line signifies the end of a row.
106  
107 27 ▶ if (keys %$row > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$row > 0) 23 4 27
108 23 $row = {};
109 23 push @table, $row;
110 }
111 27 next;
112 }
113 1 my $file_line = "$list_file:$count:";
114 1 ▶ if ($options{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"scalar"}) 1 0 1
115 1 $file_line = "$count:";
116 }
117 1 warn "$file_line unmatched line '$_'\n";
118 }
119 # Deal with the case of whitespace at the end of the file.
120 19 my $last_row = $table[-1];
121 19 ▶ if (keys %$last_row == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$last_row == 0) 8 11 19
122 8 pop @table;
123 }
124 19 ▶ croak "read_table returns an array" unless wantarray ();
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 17 19
125 17 return @table;
126 }
127  
128 sub read_table_hash
129 {
130 4 ▶ my ($list_file, $key, %options) = @_;
Subroutine read_table_hash: called CC=5 SCAR=16.1
Pod documented
131 4 my @table = read_table ($list_file, %options);
132 4 my %hash;
133 4 my $i = -1;
134 4 my @order;
135 4 for my $entry (@table) {
136 12 $i++;
137 12 my $ekey = $entry->{$key};
138 12 push @order, $ekey;
139 12 ▶ if (! $ekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ekey) 2 10 12
140 2 carp "No $key entry for element $i of $list_file";
141 2 next;
142 }
143 10 ▶ if ($hash{$ekey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{$ekey}) 2 8 10
144 2 carp "Table entries for $key are not unique, duplicate at $i";
145 2 next;
146 }
147 8 $hash{$ekey} = $entry;
148 }
149 4 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 3 4
150 1 return \%hash, \@order;
151 }
152 else {
153 3 return \%hash;
154 }
155 }
156  
157 # Maximum length of a single-line entry.
158  
159 6 our $maxlen = 75;
160  
161 sub write_table
162 {
163 7 ▶ my ($list, $file) = @_;
Subroutine write_table: called CC=14 SCAR=26.4
Pod documented
164 7 ▶ if (ref $list ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list ne "ARRAY") 1 6 7
165 1 carp "First argument to 'write_table' must be array reference";
166 1 return;
167 }
168 6 my $n = 0;
169 6 for my $i (@$list) {
170 7 ▶ if (ref $i ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i ne "HASH") 1 6 7
171 1 carp "Elements of first argument to 'write_table' must be hash references";
172 1 return;
173 }
174 6 for my $k (keys %$i) {
175 12 ▶ if (ref $i->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i->{$k}) 1 11 12
176 1 carp "Non-scalar value in key $k of element $n";
177 1 return;
178 }
179 }
180 5 $n++;
181 }
182 4 my $text = '';
183 4 for (@$list) {
184 5 for my $k (sort keys %$_) {
185 11 my $v = $_->{$k};
186 11 ▶ if (length ($v) + length ($k) > $maxlen ||
Branch: 1 branch
BranchTrueFalseTotal
if (length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/) { } 2 9 11
Condition100%3/3
length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/
l!l&&r!l&&!r
119
MC/DC100%2/2
length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/
length($v) + length($k) > $Table::Readable::maxlen $v =~ /\n/
Truth table100%3/3
A or B
A= length($v) + length($k) > $Table::Readable::maxlenB= $v =~ /\n/
ABresult
1X1
011
000
187 $v =~ /\n/) {
188 2 $text .= "%%$k:\n$v\n%%\n";
189 }
190 else {
191 9 $text .= "$k: $v\n";
192 }
193 }
194 5 $text .= "\n";
195 }
196 4 ▶ if ($file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file) { } 2 2 4
elsif (defined wantarray) { } 1 1 2
197 2 ▶ open my $out, ">:encoding(utf8)", $file or croak "Can't open $file for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">:encoding(utf8)", $file or croak("Can't open $file for writing: $!") 0 2 2
198 2 print $out $text;
199 2 ▶ close $out or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or croak($!) 0 2 2
200 }
201 elsif (defined (wantarray ())) {
202 1 return $text;
203 }
204 else {
205 1 print $text;
206 }
207 }
208  
209 6 1;
↑ Summary