blib/lib/Text/Table/Manifold.pm

filter: Statementstmt 75.0% 207 / 276 Branchbran 40.2% 33 / 82 Conditioncond 28.0% 7 / 25 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 85.7% 36 / 42 Podpod 100% 8 / 8 totaltotal 65.1% 292 / 448 CC 75 SCAR 25.8
CC
75
Cov
64%
format_as_text_unicodebox_table
42.0
format_as_text_csv
30.0
_clean_data
27.8
CRAP 13.2
↑ Summary
1 package Text::Table::Manifold;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings qw(FATAL utf8); # Fatalize encoding glitches.
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use open qw(:std :utf8); # Undeclared streams in UTF-8.
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Const::Exporter constants =>
Subroutine BEGIN: called CC=1 SCAR=0.0
9 [
10 # Values of alignment().
11  
12 align_left => 0,
13 align_center => 1, # The default.
14 align_right => 2,
15  
16 # Values for empty(), i.e. empty string handling.
17  
18 empty_as_empty => 0, # Do nothing. The default.
19 empty_as_minus => 1,
20 empty_as_text => 2, # 'empty'.
21 empty_as_undef => 3,
22  
23 # Values for escape().
24  
25 escape_nothing => 0, # The default.
26 escape_html => 1,
27 escape_uri => 2,
28  
29 # Values for extend().
30  
31 extend_with_empty => 0, # The default.
32 extend_with_undef => 1,
33  
34 # Values for format().
35  
36 format_internal_boxed => 0, # The default.
37 format_text_csv => 1,
38 format_internal_github => 2,
39 format_internal_html => 3,
40 format_html_table => 4,
41 format_text_unicodebox_table => 5,
42  
43 # Values for include().
44  
45 include_data => 1, # Default.
46 include_footers => 2,
47 include_headers => 4, # Default.
48  
49 # Values for undef(), i.e. undef handling.
50  
51 undef_as_empty => 0,
52 undef_as_minus => 1,
53 undef_as_text => 2, # 'undef'.
54 undef_as_undef => 3, # Do nothing. The default.
55 3 ];
56  
57 3 ▶use HTML::Entities::Interpolate; # This module can't be loaded at runtime.
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 3 ▶use List::AllUtils 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 3 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 3 ▶use Types::Standard qw/Any ArrayRef HashRef Int Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 3 ▶use String::TtyLength 0.02 qw/ tty_width /;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 has alignment =>
70 (
71 3 ▶ default => sub{return []},
Subroutine __ANON__: called CC=1 SCAR=0.0
72 3 is => 'rw',
73 isa => ArrayRef,
74 required => 0,
75 );
76  
77 has data =>
78 (
79 1 ▶ default => sub{return []},
Subroutine __ANON__: called CC=1 SCAR=0.0
80 3 is => 'rw',
81 isa => ArrayRef,
82 required => 0,
83 );
84  
85 has empty =>
86 (
87 3 ▶ default => sub{return empty_as_empty},
Subroutine __ANON__: called CC=1 SCAR=0.0
88 3 is => 'rw',
89 isa => Int,
90 required => 0,
91 );
92  
93 has escape =>
94 (
95 3 ▶ default => sub{return escape_nothing},
Subroutine __ANON__: called CC=1 SCAR=0.0
96 3 is => 'rw',
97 isa => Int,
98 required => 0,
99 );
100  
101 has extend_data =>
102 (
103 3 ▶ default => sub{return extend_with_empty},
Subroutine __ANON__: called CC=1 SCAR=0.0
104 3 is => 'rw',
105 isa => Int,
106 required => 0,
107 );
108  
109 has extend_footers =>
110 (
111 3 ▶ default => sub{return extend_with_empty},
Subroutine __ANON__: called CC=1 SCAR=0.0
112 3 is => 'rw',
113 isa => Int,
114 required => 0,
115 );
116  
117 has extend_headers =>
118 (
119 3 ▶ default => sub{return extend_with_empty},
Subroutine __ANON__: called CC=1 SCAR=0.0
120 3 is => 'rw',
121 isa => Int,
122 required => 0,
123 );
124  
125 has format =>
126 (
127 3 ▶ default => sub{return format_internal_boxed},
Subroutine __ANON__: called CC=1 SCAR=0.0
128 3 is => 'rw',
129 isa => Int,
130 required => 0,
131 );
132  
133 has footers =>
134 (
135 3 ▶ default => sub{return []},
Subroutine __ANON__: called CC=1 SCAR=0.0
136 3 is => 'rw',
137 isa => ArrayRef,
138 required => 0,
139 );
140  
141 has headers =>
142 (
143 3 ▶ default => sub{return []},
Subroutine __ANON__: called CC=1 SCAR=0.0
144 3 is => 'rw',
145 isa => ArrayRef,
146 required => 0,
147 );
148  
149 has include =>
150 (
151 3 ▶ default => sub{return include_data | include_headers},
Subroutine __ANON__: called CC=1 SCAR=0.0
152 3 is => 'rw',
153 isa => Int,
154 required => 0,
155 );
156  
157 has join =>
158 (
159 3 ▶ default => sub{return ''},
Subroutine __ANON__: called CC=1 SCAR=0.0
160 3 is => 'rw',
161 isa => Str,
162 required => 0,
163 );
164  
165 has padding =>
166 (
167 3 ▶ default => sub{return 0},
Subroutine __ANON__: called CC=1 SCAR=0.0
168 3 is => 'rw',
169 isa => Int,
170 required => 0,
171 );
172  
173 has pass_thru =>
174 (
175 3 ▶ default => sub{return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
176 3 is => 'rw',
177 isa => HashRef,
178 required => 0,
179 );
180  
181 has undef =>
182 (
183 3 ▶ default => sub{return undef_as_undef},
Subroutine __ANON__: called CC=1 SCAR=0.0
184 3 is => 'rw',
185 isa => Int,
186 required => 0,
187 );
188  
189 has widths =>
190 (
191 3 ▶ default => sub{return []},
Subroutine __ANON__: called CC=1 SCAR=0.0
192 3 is => 'rw',
193 isa => ArrayRef,
194 required => 0,
195 );
196  
197 3 our $VERSION = '1.03';
198  
199 # ------------------------------------------------
200  
201 sub _align_to_center
202 49 ▶{
Subroutine _align_to_center: called CC=2 SCAR=6.9
203 49 my($self, $s, $width, $padding) = @_;
204 49 ▶ $s ||= '';
Condition100%2/2
$s ||= ""
l!l
3712
MC/DC100%1/1
$s ||= ""
$s
Truth table100%2/2
A
A= $s
Aresult
11
00
205 49 my($s_width) = tty_width($s);
206 49 my($left) = int( ($width - $s_width) / 2);
207 49 my($right) = $width - $s_width - $left;
208  
209 49 return (' ' x ($left + $padding) ) . $s . (' ' x ($right + $padding) );
210  
211 } # End of _align_to_center;
212  
213 # ------------------------------------------------
214  
215 sub _align_to_left
216 0 ▶{
Subroutine _align_to_left: not called CC=2 SCAR=17.9
217 0 my($self, $s, $width, $padding) = @_;
218 0 ▶ $s ||= '';
Condition0%0/2
$s ||= ""
l!l
00
MC/DC0%0/1
$s ||= ""
$s
Truth table0%0/2
A
A= $s
Aresult
11
00
219 0 my($s_width) = tty_width($s);
220 0 my($right) = $width - $s_width;
221  
222 0 return (' ' x $padding) . $s . (' ' x ($right + $padding) );
223  
224 } # End of _align_to_left;
225  
226 # ------------------------------------------------
227  
228 sub _align_to_right
229 0 ▶{
Subroutine _align_to_right: not called CC=2 SCAR=17.9
230 0 my($self, $s, $width, $padding) = @_;
231 0 ▶ $s ||= '';
Condition0%0/2
$s ||= ""
l!l
00
MC/DC0%0/1
$s ||= ""
$s
Truth table0%0/2
A
A= $s
Aresult
11
00
232 0 my($s_width) = tty_width($s);
233 0 my($left) = $width - $s_width;
234  
235 0 return (' ' x ($left + $padding) ) . $s . (' ' x $padding);
236  
237 } # End of _align_to_right;
238  
239 # ------------------------------------------------
240 # Apply empty_as_* and undef_as_* options, as well as escaping option(s).
241  
242 sub _clean_data
243 5 ▶{
Subroutine _clean_data: called CC=15 SCAR=33.3
244 5 my($self, $alignment, $headers, $data, $footers) = @_;
245 5 my($empty) = $self -> empty;
246 5 my($escape) = $self -> escape;
247 5 my($undef) = $self -> undef;
248  
249 5 ▶ use_module('URI::Escape') if ($escape == escape_uri);
Branch: 1 branch
BranchTrueFalseTotal
if $escape == 2 0 5 5
250  
251 5 my($s);
252  
253 5 for my $row (0 .. $#$data)
254 {
255 18 for my $column (0 .. $#{$$data[$row]})
256 {
257 78 $s = $$data[$row][$column];
258 78 ▶ $s = defined($s)
Branch: 8 branches
BranchTrueFalseTotal
defined $s ? : 78 0 78
length $s == 0 ? : 18 60 78
$empty == 1 ? : 0 18 18
$empty == 2 ? : 0 18 18
$empty == 3 ? : 0 18 18
$undef == 0 ? : 0 0 0
$undef == 1 ? : 0 0 0
$undef == 2 ? : 0 0 0
259 ? (length($s) == 0) # tty_width() should not be necessary here.
260 ? ($empty == empty_as_minus)
261 ? '-'
262 : ($empty == empty_as_text)
263 ? 'empty'
264 : ($empty == empty_as_undef)
265 ? undef
266 : $s # No need to check to empty_as_empty here!
267 : $s
268 : ($undef == undef_as_empty)
269 ? ''
270 : ($undef == undef_as_minus)
271 ? '-'
272 : ($undef == undef_as_text)
273 ? 'undef'
274 : $s; # No need to check for undef_as_undef here!
275  
276 78 ▶ $s = $Entitize{$s} if (defined($s) && ($escape == escape_html) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $s and $escape == 1 0 78 78
Condition33%1/3
defined $s and $escape == 1
!ll&&!rl&&r
0780
MC/DC0%0/2
defined $s and $escape == 1
defined $s $escape == 1
Truth table33%1/3
A and B
A= defined $sB= $escape == 1
ABresult
0X0
100
111
277 78 ▶ $s = URI::Escape::uri_escape($s) if ($escape == escape_uri); # Undef harmless here.
Branch: 1 branch
BranchTrueFalseTotal
if $escape == 2 0 78 78
278 78 $$data[$row][$column] = $s;
279 }
280 }
281  
282 } # End of _clean_data.
283  
284 # ------------------------------------------------
285  
286 sub format_as_html_table
287 0 ▶{
Subroutine format_as_html_table: not called CC=1 SCAR=6.9
Pod documented
288 0 my($self, $alignment, $headers, $data, $footers) = @_;
289  
290 0 my($html) = use_module('HTML::Table') -> new(%{${$self -> pass_thru}{new} }, -data => $data);
291  
292 0 return [$html -> getTable];
293  
294 } # End of format_as_html_table.
295  
296 # ------------------------------------------------
297  
298 sub format_as_internal_boxed
299 3 ▶{
Subroutine format_as_internal_boxed: called CC=8 SCAR=21.8
Pod documented
300 3 my($self, $alignment, $headers, $data, $footers) = @_;
301 3 my($padding) = $self -> padding;
302 3 my($widths) = $self -> widths;
303 3 my($separator) = '+' . join('+', map{'-' x ($_ + 2 * $padding)} @$widths) . '+';
304 3 my(@output) = $separator;
305  
306 3 my($align);
307 my(@s);
308  
309 3 for my $column (0 .. $#$widths)
310 {
311 11 $align = $$alignment[$column];
312  
313 11 ▶ if ($align == align_left)
Branch: 2 branches
BranchTrueFalseTotal
if ($align == 0) { } 0 11 11
elsif ($align == 1) { } 11 0 11
314 {
315 0 push @s, $self -> _align_to_left($$headers[$column], $$widths[$column], $padding);
316 }
317 elsif ($align == align_center)
318 {
319 11 push @s, $self -> _align_to_center($$headers[$column], $$widths[$column], $padding);
320 }
321 else
322 {
323 0 push @s, $self -> _align_to_right($$headers[$column], $$widths[$column], $padding);
324 }
325 }
326  
327 3 push @output, '|' . join('|', @s) . '|';
328 3 push @output, $separator;
329  
330 3 for my $row (0 .. $#$data)
331 {
332 10 @s = ();
333  
334 10 for my $column (0 .. $#$widths)
335 {
336 38 $align = $$alignment[$column];
337  
338 38 ▶ if ($align == align_left)
Branch: 2 branches
BranchTrueFalseTotal
if ($align == 0) { } 0 38 38
elsif ($align == 1) { } 38 0 38
339 {
340 0 push @s, $self -> _align_to_left($$data[$row][$column], $$widths[$column], $padding);
341 }
342 elsif ($align == align_center)
343 {
344 38 push @s, $self -> _align_to_center($$data[$row][$column], $$widths[$column], $padding);
345 }
346 else
347 {
348 0 push @s, $self -> _align_to_right($$data[$row][$column], $$widths[$column], $padding);
349 }
350 }
351  
352 10 push @output, '|' . join('|', @s) . '|';
353 }
354  
355 3 push @output, $separator;
356  
357 3 return [@output];
358  
359 } # End of format_as_internal_boxed.
360  
361 # ------------------------------------------------
362  
363 sub format_as_internal_github
364 1 ▶{
Subroutine format_as_internal_github: called CC=6 SCAR=18.6
Pod documented
365 1 my($self, $alignment, $headers, $data, $footers) = @_;
366 1 my(@output) = join('|', @$headers);
367 1 my($widths) = $self -> widths;
368  
369 1 my($align);
370 my(@line);
371  
372 1 for my $column (0 .. $#$widths)
373 {
374 5 $align = $$alignment[$column];
375  
376 5 ▶ if ($align == align_left)
Branch: 2 branches
BranchTrueFalseTotal
if ($align == 0) { } 0 5 5
elsif ($align == 1) { } 5 0 5
377 {
378 0 push @line, ':' . ('-' x $$widths[$column - 1]);
379 }
380 elsif ($align == align_center)
381 {
382 5 push @line, ':' . ('-' x $$widths[$column - 2]) . ':';
383 }
384 else
385 {
386 0 push @line, ('-' x $$widths[$column - 1]) . ':';
387 }
388 }
389  
390 1 push @output, join('|', @line);
391  
392 1 for my $row (0 .. $#$data)
393 {
394 4 ▶ push @output, join('|', map{defined($_) ? $_ : ''} @{$$data[$row]});
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 20 0 20
395 }
396  
397 1 return [@output];
398  
399 } # End of format_as_internal_github.
400  
401 # ------------------------------------------------
402  
403 sub format_as_internal_html
404 1 ▶{
Subroutine format_as_internal_html: called CC=13 SCAR=29.6
Pod documented
405 1 my($self, $alignment, $headers, $data, $footers) = @_;
406 1 my($table) = '';
407 1 ▶ my($table_options) = ${$self -> pass_thru}{new}{table} || {};
Condition50%1/2
do { $self->pass_thru }->{'new'}{'table'} || {}
l!l
01
MC/DC0%0/1
do { $self->pass_thru }->{'new'}{'table'} || {}
do { $self->pass_thru }->{'new'}{'table'}
Truth table50%1/2
A
A= do { $self->pass_thru }->{'new'}{'table'}
Aresult
11
00
408 1 my(@table_keys) = sort keys %$table_options;
409 1 my($include) = $self -> include;
410 1 my(%align) =
411 (
412 0 => "<div style='text-align: left'>",
413 1 => "<div style='text-align: center'>",
414 2 => "<div style='text-align: right'>",
415 );
416  
417 1 ▶ if (scalar @table_keys)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @table_keys) 0 1 1
418 {
419 0 $table .= ' ' . join(' ', map{qq|$_ = "$$table_options{$_}"|} sort keys %$table_options);
420 }
421  
422 1 my(@output) = "<table$table>";
423  
424 1 my(@line);
425 my($value);
426  
427 1 ▶ if ( ($include & include_headers) && ($#$headers >= 0) )
Branch: 1 branch
BranchTrueFalseTotal
if ($include & 4 and $headers->$#* >= 0) 1 0 1
Condition33%1/3
$include & 4 and $headers->$#* >= 0
!ll&&!rl&&r
001
MC/DC0%0/2
$include & 4 and $headers->$#* >= 0
$include & 4 $headers->$#* >= 0
Truth table33%1/3
A and B
A= $include & 4B= $headers->$#* >= 0
ABresult
0X0
100
111
428 {
429 1 push @output, '<thead>';
430  
431 1 for my $column (0 .. $#$headers)
432 {
433 5 push @line, "<th>$align{$$alignment[$column]}$$headers[$column]</div></th>";
434 }
435  
436 1 push @output, '<tr>' . join('', @line) . '</tr>';
437 1 push @output, '</thead>';
438 }
439  
440 1 ▶ if ($include & include_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($include & 1) 1 0 1
441 {
442 1 for my $row (0 .. $#$data)
443 {
444 4 @line = ();
445  
446 # Every row will have the same # of columns, so we pick 1.
447  
448 4 for my $column (0 .. $#{$$data[0]})
449 {
450 20 $value = $$data[$row][$column];
451 20 ▶ $value = defined($value) ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 20 0 20
452  
453 20 push @line, "<td>$align{$$alignment[$column]}$value</div></td>";
454 }
455  
456 4 push @output, '<tr>' . join('', @line) . '</tr>';
457 }
458 }
459  
460 1 ▶ if ( ($include & include_footers) && ($#$footers >= 0) )
Branch: 1 branch
BranchTrueFalseTotal
if ($include & 2 and $footers->$#* >= 0) 0 1 1
Condition33%1/3
$include & 2 and $footers->$#* >= 0
!ll&&!rl&&r
100
MC/DC0%0/2
$include & 2 and $footers->$#* >= 0
$include & 2 $footers->$#* >= 0
Truth table33%1/3
A and B
A= $include & 2B= $footers->$#* >= 0
ABresult
0X0
100
111
461 {
462 0 push @output, '<tfoot>';
463  
464 0 @line = ();
465  
466 0 for my $column (0 .. $#$footers)
467 {
468 0 push @line, "<th>$align{$$alignment[$column]}$$footers[$column]</div></th>";
469 }
470  
471 0 push @output, '<tr>' . join('', @line) . '</tr>';
472 0 push @output, '<tfoot>';
473 }
474  
475 1 push @output, '</table>';
476  
477 1 return [@output];
478  
479 } # End of format_as_internal_html.
480  
481 # ------------------------------------------------
482  
483 sub format_as_text_csv
484 0 ▶{
Subroutine format_as_text_csv: not called CC=5 SCAR=34.0
Pod documented
485 0 my($self, $alignment, $headers, $data, $footers) = @_;
486  
487 0 ▶ my($csv) = use_module('Text::CSV') -> new(${$self -> pass_thru}{new} || {});
Condition0%0/2
do { $self->pass_thru }->{'new'} || {}
l!l
00
MC/DC0%0/1
do { $self->pass_thru }->{'new'} || {}
do { $self->pass_thru }->{'new'}
Truth table0%0/2
A
A= do { $self->pass_thru }->{'new'}
Aresult
11
00
488 0 my($status) = $csv -> combine(@$headers);
489  
490 0 my(@output);
491  
492 0 ▶ if ($status)
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
493 {
494 0 push @output, $csv -> string;
495  
496 0 for my $row (0 .. $#$data)
497 {
498 0 $status = $csv -> combine(@{$$data[$row]});
499  
500 0 ▶ if ($status)
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
501 {
502 0 push @output, $csv -> string
503 }
504 else
505 {
506 0 die "Can't combine data:\nLine: " . $csv -> error_input . "\nMessage: " . $csv -> error_diag . "\n";
507 }
508 }
509 }
510 else
511 {
512 0 die "Can't combine headers:\nHeader: " . $csv -> error_input . "\nMessage: " . $csv -> error_diag . "\n";
513 }
514  
515 0 return [@output];
516  
517 } # End of format_as_text_csv.
518  
519 # ------------------------------------------------
520  
521 sub format_as_text_unicodebox_table
522 0 ▶{
Subroutine format_as_text_unicodebox_table: not called CC=6 SCAR=37.4
Pod documented
523 0 my($self, $alignment, $headers, $data, $footers) = @_;
524 0 my($include) = $self -> include;
525 0 my($table) = use_module('Text::UnicodeBox::Table') -> new(%{${$self -> pass_thru}{new} });
526  
527 0 ▶ if ( ($include & include_headers) && ($#$headers >= 0) )
Branch: 1 branch
BranchTrueFalseTotal
if ($include & 4 and $headers->$#* >= 0) 0 0 0
Condition0%0/3
$include & 4 and $headers->$#* >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$include & 4 and $headers->$#* >= 0
$include & 4 $headers->$#* >= 0
Truth table0%0/3
A and B
A= $include & 4B= $headers->$#* >= 0
ABresult
0X0
100
111
528 {
529 # Note: Text::UnicodeBox::Table does not support central alignment.
530  
531 0 ▶ my(@align) = map{ ($_ == align_left) ? 'left' : 'right'} @{$self -> alignment};
Branch: 1 branch
BranchTrueFalseTotal
$_ == 0 ? : 0 0 0
532  
533 0 $table -> add_header({alignment => [@align]}, @$headers);
534 }
535  
536 0 ▶ if ($include & include_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($include & 1) 0 0 0
537 {
538 0 for my $row (0 .. $#$data)
539 {
540 0 $table -> add_row(@{$$data[$row]});
541 }
542 }
543  
544 0 return [$table -> render];
545  
546 } # End of format_as_text_unicodebox_table.
547  
548 # ------------------------------------------------
549 # Find the maimum width of header/data/footer each column.
550  
551 sub _gather_statistics
552 5 ▶{
Subroutine _gather_statistics: called CC=3 SCAR=11.0
553 5 my($self, $alignment, $headers, $data, $footers) = @_;
554  
555 5 $self -> _rectify_data($alignment, $headers, $data, $footers);
556 5 $self -> _clean_data($alignment, $headers, $data, $footers);
557  
558 5 my(@column);
559 my($header_width);
560 5 my(@max_widths);
561  
562 5 for my $column (0 .. $#$headers)
563 {
564 21 @column = ($$headers[$column], $$footers[$column]);
565  
566 21 for my $row (0 .. $#$data)
567 {
568 78 push @column, $$data[$row][$column];
569 }
570  
571 21 push @max_widths, max map{ tty_width($_) } @column;
572 }
573  
574 5 $self -> widths(\@max_widths);
575  
576 } # End of _gather_statistics.
577  
578 # ------------------------------------------------
579 # Ensure all header/data/footer rows are the same length.
580  
581 sub _rectify_data
582 5 ▶{
Subroutine _rectify_data: called CC=13 SCAR=25.8
583 5 my($self, $alignment, $headers, $data, $footers) = @_;
584  
585 # Note: include is not validated, since it's a set of bit fields.
586  
587 5 $self -> _validate($self -> empty, empty_as_empty, empty_as_undef, 'empty');
588 5 $self -> _validate($self -> escape, escape_nothing, escape_uri, 'escape');
589 5 $self -> _validate($self -> extend_data, extend_with_empty, extend_with_undef, 'extend_data');
590 5 $self -> _validate($self -> extend_footers, extend_with_empty, extend_with_undef, 'extend_headers');
591 5 $self -> _validate($self -> extend_footers, extend_with_empty, extend_with_undef, 'extend_footers');
592 5 $self -> _validate($self -> format, format_internal_boxed, format_text_unicodebox_table, 'format');
593 5 $self -> _validate($self -> undef, undef_as_empty, undef_as_undef, 'undef');
594  
595 5 for my $alignment (@{$self -> alignment})
596 {
597 10 $self -> _validate($alignment, align_left, align_right, 'align');
598 }
599  
600 # Find the longest header/data/footer row. Ignore aligment.
601  
602 5 my($max_length) = 0;
603  
604 5 for my $row (0 .. $#$data)
605 {
606 18 ▶ $max_length = $#{$$data[$row]} if ($#{$$data[$row]} > $max_length);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$data[$row];} > $max_length 5 13 18
607 }
608  
609 5 $max_length = max $#$headers, $#$footers, $max_length;
610  
611 # Shrink the alignment row if necessary.
612  
613 5 ▶ $#$alignment = $max_length if ($#$alignment > $max_length);
Branch: 1 branch
BranchTrueFalseTotal
if $alignment->$#* > $max_length 0 5 5
614  
615 # Now expand all rows to be the same, maximum, length.
616  
617 5 my($filler) = align_center;
618 5 $$alignment[$_] = $filler for ($#$alignment + 1 .. $max_length);
619 5 ▶ $filler = ($self -> extend_headers == extend_with_empty) ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->extend_headers == 0 ? : 5 0 5
620 5 $$headers[$_] = $filler for ($#$headers + 1 .. $max_length);
621 5 ▶ $filler = ($self -> extend_footers == extend_with_empty) ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->extend_footers == 0 ? : 5 0 5
622 5 $$footers[$_] = $filler for ($#$footers + 1 .. $max_length);
623 5 ▶ $filler = ($self -> extend_data == extend_with_empty) ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->extend_data == 0 ? : 5 0 5
624  
625 5 for my $row (0 .. $#$data)
626 {
627 18 $$data[$row][$_] = $filler for ($#{$$data[$row]} + 1 .. $max_length);
628 }
629  
630 } # End of _rectify_data.
631  
632 # ------------------------------------------------
633  
634 sub render
635 5 ▶{
Subroutine render: called CC=8 SCAR=24.6
Pod documented
636 5 my($self, %hash) = @_;
637  
638 # Process parameters passed to render(), which can be the same as to new().
639  
640 5 for my $key (keys %hash)
641 {
642 0 $self -> $key($hash{$key});
643 }
644  
645 5 my($alignment) = $self -> alignment;
646 5 my($headers) = $self -> headers;
647 5 my($data) = $self -> data;
648 5 my($footers) = $self -> footers;
649 5 my($format) = $self -> format;
650  
651 5 $self -> _gather_statistics($alignment, $headers, $data, $footers);
652  
653 5 my($output);
654  
655 5 ▶ if ($format == format_internal_boxed)
Branch: 6 branches
BranchTrueFalseTotal
if ($format == 0) { } 3 2 5
elsif ($format == 2) { } 1 1 2
elsif ($format == 3) { } 1 0 1
elsif ($format == 4) { } 0 0 0
elsif ($format == 1) { } 0 0 0
elsif ($format == 5) { } 0 0 0
656 {
657 3 $output = $self -> format_as_internal_boxed($alignment, $headers, $data, $footers);
658 }
659 elsif ($format == format_internal_github)
660 {
661 1 $output = $self -> format_as_internal_github($alignment, $headers, $data, $footers);
662 }
663 elsif ($format == format_internal_html)
664 {
665 1 $output = $self -> format_as_internal_html($alignment, $headers, $data, $footers);
666 }
667 elsif ($format == format_html_table)
668 {
669 0 $output = $self -> format_as_html_table($alignment, $headers, $data, $footers);
670 }
671 elsif ($format == format_text_csv)
672 {
673 0 $output = $self -> format_as_text_csv($alignment, $headers, $data, $footers);
674 }
675 elsif ($format == format_text_unicodebox_table)
676 {
677 0 $output = $self -> format_as_text_unicodebox_table($alignment, $headers, $data, $footers);
678 }
679 else
680 {
681 0 die 'Error: format not implemented: ' . $format . "\n";
682 }
683  
684 5 return $output;
685  
686 } # End of render.
687  
688 # ------------------------------------------------
689  
690 sub render_as_string
691 0 ▶{
Subroutine render_as_string: not called CC=2 SCAR=17.9
Pod documented
692 0 my($self, %hash) = @_;
693 0 ▶ my($join) = defined($hash{join}) ? $hash{join} : $self -> join;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'join'} ? : 0 0 0
694  
695 0 return join($join, @{$self -> render(%hash)});
696  
697 } # End of render_as_string.
698  
699 # ------------------------------------------------
700  
701 sub _validate
702 45 ▶{
Subroutine _validate: called CC=3 SCAR=14.2
703 45 my($self, $value, $min, $max, $name) = @_;
704  
705 45 ▶ if ( ($value < $min) || ($value > $max) )
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $min or $value > $max) 0 45 45
Condition33%1/3
$value < $min or $value > $max
l!l&&r!l&&!r
0045
MC/DC0%0/2
$value < $min or $value > $max
$value < $min $value > $max
Truth table33%1/3
A or B
A= $value < $minB= $value > $max
ABresult
1X1
011
000
706 {
707 0 die "Error. The value for '$name', $value, is out of the range ($min .. $max)\n";
708 }
709  
710 } # End of _validate.
711  
712 # ------------------------------------------------
713  
714 3 1;
715  
716 =pod
717  
718 =encoding utf8
719  
720 =head1 NAME
721  
722 C<Text::Table::Manifold> - Render tables in manifold formats
723  
724 =head1 Synopsis
725  
726 This is scripts/synopsis.pl:
727  
728 #!/usr/bin/env perl
729  
730 use strict;
731 use utf8;
732 use warnings;
733 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
734 use open qw(:std :utf8); # Undeclared streams in UTF-8.
735  
736 use Text::Table::Manifold ':constants';
737  
738 # -----------
739  
740 # Set parameters with new().
741  
742 my($table) = Text::Table::Manifold -> new
743 (
744 alignment =>
745 [
746 align_left,
747 align_center,
748 align_right,
749 align_center,
750 ]
751 );
752  
753 $table -> headers(['Homepage', 'Country', 'Name', 'Metadata']);
754 $table -> data(
755 [
756 ['http://savage.net.au/', 'Australia', 'Ron Savage', undef],
757 ['https://duckduckgo.com/', 'Earth', 'Mr. S. Engine', ''],
758 ]);
759  
760 # Note: Save the data, since render() may update it.
761  
762 my(@data) = @{$table -> data};
763  
764 # Set parameters with methods.
765  
766 $table -> empty(empty_as_text);
767 $table -> format(format_internal_boxed);
768 $table -> undef(undef_as_text);
769  
770 # Set parameters with render().
771  
772 print "Format: format_internal_boxed: \n";
773 print join("\n", @{$table -> render(padding => 1)}), "\n";
774 print "\n";
775  
776 $table -> headers(['One', 'Two', 'Three']);
777 $table -> data(
778 [
779 ['Reichwaldstraße', 'Böhme', 'ʎ ʏ ʐ ʑ ʒ ʓ ʙ ʚ'],
780 ['ΔΔΔΔΔΔΔΔΔΔ', 'Πηληϊάδεω Ἀχιλῆος', 'A snowman: ☃'],
781 ['Two ticks: ✔✔', undef, '<table><tr><td>TBA</td></tr></table>'],
782 ]);
783  
784 # Save the data, since render() may update it.
785  
786 @data = @{$table -> data};
787  
788 $table -> empty(empty_as_minus);
789 $table -> format(format_internal_boxed);
790 $table -> undef(undef_as_text);
791 $table -> padding(2);
792  
793 print "Format: format_internal_boxed: \n";
794 print join("\n", @{$table -> render}), "\n";
795 print "\n";
796  
797 # Restore the saved data.
798  
799 $table -> data([@data]);
800  
801 # Etc.
802  
803 This is data/synopsis.log, the output of synopsis.pl:
804  
805 Format: format_internal_boxed:
806 +-------------------------+-----------+---------------+----------+
807 | Homepage | Country | Name | Metadata |
808 +-------------------------+-----------+---------------+----------+
809 | http://savage.net.au/ | Australia | Ron Savage | undef |
810 | https://duckduckgo.com/ | Earth | Mr. S. Engine | empty |
811 +-------------------------+-----------+---------------+----------+
812  
813 Format: format_internal_boxed:
814 +-------------------+---------------------+----------------------------------------+
815 | One | Two | Three |
816 +-------------------+---------------------+----------------------------------------+
817 | Reichwaldstraße | Böhme | ʎ ʏ ʐ ʑ ʒ ʓ ʙ ʚ |
818 | ΔΔΔΔΔΔΔΔΔΔ | Πηληϊάδεω Ἀχιλῆος | A snowman: ☃ |
819 | Two ticks: ✔✔ | undef | <table><tr><td>TBA</td></tr></table> |
820 +-------------------+---------------------+----------------------------------------+
821  
822 The latter table renders perfectly in FF, but not so in Chrome (today, 2015-01-31).
823  
824 =head1 Description
825  
826 Outputs tables in any one of several supported types.
827  
828 Features:
829  
830 =over 4
831  
832 =item o Generic interface to all supported table formats
833  
834 =item o Separately specify header/data/footer rows
835  
836 =item o Separately include/exclude header/data/footer rows
837  
838 =item o Align cell values
839  
840 Each column has its own alignment option, left, center or right.
841  
842 For internally generated HTML, this is done with a CSS C<div> within each C<td>, not with the obsolete
843 C<td align> attribute.
844  
845 But decimal places are not alignable, yet, as discussed in the L</TODO>.
846  
847 =item o Escape HTML entities or URIs
848  
849 But not both at the same time!
850  
851 =item o Extend short header/data/footer rows with empty strings or undef
852  
853 Auto-extension results in all rows being the same length.
854  
855 This takes place before the transformation, if any, mentioned next.
856  
857 =item o Tranform cell values which are empty strings and undef
858  
859 =item o Pad cell values
860  
861 =item o Handle UFT8
862  
863 =item o Return the table as an arrayref of lines or as a string
864  
865 The arrayref is returned by L</render([%hash])>, and the string by L</render_as_string([%hash])>.
866  
867 When returning a string by calling C<render_as_string()> (which calls C<render()>), you can specify
868 how the lines in the arrayref are joined.
869  
870 In the same way the C<format> parameter discussed just below controls the output, the C<join>
871 parameter controls the join.
872  
873 =back
874  
875 The format of the output is controlled by the C<format> parameter to C<new()>, or by the parameter
876 to the L</format([$format])> method, or by the value of the C<format> key in the hash passed to
877 L</render([%hash])> and L</render_as_string(%hash])>, and must be one of these imported constants:
878  
879 =over 4
880  
881 =item o format_internal_boxed
882  
883 All headers, footers and table data are surrounded by ASCII characters.
884  
885 The rendering is done internally.
886  
887 See scripts/internal.boxed.pl and output file data/internal.boxed.log.
888  
889 =item o format_internal_github
890  
891 Render as github-flavoured markdown.
892  
893 The rendering is done internally.
894  
895 See scripts/internal.github.pl and output file data/internal.github.log.
896  
897 =item o format_internal_html
898  
899 Render as a HTML table. You can use the L</pass_thru([$hashref])> method to set options for the HTML
900 table.
901  
902 The rendering is done internally.
903  
904 See scripts/internal.html.pl and output file data/internal.html.log.
905  
906 =item o format_html_table
907  
908 Passes the data to L<HTML::Table>. You can use the L</pass_thru([$hashref])> method to set options
909 for the C<HTML::Table> object constructor.
910  
911 Warning: You must use C<Text::Table::Manifold>'s C<data()> method, or the C<data> parameter to
912 C<new()>, and not the C<-data> option to C<HTML::Table>. This is because the module processes the
913 data before calling the C<HTML::Table> constructor.
914  
915 =item o format_text_csv
916  
917 Passes the data to L<Text::CSV>. You can use the L</pass_thru([$hashref])> method to set options for
918 the C<Text::CSV> object constructor.
919  
920 See scripts/text.csv.pl and output file data/text.csv.log.
921  
922 =item o format_text_unicodebox_table
923  
924 Passes the data to L<Text::UnicodeBox::Table>. You can use the L</pass_thru([$hashref])> method to
925 set options for the C<Text::UnicodeBox::Table> object constructor.
926  
927 See scripts/text.unicodebox.table.pl and output file data/text.unicodebox.table.log.
928  
929 =back
930  
931 See also scripts/synopsis.pl, and the output data/synopsis.log.
932  
933 =head1 Distributions
934  
935 This module is available as a Unix-style distro (*.tgz).
936  
937 See L<http://savage.net.au/Perl-modules/html/installing-a-module.html>
938 for help on unpacking and installing distros.
939  
940 =head1 Installation
941  
942 Install L<Text::Table::Manifold> as you would any C<Perl> module:
943  
944 Run:
945  
946 cpanm Text::Table::Manifold
947  
948 or run:
949  
950 sudo cpan Text::Table::Manifold
951  
952 or unpack the distro, and then either:
953  
954 perl Build.PL
955 ./Build
956 ./Build test
957 sudo ./Build install
958  
959 or:
960  
961 perl Makefile.PL
962 make (or dmake or nmake)
963 make test
964 make install
965  
966 =head1 Constructor and Initialization
967  
968 C<new()> is called as C<< my($parser) = Text::Table::Manifold -> new(k1 => v1, k2 => v2, ...) >>.
969  
970 It returns a new object of type C<Text::Table::Manifold>.
971  
972 Details of all parameters are explained in the L</FAQ>.
973  
974 Key-value pairs accepted in the parameter list (see corresponding methods for details
975 [e.g. L</data([$arrayref])>]):
976  
977 =over 4
978  
979 =item o alignment => $arrayref of imported constants
980  
981 This specifies alignment per column. There should be one array element per column of data. The
982 $arrayref will be auto-extended if necessary, using the constant C<align_center>.
983  
984 Alignment applies equally to every cell in the column.
985  
986 A value for this parameter is optional.
987  
988 Default: align_center for every column.
989  
990 =item o data => $arrayref_of_arrayrefs
991  
992 This specifies the table of cell values.
993  
994 An arrayref of arrayrefs, each inner arrayref is a row of data.
995  
996 The # of elements in each alignment/header/data/footer row does not have to be the same. See the
997 C<extend*> parameters for more. Auto-extension results in all rows being the same length.
998  
999 A value for this parameter is optional.
1000  
1001 Default: [].
1002  
1003 =item o empty => An imported constant
1004  
1005 This specifies how to transform cell values which are the empty string. See also the C<undef>
1006 parameter.
1007  
1008 The C<empty> parameter is activated after the C<extend*> parameters has been applied.
1009  
1010 A value for this parameter is optional.
1011  
1012 Default: empty_as_empty. I.e. do not transform.
1013  
1014 =item o escape => An imported constant
1015  
1016 This specifies escaping of either HTML entities or URIs.
1017  
1018 A value for this parameter is optional.
1019  
1020 Default: escape_nothing. I.e. do not transform.
1021  
1022 =item o extend_data => An imported constant
1023  
1024 The 2 constants available allow you to specify how short data rows are extended. Then, after
1025 extension, the transformations specified by the parameters C<empty> and C<undef> are applied.
1026  
1027 A value for this parameter is optional.
1028  
1029 Default: extend_with_empty. I.e. extend short data rows with the empty string.
1030  
1031 =item o extend_footers => An imported constant
1032  
1033 The 2 constants available allow you to specify how short footer rows are extended. Then, after
1034 extension, the transformations specified by the parameters C<empty> and C<undef> are applied.
1035  
1036 A value for this parameter is optional.
1037  
1038 Default: extend_with_empty. I.e. extend short footer rows with the empty string.
1039  
1040 =item o extend_headers => An imported constant
1041  
1042 The 2 constants available allow you to specify how short header rows are extended. Then, after
1043 extension, the transformations specified by the parameters C<empty> and C<undef> are applied.
1044  
1045 A value for this parameter is optional.
1046  
1047 Default: extend_with_empty. I.e. extend short header rows with the empty string.
1048  
1049 =item o footers => $arrayref
1050  
1051 These are the column footers. See also the C<headers> option.
1052  
1053 The # of elements in each header/data/footer row does not have to be the same. See the C<extend*>
1054 parameters for more.
1055  
1056 A value for this parameter is optional.
1057  
1058 Default: [].
1059  
1060 =item o format => An imported constant
1061  
1062 This specifies which format to output from the rendering methods.
1063  
1064 A value for this parameter is optional.
1065  
1066 Default: format_internal_boxed.
1067  
1068 =item o headers => $arrayref
1069  
1070 These are the column headers. See also the C<footers> option.
1071  
1072 The # of elements in each header/data/footer row does not have to be the same. See the C<extend*>
1073 parameters for more.
1074  
1075 A value for this parameter is optional.
1076  
1077 Default: [].
1078  
1079 =item o include => An imported constant
1080  
1081 Controls whether header/data/footer rows are included in the output.
1082  
1083 The are three constants available, and any of them can be combined with '|', the logical OR
1084 operator.
1085  
1086 A value for this parameter is optional.
1087  
1088 Default: include_headers | include_data.
1089  
1090 =item o join => $string
1091  
1092 L</render_as_string([%hash])> uses $hash{join}, or $self -> join, in Perl's
1093 C<join($join, @$araref)> to join the elements of the arrayref returned by internally calling
1094 L</render([%hash])>.
1095  
1096 C<render()> ignores the C<join> key in the hash.
1097  
1098 A value for this parameter is optional.
1099  
1100 Default: ''.
1101  
1102 =item o padding => $integer
1103  
1104 This integer is the # of spaces added to each side of the cell value, after the C<alignment>
1105 parameter has been applied.
1106  
1107 A value for this parameter is optional.
1108  
1109 Default: 0.
1110  
1111 =item o pass_thru => $hashref
1112  
1113 A hashref of values to pass thru to another object.
1114  
1115 The keys in this $hashref control what parameters are passed to rendering routines.
1116  
1117 A value for this parameter is optional.
1118  
1119 Default: {}.
1120  
1121 =item o undef => An imported constant
1122  
1123 This specifies how to transform cell values which are undef. See also the C<empty> parameter.
1124  
1125 The C<undef> parameter is activated after the C<extend*> parameters have been applied.
1126  
1127 A value for this parameter is optional.
1128  
1129 Default: undef_as_undef. I.e. do not transform.
1130  
1131 =back
1132  
1133 =head1 Methods
1134  
1135 See the L</FAQ> for details of all importable constants mentioned here.
1136  
1137 And remember, all methods listed here which are parameters to L</new([%hash])>, are also parameters
1138 to both L</render([%hash])> and L</render_as_string([%hash])>.
1139  
1140 =head2 alignment([$arrayref])
1141  
1142 Here, the [] indicate an optional parameter.
1143  
1144 Returns the alignment as an arrayref of constants, one per column.
1145  
1146 There should be one element in $arrayref for each column of data. If the $arrayref is too short,
1147 C<align_center> is the default for the missing alignments.
1148  
1149 Obviously, $arrayref might force spaces to be added to one or both sides of a cell value.
1150  
1151 Alignment applies equally to every cell in the column.
1152  
1153 This happens before any spaces specified by L</padding([$integer])> are added.
1154  
1155 See the L</FAQ#What are the constants for alignment?> for legal values for the alignments (per
1156 column).
1157  
1158 C<alignment> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1159  
1160 =head2 data([$arrayref])
1161  
1162 Here, the [] indicate an optional parameter.
1163  
1164 Returns the data as an arrayref. Each element in this arrayref is an arrayref of one row of data.
1165  
1166 The structure of C<$arrayref>, if provided, must match the description in the previous line.
1167  
1168 Rows do not need to have the same number of elements.
1169  
1170 Use Perl's C<undef> or '' (the empty string) for missing values.
1171  
1172 See L</empty([$empty])> and L</undef([$undef])> for how '' and C<undef> are handled.
1173  
1174 See L</extend_data([$extend])> for how to extend short data rows, or let the code extend auto-extend
1175 them.
1176  
1177 C<data> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1178  
1179 =head2 empty([$empty])
1180  
1181 Here, the [] indicate an optional parameter.
1182  
1183 Returns the option specifying how empty cell values ('') are being dealt with.
1184  
1185 $empty controls how empty strings in cells are rendered.
1186  
1187 See the L</FAQ#What are the constants for handling cell values which are empty strings?>
1188 for legal values for $empty.
1189  
1190 See also L</undef([$undef])>.
1191  
1192 C<empty> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1193  
1194 =head2 escape([$escape])
1195  
1196 Here, the [] indicate an optional parameter.
1197  
1198 Returns the option specifying how HTML entities and URIs are being dealt with.
1199  
1200 $escape controls how either HTML entities or URIs are rendered.
1201  
1202 See the L</FAQ#What are the constants for escaping HTML entities and URIs?>
1203 for legal values for $escape.
1204  
1205 C<escape> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1206  
1207 =head2 extend_data([$extend])
1208  
1209 Here, the [] indicate an optional parameter.
1210  
1211 Returns the option specifying how short data rows are extended.
1212  
1213 If the # of elements in a data row is shorter than the longest row, $extend
1214 specifies how to extend those short rows.
1215  
1216 See the L</FAQ#What are the constants for extending short rows?> for legal values for $extend.
1217  
1218 C<extend_data> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1219  
1220 =head2 extend_footers([$extend])
1221  
1222 Here, the [] indicate an optional parameter.
1223  
1224 Returns the option specifying how short footer rows are extended.
1225  
1226 If the # of elements in a footer row is shorter than the longest row, $extend
1227 specifies how to extend those short rows.
1228  
1229 See the L</FAQ#What are the constants for extending short rows?> for legal values for $extend.
1230  
1231 C<extend_footers> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1232  
1233 =head2 extend_headers([$extend])
1234  
1235 Here, the [] indicate an optional parameter.
1236  
1237 Returns the option specifying how short header rows are extended.
1238  
1239 If the # of elements in a header row is shorter than the longest row, $extend
1240 specifies how to extend those short rows.
1241  
1242 See the L</FAQ#What are the constants for extending short rows?> for legal values for $extend.
1243  
1244 C<extend_headers> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1245  
1246 =head2 footers([$arrayref])
1247  
1248 Here, the [] indicate an optional parameter.
1249  
1250 Returns the footers as an arrayref of strings.
1251  
1252 $arrayref, if provided, must be an arrayref of strings.
1253  
1254 See L</extend_footers([$extend])> for how to extend a short footer row, or let the code auto-extend
1255 it.
1256  
1257 C<footers> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1258  
1259 =head2 format([$format])
1260  
1261 Here, the [] indicate an optional parameter.
1262  
1263 Returns the format as a constant (actually an integer).
1264  
1265 See the L</FAQ#What are the constants for formatting?> for legal values for $format.
1266  
1267 C<format> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1268  
1269 =head2 format_as_internal_boxed()
1270  
1271 Called by L</render([%hash])>.
1272  
1273 =head2 format_as_internal_github()
1274  
1275 Called by L</render([%hash])>.
1276  
1277 =head2 format_as_internal_html()
1278  
1279 Called by L</render([%hash])>.
1280  
1281 =head2 format_as_html_table()
1282  
1283 Called by L</render([%hash])>.
1284  
1285 =head2 format_as_text_csv().
1286  
1287 Called by L</render([%hash])>.
1288  
1289 =head2 format_as_text_unicodebox_table()
1290  
1291 Called by L</render([%hash])>.
1292  
1293 =head2 headers([$arrayref])
1294  
1295 Here, the [] indicate an optional parameter.
1296  
1297 Returns the headers as an arrayref of strings.
1298  
1299 $arrayref, if provided, must be an arrayref of strings.
1300  
1301 See L</extend_headers([$extend])> for how to extend a short header row, or let the code auto-extend
1302 it.
1303  
1304 C<headers> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1305  
1306 =head2 include([$include])
1307  
1308 Here, the [] indicate an optional parameter.
1309  
1310 Returns the option specifying if header/data/footer rows are included in the output.
1311  
1312 See the L</FAQ#What are the constants for including/excluding rows in the output?> for legal values
1313 for $include.
1314  
1315 C<include> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1316  
1317 =head2 join([$join])
1318  
1319 Here, the [] indicate an optional parameter.
1320  
1321 Returns the string used to join lines in the table when you call L</render_as_string([%hash])>.
1322  
1323 $join is the parameter passed to the Perl function C<join()> by C<render_as_string()>.
1324  
1325 Further, you can use the key C<join> in %hash to pass a value directly to
1326 L</render_as_string([%hash])>.
1327  
1328 =head2 new([%hash])
1329  
1330 The constructor. See L</Constructor and Initialization> for details of the parameter list.
1331  
1332 Note: L</render([%hash])> and L</render_as_string([%hash])>support the same options as C<new()>.
1333  
1334 =head2 padding([$integer])
1335  
1336 Here, the [] indicate an optional parameter.
1337  
1338 Returns the padding as an integer.
1339  
1340 Padding is the # of spaces to add to both sides of the cell value after it has been aligned.
1341  
1342 C<padding> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1343  
1344 =head2 pass_thru([$hashref])
1345  
1346 Here, the [] indicate an optional parameter.
1347  
1348 Returns the hashref previously provided.
1349  
1350 See L</FAQ#What is the format of the $hashref used in the call to pass_thru()?> for details.
1351  
1352 See scripts/html.table.pl, scripts/internal.table.pl and scripts/text.csv.pl for sample code where
1353 it is used in various ways.
1354  
1355 C<pass_thru> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1356  
1357 =head2 render([%hash])
1358  
1359 Here, the [] indicate an optional parameter.
1360  
1361 Returns an arrayref, where each element is 1 line of the output table. These lines do not have "\n"
1362 or any other line terminator added by this module.
1363  
1364 It's up to you how to handle the output. The simplest thing is to just do:
1365  
1366 print join("\n", @{$table -> render}), "\n";
1367  
1368 Note: C<render()> supports the same options as L</new([%hash])>.
1369  
1370 C<render()> ignores the C<join> key in the hash.
1371  
1372 See also L</render_as_string([%hash])>.
1373  
1374 =head2 render_as_string([%hash])
1375  
1376 Here, the [] indicate an optional parameter.
1377  
1378 Returns the rendered data as a string.
1379  
1380 C<render_as_string> uses the value of $hash{join}, or the result of calling $self -> join, in Perl's
1381 C<join($join, @$araref)> to join the elements of the arrayref returned by internally calling
1382 L</render([%hash])>.
1383  
1384 Note: C<render_as_string()> supports the same options as L</new([%hash])>, and passes them all to
1385 L</render([%hash])>.
1386  
1387 See also L</render([%hash])>.
1388  
1389 =head2 undef([$undef])
1390  
1391 Here, the [] indicate an optional parameter.
1392  
1393 Returns the option specifying how undef cell values are being dealt with.
1394  
1395 $undef controls how undefs in cells are rendered.
1396  
1397 See the L</FAQ#What are the constants for handling cell values which are undef?>
1398 for legal values for $undef.
1399  
1400 See also L</empty([$empty])>.
1401  
1402 C<undef> is a parameter to L</new([%hash])>. See L</Constructor and Initialization>.
1403  
1404 =head2 widths()
1405  
1406 Returns an arrayref of the width of each column, after the data is cleaned and rectified, but before
1407 it has been aligned or padded.
1408  
1409 =head1 FAQ
1410  
1411 Note: See L</TODO> for what has not been implemented yet.
1412  
1413 =head2 How are imported constants used?
1414  
1415 Firstly, you must import them with:
1416  
1417 use Text::Table::Manifold ':constants';
1418  
1419 Then you can use them in the constructor:
1420  
1421 my($table) = Text::Table::Manifold -> new(empty => empty_as_text);
1422  
1423 And/or you can use them in method calls:
1424  
1425 $table -> format(format_internal_boxed);
1426  
1427 See scripts/synopsis.pl for various use cases.
1428  
1429 Note how sample code uses the names of the constants. The integer values listed below are just FYI.
1430  
1431 =head2 What are the constants for alignment?
1432  
1433 The parameters, one per column, to L</alignment([$arrayref])> must be one of the following:
1434  
1435 =over 4
1436  
1437 =item o align_left => 0
1438  
1439 =item o align_center => 1
1440  
1441 So-spelt. Not 'centre'.
1442  
1443 =item o align_right => 2
1444  
1445 =back
1446  
1447 Alignment applies equally to every cell in a column.
1448  
1449 =head2 What are the constants for handling cell values which are empty strings?
1450  
1451 The parameter to L</empty([$empty])> must be one of the following:
1452  
1453 =over 4
1454  
1455 =item o empty_as_empty => 0
1456  
1457 Do nothing. This is the default.
1458  
1459 =item o empty_as_minus => 1
1460  
1461 Convert empty cell values to '-'.
1462  
1463 =item o empty_as_text => 2
1464  
1465 Convert empty cell values to the text string 'empty'.
1466  
1467 =item o empty_as_undef => 3
1468  
1469 Convert empty cell values to undef.
1470  
1471 =back
1472  
1473 See also L</undef([$undef])>.
1474  
1475 Warning: This updates the original data!
1476  
1477 =head2 What are the constants for escaping HTML entities and URIs?
1478  
1479 The parameter to L</escape([$escape])> must be one of the following:
1480  
1481 =over 4
1482  
1483 =item o escape_nothing => 0
1484  
1485 This is the default.
1486  
1487 =item o escape_html => 1
1488  
1489 Use L<HTML::Entities::Interpolate> to escape HTML entities. C<HTML::Entities::Interpolate> cannot
1490 be loaded at runtime, and so is always needed.
1491  
1492 =item o escape_uri => 2
1493  
1494 Use L<URI::Escape>'s uri_escape() method to escape URIs. C<URI::Escape> is loaded at runtime
1495 if needed.
1496  
1497 =back
1498  
1499 Warning: This updates the original data!
1500  
1501 =head2 What are the constants for extending short rows?
1502  
1503 The parameters to L</extend_data([$extend])>, L</extend_footers([$extend])> and
1504 L</extend_headers([$extend])>, must be one of the following:
1505  
1506 =over 4
1507  
1508 =item o extend_with_empty => 0
1509  
1510 Short header/data/footer rows are extended with the empty string.
1511  
1512 Later, the values discussed under
1513 L</FAQ#What are the constants for handling cell values which are empty strings?> will be applied.
1514  
1515 =item o extend_with_undef => 1
1516  
1517 Short header/data/footer rows are extended with undef.
1518  
1519 Later, the values discussed under
1520 L</FAQ#What are the constants for handling cell values which are undef?> will be applied.
1521  
1522 =back
1523  
1524 See also L</empty([$empty])> and L</undef([$undef])>.
1525  
1526 Warning: This updates the original data!
1527  
1528 =head2 What are the constants for formatting?
1529  
1530 The parameter to L</format([$format])> must be one of the following:
1531  
1532 =over 4
1533  
1534 =item o format_internal_boxed => 0
1535  
1536 Render internally.
1537  
1538 =item o format_text_csv => 1
1539  
1540 L<Text::CSV> is loaded at runtime if this option is used.
1541  
1542 =item o format_internal_github => 2
1543  
1544 Render internally.
1545  
1546 =item o format_internal_html => 3
1547  
1548 Render internally.
1549  
1550 =item o format_html_table => 4
1551  
1552 L<HTML::Table> is loaded at runtime if this option is used.
1553  
1554 =item o format_text_unicodebox_table => 5
1555  
1556 L<Text::UnicodeBox::Table> is loaded at runtime if this option is used.
1557  
1558 =back
1559  
1560 =head2 What are the constants for including/excluding rows in the output?
1561  
1562 The parameter to L</include([$include])> must be one or more of the following:
1563  
1564 =over 4
1565  
1566 =item o include_data => 1
1567  
1568 Data rows are included in the output.
1569  
1570 =item o include_footers => 2
1571  
1572 Footer rows are included in the output.
1573  
1574 =item o include_headers => 4
1575  
1576 Header rows are included in the output.
1577  
1578 =back
1579  
1580 =head2 What is the format of the $hashref used in the call to pass_thru()?
1581  
1582 It takes these (key => value) pairs:
1583  
1584 =over 4
1585  
1586 =item o new => $hashref
1587  
1588 =over 4
1589  
1590 =item o For internal rendering of HTML
1591  
1592 $$hashref{table} is used to specify parameters for the C<table> tag.
1593  
1594 Currently, C<table> is the only tag supported by this mechanism.
1595  
1596 =item o When using L<HTML::Table>, for external rendering of HTML
1597  
1598 $hashref is passed to the L<HTML::Table> constructor.
1599  
1600 =item o When using L<Text::CSV>, for external rendering of CSV
1601  
1602 $hashref is passed to the L<Text::CSV> constructor.
1603  
1604 =item o When using L<Text::UnicodeBox::Table>, for external rendering of boxes
1605  
1606 $hashref is passed to the L<Text::UnicodeBox::Table> constructor.
1607  
1608 =back
1609  
1610 =back
1611  
1612 See html.table.pl, internal.html.pl and text.csv.pl, all in the scripts/ directory.
1613  
1614 =head2 What are the constants for handling cell values which are undef?
1615  
1616 The parameter to L</undef([$undef])> must be one of the following:
1617  
1618 =over 4
1619  
1620 =item o undef_as_empty => 0
1621  
1622 Convert undef cell values to the empty string ('').
1623  
1624 =item o undef_as_minus => 1
1625  
1626 Convert undef cell values to '-'.
1627  
1628 =item o undef_as_text => 2
1629  
1630 Convert undef cell values to the text string 'undef'.
1631  
1632 =item o undef_as_undef => 3
1633  
1634 Do nothing.
1635  
1636 This is the default.
1637  
1638 =back
1639  
1640 See also L</empty([$undef])>.
1641  
1642 Warning: This updates the original data!
1643  
1644 =head2 Will you extend the program to support other external renderers?
1645  
1646 Possibly, but only if the extension matches the spirit of this module, which is roughly: Keep it
1647 simple, and provide just enough options but not too many options. IOW, there is no point in passing
1648 a huge number of options to an external class when you can use that class directly anyway.
1649  
1650 I've looked a number of times at L<PDF::Table>, for example, but it is just a little bit too
1651 complex. Similarly, L<Text::ANSITable> has too many methods.
1652  
1653 See also L</TODO>.
1654  
1655 =head2 How do I run author tests?
1656  
1657 This runs both standard and author tests:
1658  
1659 shell> perl Build.PL; ./Build; ./Build authortest
1660  
1661 =head1 TODO
1662  
1663 =over 4
1664  
1665 =item o Fancy alignment of real numbers
1666  
1667 It makes sense to right-justify integers, but in the rest of the table you probably want to
1668 left-justify strings.
1669  
1670 Then, vertically aligning decimal points (whatever they are in your locale) is another complexity.
1671  
1672 See L<Text::ASCIITable> and L<Text::Table>.
1673  
1674 =item o Embedded newlines
1675  
1676 Cell values could be split at each "\n" character, to find the widest line within the cell. That
1677 would be then used as the cell's width.
1678  
1679 For Unicode, this is complex. See L<http://www.unicode.org/versions/Unicode7.0.0/ch04.pdf>, and
1680 especially p 192, for 'Line break' controls. Also, the Unicode line breaking algorithm is documented
1681 in L<http://www.unicode.org/reports/tr14/>.
1682  
1683 Perl modules and other links relevant to this topic are listed under L</See Also#Line Breaking>.
1684  
1685 =item o Nested tables
1686  
1687 This really requires the implementation of embedded newline analysis, as per the previous point.
1688  
1689 =item o Pass-thru class support
1690  
1691 The problem is the mixture of options required to drive classes.
1692  
1693 =item o Sorting the rows, or individual columns
1694  
1695 See L<Data::Table> and L<HTML::Table>.
1696  
1697 =item o Color support
1698  
1699 See L<Text::ANSITable>.
1700  
1701 =item o Subtotal support
1702  
1703 Maybe one day. I did see a subtotal feature in a module while researching this, but I can't find it
1704 any more.
1705  
1706 See L<Data::Table>. It has grouping features.
1707  
1708 =back
1709  
1710 =head1 See Also
1711  
1712 =head2 Table Rendering
1713  
1714 L<Any::Renderer>
1715  
1716 L<Data::Formatter::Text>
1717  
1718 L<Data::Tab>
1719  
1720 L<Data::Table>
1721  
1722 L<Data::Tabulate>
1723  
1724 L<Gapp::TableMap>
1725  
1726 L<HTML::Table>
1727  
1728 L<HTML::Tabulate>
1729  
1730 L<LaTeX::Table>
1731  
1732 L<PDF::Table>
1733  
1734 L<PDF::TableX>
1735  
1736 L<PDF::Report::Table>
1737  
1738 L<Table::Simple>
1739  
1740 L<Term::TablePrint>
1741  
1742 L<Text::ANSITable>
1743  
1744 L<Text::ASCIITable>
1745  
1746 L<Text::CSV>
1747  
1748 L<Text::FormatTable>
1749  
1750 L<Text::MarkdownTable>
1751  
1752 L<Text::SimpleTable>
1753  
1754 L<Text::Table>
1755  
1756 L<Text::Table::Tiny>
1757  
1758 L<Text::TabularDisplay>
1759  
1760 L<Text::Tabulate>
1761  
1762 L<Text::UnicodeBox>
1763  
1764 L<Text::UnicodeBox::Table>
1765  
1766 L<Text::UnicodeTable::Simple>
1767  
1768 L<Tie::Array::CSV>
1769  
1770 =head2 Line Breaking
1771  
1772 L<Text::Format>
1773  
1774 L<Text::LineFold>
1775  
1776 L<Text::NWrap>
1777  
1778 L<Text::Wrap>
1779  
1780 L<Text::WrapI18N>
1781  
1782 L<Unicode::LineBreak>.
1783  
1784 L<UNICODE LINE BREAKING ALGORITHM|http://unicode.org/reports/tr14/>
1785  
1786 =head1 Machine-Readable Change Log
1787  
1788 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
1789  
1790 =head1 Version Numbers
1791  
1792 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
1793  
1794 =head1 Repository
1795  
1796 L<https://github.com/ronsavage/Text-Table-Manifold>
1797  
1798 =head1 Support
1799  
1800 Bugs should be reported via the CPAN bug tracker at
1801  
1802 L<https://github.com/ronsavage/Text-Table-Manifold/issues>
1803  
1804 =head1 Author
1805  
1806 L<Text::Table::Manifold> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2015.
1807  
1808 Marpa's homepage: L<http://savage.net.au/Marpa.html>.
1809  
1810 My homepage: L<http://savage.net.au/>.
1811  
1812 =head1 Copyright
1813  
1814 Australian copyright (c) 2014, Ron Savage.
1815  
1816 All Programs of mine are 'OSI Certified Open Source Software';
1817 you can redistribute them and/or modify them under the terms of
1818 The Perl Artistic License, a copy of which is available at:
1819 https://perldoc.perl.org/perlartistic.html.
1820  
1821 =cut
↑ Summary