blib/lib/Table/Readable.pm

filter: Statementstmt 87.9% 160 / 182 Branchbran 76.2% 61 / 80 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 81.2% 13 / 16 Podpod 54.5% 6 / 11 totaltotal 83.3% 245 / 294 CC 53 SCAR 24.1
CC
53
Cov
85%
edit_entry
30.0
read_table
18.0
append_table
12.0
CRAP 11.1
↑ Summary
1 package Table::Readable;
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 require Exporter;
5 7 our @ISA = qw(Exporter);
6 7 our @EXPORT_OK = qw/
7 append_table
8 read_table
9 read_table_hash
10 replace
11 write_table
12 /;
13 7 our %EXPORT_TAGS = (all => \@EXPORT_OK);
14 7 our $VERSION = '0.07';
15 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Private routine. This reads the file in and turns it into an array
20 # containing the lines of the file, without doing any further
21 # processing work.
22  
23 sub read_file
24 15 ▶{
Subroutine read_file: called CC=4 SCAR=13.9
Pod undocumented
25 15 my ($file) = @_;
26 15 my @rv;
27 15 ▶ open my $in, "<:encoding(utf8)", $file or croak "Error opening '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:encoding(utf8)", $file or croak("Error opening '${file}': $!") 14 1 15
28 14 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 143 14 157
29 143 push @rv, $_;
30 }
31 14 ▶ close $in or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $in or croak($!) 14 0 14
32 14 return @rv;
33 }
34  
35 sub scalar_to_lines
36 6 ▶{
Subroutine scalar_to_lines: called CC=2 SCAR=6.9
Pod undocumented
37 6 my ($list_file) = @_;
38 6 my @lines = split /\n/, $list_file;
39 6 for (@lines) {
40 12 $_ .= "\n";
41 }
42 6 $lines[-1] =~ s/\n$//;
43 6 return @lines;
44 }
45  
46 sub read_table
47 23 ▶{
Subroutine read_table: called CC=18 SCAR=28.9
Pod documented
48 23 ▶ croak "read_table returns an array" unless wantarray ();
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 21 23
49 21 my ($list_file, %options) = @_;
50 21 my $noslash = $options{noslash};
51 21 if (0 && $noslash) {
52 print "Not stripping out slashes\n";
53 }
54 # Return value
55 21 my @table;
56 21 my $row = {};
57 21 push @table, $row;
58 # This variable controls whether we parse the current entry as a
59 # key/value pair on a single line, or one spanning multiple lines.
60 21 my $mode = "single-line";
61 21 my $mkey;
62  
63 my @lines;
64 21 ▶ if ($options{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'scalar'}) { } 6 15 21
65 6 @lines = scalar_to_lines ($list_file);
66 }
67 else {
68 15 @lines = read_file ($list_file);
69 }
70 20 my $count = 0;
71 20 for (@lines) {
72  
73 154 $count++;
74  
75 154 ▶ if ($mode ne 'multi-line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "multi-line") 112 42 154
76  
77 # Detect the first line of a cell of the table whose
78 # information spans several lines of the input file.
79  
80 112 ▶ if (/^%%\s*([^:]+):\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%\s*([^:]+):\s*$/u) 6 106 112
81 6 $mode = "multi-line";
82 6 $mkey = $1;
83 6 next;
84 }
85 }
86  
87 # Continue to process a table cell whose information spans
88 # several lines of the input file.
89  
90 148 ▶ if ($mode eq "multi-line") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "multi-line") 42 106 148
91 42 ▶ if (/^%%\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%\s*$/u) 6 36 42
92 6 $mode = "single-line";
93 6 ▶ if ($row->{$mkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{$mkey}) 6 0 6
94 # Strip leading and trailing whitespace
95 6 $row->{$mkey} =~ s/^\s+|\s+$//g;
96 6 ▶ if (! $noslash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noslash) 6 0 6
97 # Strip leading and trailing slashes
98 6 $row->{$mkey} =~ s/^\\|\\$//g;
99 }
100 }
101 6 $mkey = undef;
102 6 next;
103 }
104 36 $row->{$mkey} .= $_;
105 36 next;
106 }
107 106 ▶ if (/^\s*#.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*#.*/u) 2 104 106
108  
109 # Skip comments.
110  
111 2 next;
112 }
113 104 ▶ if (/([^:]+):\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/([^:]+):\s*(.*?)\s*$/u) 76 28 104
114  
115 # Key / value pair on a single line.
116  
117 76 my $key = $1;
118 76 my $value = $2;
119  
120 # If there are any spaces in the key, substitute them with
121 # underscores.
122  
123 76 $key =~ s/\s/_/g;
124 76 ▶ if ($row->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{$key}) 1 75 76
125 1 croak "$list_file:$count: duplicate for key $key\n";
126 }
127 # Strip leading and trailing slashes
128 75 ▶ if (! $noslash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noslash) 75 0 75
129 75 $value =~ s/^\\|\\$//g;
130 }
131 75 $row->{$key} = $value;
132 75 next;
133 }
134 28 ▶ if (/^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/u) 27 1 28
135  
136 # A blank line signifies the end of a row.
137  
138 27 ▶ if (keys %$row > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$row > 0) 23 4 27
139 23 $row = {};
140 23 push @table, $row;
141 }
142 27 next;
143 }
144 1 my $file_line = "$list_file:$count:";
145 1 ▶ if ($options{scalar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"scalar"}) 1 0 1
146 1 $file_line = "$count:";
147 }
148 1 warn "$file_line unmatched line '$_'\n";
149 }
150 # Deal with the case of whitespace at the end of the file.
151 19 my $last_row = $table[-1];
152 19 ▶ if (keys %$last_row == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$last_row == 0) 8 11 19
153 8 pop @table;
154 }
155 19 return @table;
156 }
157  
158 sub read_table_hash
159 4 ▶{
Subroutine read_table_hash: called CC=5 SCAR=16.1
Pod documented
160 4 my ($list_file, $key, %options) = @_;
161 4 my @table = read_table ($list_file, %options);
162 4 my %hash;
163 4 my $i = -1;
164 4 my @order;
165 4 for my $entry (@table) {
166 12 $i++;
167 12 my $ekey = $entry->{$key};
168 12 push @order, $ekey;
169 12 ▶ if (! $ekey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ekey) 2 10 12
170 2 carp "No $key entry for element $i of $list_file";
171 2 next;
172 }
173 10 ▶ if ($hash{$ekey}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{$ekey}) 2 8 10
174 2 carp "Table entries for $key are not unique, duplicate '$ekey' at $i";
175 2 next;
176 }
177 8 $hash{$ekey} = $entry;
178 }
179 4 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 3 4
180 1 return \%hash, \@order;
181 }
182 else {
183 3 return \%hash;
184 }
185 }
186  
187 sub sort_file
188 0 ▶{
Subroutine sort_file: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($file, $key) = @_;
190 0 my @lines = read_table ($file);
191 @lines = sort {
192 0 $a->{$key} cmp $b->{$key}
193 } @lines;
194 0 write_table (\@lines, $file);
195 }
196  
197 # Maximum length of a single-line entry.
198  
199 7 our $maxlen = 75;
200  
201 sub write_table
202 8 ▶{
Subroutine write_table: called CC=11 SCAR=24.0
Pod documented
203 8 my ($list, $file) = @_;
204 8 ▶ if (ref $list ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list ne "ARRAY") 1 7 8
205 1 carp "First argument to 'write_table' must be array reference";
206 1 return;
207 }
208 7 my $n = 0;
209 7 for my $i (@$list) {
210 9 ▶ if (ref $i ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i ne "HASH") 1 8 9
211 1 carp "Elements of first argument to 'write_table' must be hash references";
212 1 return;
213 }
214 8 for my $k (keys %$i) {
215 16 ▶ if (ref $i->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i->{$k}) 1 15 16
216 1 carp "Non-scalar value in key $k of element $n";
217 1 return;
218 }
219 }
220 7 $n++;
221 }
222 5 my $text = '';
223 5 for (@$list) {
224 7 $text .= entry_to_text($_);
225 }
226 5 ▶ if ($file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file) { } 3 2 5
elsif (defined wantarray) { } 1 1 2
227 3 ▶ 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: $!") 3 0 3
228 3 print $out $text;
229 3 ▶ close $out or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or croak($!) 3 0 3
230 }
231 elsif (defined (wantarray ())) {
232 1 return $text;
233 }
234 else {
235 1 print $text;
236 }
237 }
238  
239 4 ▶sub same_entry($a, $b) {
Subroutine same_entry: called CC=6 SCAR=19.0
Pod undocumented
240 4 for my $k (keys %$a) {
241 6 ▶ if (! defined $b->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $b->{$k}) 3 3 6
242 3 return 0;
243 }
244 3 ▶ if ($a->{$k} ne $b->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->{$k} ne $b->{$k}) 0 3 3
245 0 return 0;
246 }
247 }
248 1 for my $k (keys %$b) {
249 2 ▶ if (! defined $a->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a->{$k}) 0 2 2
250 0 return 0;
251 }
252 }
253 1 return 1;
254 }
255  
256 2 ▶sub replace($file, $old, $new) {
Subroutine replace: called CC=4 SCAR=13.9
Pod documented
257 2 my @table = read_table($file);
258 2 my @newtable;
259 my $changed;
260 2 for my $entry (@table) {
261 4 ▶ if (same_entry($entry, $old)) {
Branch: 1 branch
BranchTrueFalseTotal
if (same_entry($entry, $old)) 1 3 4
262 1 push @newtable, $new;
263 1 $changed = 1;
264 1 next;
265 }
266 3 push @newtable, $entry;
267 }
268 2 ▶ if (! $changed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changed) 1 1 2
269 1 warn "replace: entry not found in $file";
270 1 return;
271 }
272 1 write_table(\@newtable, $file);
273 }
274  
275 sub edit_entry
276 0 ▶{
Subroutine edit_entry: not called CC=5 SCAR=34.0
Pod undocumented
277 0 my ($file, $key, %entry) = @_;
278 0 my @table = read_table ($file);
279 0 my $v = $entry{$key};
280 0 ▶ if (! $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v) 0 0 0
281 0 croak "Values for entry don't contain an entry for $key";
282 }
283 0 for my $entry (@table) {
284 0 ▶ if (! $entry->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->{$key}) 0 0 0
285 0 next;
286 }
287 0 ▶ if ($entry->{$key} ne $entry{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{$key} ne $entry{$key}) 0 0 0
288 0 next;
289 }
290 0 print "Found the entry for $key.\n";
291 }
292 }
293  
294 sub append_table
295 0 ▶{
Subroutine append_table: not called CC=3 SCAR=24.8
Pod documented
296 0 my ($file, %entry) = @_;
297 0 ▶ open my $out, '>>:encoding(utf8)', $file or die "Failed to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">>:encoding(utf8)", $file or die "Failed to open ${file}: $!" 0 0 0
298 0 print $out entry_to_text(\%entry);
299 0 ▶ close $out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die $! 0 0 0
300 }
301  
302 sub entry_to_text
303 7 ▶{
Subroutine entry_to_text: called CC=4 SCAR=13.9
Pod undocumented
304 7 my ($entry) = @_;
305 7 my $text = '';
306 7 for my $k (sort keys %$entry) {
307 15 my $v = $entry->{$k};
308 15 ▶ if (length ($v) + length ($k) > $maxlen ||
Branch: 1 branch
BranchTrueFalseTotal
if (length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/u) { } 2 13 15
Condition100%3/3
length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/u
l!l&&r!l&&!r
1113
MC/DC100%2/2
length($v) + length($k) > $Table::Readable::maxlen or $v =~ /\n/u
length($v) + length($k) > $Table::Readable::maxlen $v =~ /\n/u
Truth table100%3/3
A or B
A= length($v) + length($k) > $Table::Readable::maxlenB= $v =~ /\n/u
ABresult
1X1
011
000
309 $v =~ /\n/) {
310 2 $text .= "%%$k:\n$v\n%%\n";
311 }
312 else {
313 13 $text .= "$k: $v\n";
314 }
315 }
316 7 $text .= "\n";
317 7 return $text;
318 }
319  
320 7 1;
↑ Summary