blib/lib/Table/Readable.pm

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