line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Text::Table - Organize Data in Tables |
2
|
|
|
|
|
|
|
package Text::Table; |
3
|
|
|
|
|
|
|
$Text::Table::VERSION = '1.134'; |
4
|
6
|
|
|
6
|
|
352501
|
use strict; |
|
6
|
|
|
|
|
56
|
|
|
6
|
|
|
|
|
177
|
|
5
|
6
|
|
|
6
|
|
36
|
use warnings; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
140
|
|
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
147
|
use 5.008; |
|
6
|
|
|
|
|
27
|
|
8
|
|
|
|
|
|
|
|
9
|
6
|
|
|
6
|
|
34
|
use List::Util qw(sum max); |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
660
|
|
10
|
|
|
|
|
|
|
|
11
|
6
|
|
|
6
|
|
3185
|
use Text::Aligner qw(align); |
|
6
|
|
|
|
|
72433
|
|
|
6
|
|
|
|
|
474
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use overload ( |
14
|
|
|
|
|
|
|
# Don't stringify when only doing boolean tests, since stringification can |
15
|
|
|
|
|
|
|
# be expensive for large tables: |
16
|
1
|
|
|
1
|
|
271
|
bool => sub { return 1; }, |
17
|
|
|
|
|
|
|
# Stringify when Table instances are used in most other scalar cases: |
18
|
6
|
|
|
|
|
82
|
'""' => 'stringify', |
19
|
6
|
|
|
6
|
|
6375
|
); |
|
6
|
|
|
|
|
5297
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
### User interface: How to specify columns and column separators |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _is_sep { |
24
|
107
|
|
|
107
|
|
148
|
my $datum = shift; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
return |
27
|
|
|
|
|
|
|
( |
28
|
|
|
|
|
|
|
defined($datum) |
29
|
|
|
|
|
|
|
and |
30
|
|
|
|
|
|
|
( |
31
|
|
|
|
|
|
|
(ref($datum) eq 'SCALAR') |
32
|
|
|
|
|
|
|
or |
33
|
|
|
|
|
|
|
(ref($datum) eq 'HASH' and $datum->{is_sep}) |
34
|
107
|
|
33
|
|
|
572
|
) |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _get_sep_title_body |
39
|
|
|
|
|
|
|
{ |
40
|
16
|
|
|
16
|
|
24
|
my $sep = shift; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
return |
43
|
|
|
|
|
|
|
+( ref($sep) eq 'HASH' ) |
44
|
7
|
|
|
|
|
21
|
? @{ $sep }{qw(title body)} |
45
|
16
|
100
|
|
|
|
39
|
: split( /\n/, ${$sep}, -1 ) ; |
|
9
|
|
|
|
|
30
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _parse_sep { |
49
|
16
|
|
|
16
|
|
25
|
my $sep = shift; |
50
|
|
|
|
|
|
|
|
51
|
16
|
50
|
|
|
|
47
|
if (!defined($sep)) |
52
|
|
|
|
|
|
|
{ |
53
|
0
|
|
|
|
|
0
|
$sep = ''; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
16
|
|
|
|
|
33
|
my ($title, $body) = _get_sep_title_body($sep); |
57
|
|
|
|
|
|
|
|
58
|
16
|
100
|
|
|
|
38
|
if (!defined($body)) |
59
|
|
|
|
|
|
|
{ |
60
|
8
|
|
|
|
|
12
|
$body = $title; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
16
|
|
|
|
|
45
|
($title, $body) = align( 'left', $title, $body); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
return |
66
|
|
|
|
|
|
|
{ |
67
|
16
|
|
|
|
|
4999
|
is_sep => 1, |
68
|
|
|
|
|
|
|
title => $title, |
69
|
|
|
|
|
|
|
body => $body, |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _default_if_empty |
74
|
|
|
|
|
|
|
{ |
75
|
286
|
|
|
286
|
|
445
|
my ($ref, $default) = @_; |
76
|
|
|
|
|
|
|
|
77
|
286
|
100
|
100
|
|
|
692
|
if (! (defined($$ref) && length($$ref))) |
78
|
|
|
|
|
|
|
{ |
79
|
223
|
|
|
|
|
316
|
$$ref = $default; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
286
|
|
|
|
|
388
|
return; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _is_align |
86
|
|
|
|
|
|
|
{ |
87
|
184
|
|
|
184
|
|
245
|
my $align = shift; |
88
|
|
|
|
|
|
|
|
89
|
184
|
|
|
|
|
557
|
return $align =~ /\A(?:left|center|right)/; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _parse_spec { |
93
|
92
|
|
|
92
|
|
38346
|
my $spec = shift; |
94
|
|
|
|
|
|
|
|
95
|
92
|
100
|
|
|
|
191
|
if (!defined($spec)) |
96
|
|
|
|
|
|
|
{ |
97
|
2
|
|
|
|
|
4
|
$spec = ''; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
92
|
|
|
|
|
280
|
my $alispec = qr/^ *(?:left|center|right|num|point|auto)/; |
101
|
92
|
|
|
|
|
166
|
my ( $title, $align, $align_title, $align_title_lines, $sample); |
102
|
92
|
100
|
|
|
|
184
|
if ( ref ($spec) eq 'HASH' ) { |
103
|
|
|
|
|
|
|
( $title, $align, $align_title, $align_title_lines, $sample) = |
104
|
34
|
|
|
|
|
53
|
@{$spec}{qw( title align align_title align_title_lines sample )}; |
|
34
|
|
|
|
|
102
|
|
105
|
|
|
|
|
|
|
} else { |
106
|
58
|
|
|
|
|
128
|
my $alispec = qr/&(.*)/; |
107
|
58
|
100
|
|
|
|
288
|
if ( $spec =~ $alispec ) { |
108
|
21
|
|
|
|
|
181
|
($title, $align, $sample) = ($spec =~ /(.*)^$alispec\n?(.*)/sm); |
109
|
|
|
|
|
|
|
} else { |
110
|
37
|
|
|
|
|
59
|
$title = $spec; |
111
|
|
|
|
|
|
|
} |
112
|
58
|
|
|
|
|
112
|
for my $s ($title, $sample) |
113
|
|
|
|
|
|
|
{ |
114
|
116
|
100
|
|
|
|
246
|
if (defined($s)) |
115
|
|
|
|
|
|
|
{ |
116
|
79
|
|
|
|
|
164
|
chomp($s); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Assign default values. |
122
|
92
|
|
|
|
|
161
|
foreach my $x ($title, $sample) |
123
|
|
|
|
|
|
|
{ |
124
|
184
|
100
|
|
|
|
402
|
if (!defined($x)) |
|
|
100
|
|
|
|
|
|
125
|
|
|
|
|
|
|
{ |
126
|
60
|
|
|
|
|
103
|
$x = []; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
elsif (ref($x) ne 'ARRAY') |
129
|
|
|
|
|
|
|
{ |
130
|
104
|
|
|
|
|
309
|
$x = [ split /\n/, $x, -1]; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
92
|
|
|
|
|
234
|
_default_if_empty(\$align, 'auto'); |
135
|
|
|
|
|
|
|
|
136
|
92
|
50
|
66
|
|
|
524
|
unless ( |
137
|
|
|
|
|
|
|
ref $align eq 'Regexp' or |
138
|
|
|
|
|
|
|
$align =~ /^(?:left|center|right|num\(?|point\(?|auto)/ |
139
|
|
|
|
|
|
|
) { |
140
|
0
|
|
|
|
|
0
|
_warn( "Invalid align specification: '$align', using 'auto'"); |
141
|
0
|
|
|
|
|
0
|
$align = 'auto'; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
92
|
|
|
|
|
215
|
_default_if_empty(\$align_title, 'left'); |
145
|
|
|
|
|
|
|
|
146
|
92
|
50
|
|
|
|
170
|
if ( ! _is_align($align_title) ) { |
147
|
0
|
|
|
|
|
0
|
_warn( "Invalid align_title specification: " . |
148
|
|
|
|
|
|
|
"'$align_title', using 'left'", |
149
|
|
|
|
|
|
|
); |
150
|
0
|
|
|
|
|
0
|
$align_title = 'left'; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
92
|
|
|
|
|
235
|
_default_if_empty(\$align_title_lines, $align_title); |
154
|
|
|
|
|
|
|
|
155
|
92
|
50
|
|
|
|
154
|
if ( ! _is_align($align_title_lines) ) { |
156
|
0
|
|
|
|
|
0
|
_warn( "Invalid align_title_lines specification: " . |
157
|
|
|
|
|
|
|
"'$align_title_lines', using 'left'", |
158
|
|
|
|
|
|
|
); |
159
|
0
|
|
|
|
|
0
|
$align_title_lines = 'left'; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
return |
163
|
|
|
|
|
|
|
{ |
164
|
92
|
|
|
|
|
500
|
title => $title, |
165
|
|
|
|
|
|
|
align => $align, |
166
|
|
|
|
|
|
|
align_title => $align_title, |
167
|
|
|
|
|
|
|
align_title_lines => $align_title_lines, |
168
|
|
|
|
|
|
|
sample => $sample, |
169
|
|
|
|
|
|
|
}; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
### table creation |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub new |
175
|
|
|
|
|
|
|
{ |
176
|
35
|
|
|
35
|
1
|
14658
|
my $tb = bless {}, shift; |
177
|
|
|
|
|
|
|
|
178
|
35
|
|
|
|
|
177
|
return $tb->_entitle( [ @_ ] ); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub _blank |
182
|
|
|
|
|
|
|
{ |
183
|
217
|
|
|
217
|
|
307
|
my $self = shift; |
184
|
|
|
|
|
|
|
|
185
|
217
|
100
|
|
|
|
418
|
if (@_) |
186
|
|
|
|
|
|
|
{ |
187
|
189
|
|
|
|
|
306
|
$self->{blank} = shift; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
217
|
|
|
|
|
332
|
return $self->{blank}; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub _cols |
194
|
|
|
|
|
|
|
{ |
195
|
349
|
|
|
349
|
|
452
|
my $self = shift; |
196
|
|
|
|
|
|
|
|
197
|
349
|
100
|
|
|
|
599
|
if (@_) |
198
|
|
|
|
|
|
|
{ |
199
|
39
|
|
|
|
|
70
|
$self->{cols} = shift; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
349
|
|
|
|
|
981
|
return $self->{cols}; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub _forms |
206
|
|
|
|
|
|
|
{ |
207
|
180
|
|
|
180
|
|
220
|
my $self = shift; |
208
|
|
|
|
|
|
|
|
209
|
180
|
100
|
|
|
|
305
|
if (@_) |
210
|
|
|
|
|
|
|
{ |
211
|
39
|
|
|
|
|
63
|
$self->{forms} = shift; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
180
|
|
|
|
|
308
|
return $self->{forms}; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub _lines |
218
|
|
|
|
|
|
|
{ |
219
|
415
|
|
|
415
|
|
565
|
my $self = shift; |
220
|
|
|
|
|
|
|
|
221
|
415
|
100
|
|
|
|
684
|
if (@_) |
222
|
|
|
|
|
|
|
{ |
223
|
189
|
|
|
|
|
260
|
$self->{lines} = shift; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
415
|
|
|
|
|
730
|
return $self->{lines}; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub _spec |
230
|
|
|
|
|
|
|
{ |
231
|
259
|
|
|
259
|
|
360
|
my $self = shift; |
232
|
|
|
|
|
|
|
|
233
|
259
|
100
|
|
|
|
484
|
if (@_) |
234
|
|
|
|
|
|
|
{ |
235
|
39
|
|
|
|
|
223
|
$self->{spec} = shift; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
259
|
|
|
|
|
585
|
return $self->{spec}; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub _titles |
242
|
|
|
|
|
|
|
{ |
243
|
330
|
|
|
330
|
|
431
|
my $self = shift; |
244
|
|
|
|
|
|
|
|
245
|
330
|
100
|
|
|
|
549
|
if (@_) |
246
|
|
|
|
|
|
|
{ |
247
|
39
|
|
|
|
|
59
|
$self->{titles} = shift; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
330
|
|
|
|
|
721
|
return $self->{titles}; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
sub _entitle { |
254
|
39
|
|
|
39
|
|
76
|
my ($tb, $sep_list) = @_; # will be completely overwritten |
255
|
|
|
|
|
|
|
# find active separators and, well separate them from col specs. |
256
|
|
|
|
|
|
|
# n+1 separators for n cols |
257
|
39
|
|
|
|
|
86
|
my ( @seps, @spec); # separators and column specifications |
258
|
39
|
|
|
|
|
0
|
my $sep; |
259
|
39
|
|
|
|
|
61
|
foreach my $sep_item ( @{$sep_list} ) { |
|
39
|
|
|
|
|
89
|
|
260
|
82
|
100
|
|
|
|
160
|
if ( _is_sep ($sep_item) ) { |
261
|
16
|
|
|
|
|
47
|
$sep = _parse_sep($sep_item); |
262
|
|
|
|
|
|
|
} else { |
263
|
66
|
|
|
|
|
121
|
push @seps, $sep; |
264
|
66
|
|
|
|
|
116
|
push @spec, _parse_spec($sep_item); |
265
|
66
|
|
|
|
|
136
|
undef $sep; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
} |
268
|
39
|
|
|
|
|
76
|
push @seps, $sep; |
269
|
|
|
|
|
|
|
# build sprintf formats from separators |
270
|
39
|
|
|
|
|
106
|
my $title_form = _compile_field_format('title', \@seps); |
271
|
39
|
|
|
|
|
95
|
my $body_form = _compile_field_format('body', \@seps); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# pre_align titles |
274
|
39
|
|
|
|
|
102
|
my @titles = map { [ @{ $_->{title} } ] } @spec; |
|
66
|
|
|
|
|
83
|
|
|
66
|
|
|
|
|
201
|
|
275
|
|
|
|
|
|
|
|
276
|
39
|
|
|
|
|
95
|
my $title_height = max(0, map { scalar(@$_) } @titles); |
|
66
|
|
|
|
|
144
|
|
277
|
|
|
|
|
|
|
|
278
|
39
|
|
|
|
|
77
|
foreach my $title (@titles) |
279
|
|
|
|
|
|
|
{ |
280
|
66
|
|
|
|
|
106
|
push @{$title}, ( '') x ( $title_height - @{$title}); |
|
66
|
|
|
|
|
93
|
|
|
66
|
|
|
|
|
148
|
|
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
39
|
|
|
|
|
78
|
foreach my $t_idx (0 .. $#titles) |
284
|
|
|
|
|
|
|
{ |
285
|
66
|
|
|
|
|
5976
|
align($spec[$t_idx]->{align_title_lines}, @{$titles[$t_idx]}); |
|
66
|
|
|
|
|
171
|
|
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
# build data structure |
289
|
39
|
|
|
|
|
5308
|
$tb->_spec(\@spec); |
290
|
39
|
|
|
|
|
179
|
$tb->_cols([ map [], 1 .. @spec]); |
291
|
39
|
|
|
|
|
117
|
$tb->_forms([ $title_form, $body_form]); # separators condensed |
292
|
39
|
|
|
|
|
95
|
$tb->_titles(\@titles); |
293
|
|
|
|
|
|
|
|
294
|
39
|
|
|
|
|
89
|
$tb->_clear_cache; |
295
|
|
|
|
|
|
|
|
296
|
39
|
|
|
|
|
194
|
return $tb; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
# sprintf-format for line assembly, using separators |
300
|
|
|
|
|
|
|
sub _compile_format { |
301
|
78
|
|
|
78
|
|
123
|
my $seps = shift; # mix of strings and undef (for default) |
302
|
|
|
|
|
|
|
|
303
|
78
|
|
|
|
|
174
|
for my $idx (0 .. $#$seps) |
304
|
|
|
|
|
|
|
{ |
305
|
210
|
100
|
|
|
|
341
|
if (!defined($seps->[$idx])) |
306
|
|
|
|
|
|
|
{ |
307
|
180
|
100
|
100
|
|
|
562
|
$seps->[$idx] = ($idx == 0 or $idx == $#$seps) ? '' : q{ }; |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
else |
310
|
|
|
|
|
|
|
{ |
311
|
|
|
|
|
|
|
# protect against sprintf |
312
|
30
|
|
|
|
|
55
|
$seps->[$idx] =~ s/%/%%/g; |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
} |
315
|
78
|
|
|
|
|
267
|
return join '%s', @$seps; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
sub _compile_field_format |
319
|
|
|
|
|
|
|
{ |
320
|
78
|
|
|
78
|
|
145
|
my ($field, $seps) = @_; |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
return _compile_format( |
323
|
78
|
100
|
|
|
|
136
|
[map { defined($_) ? $_->{$field} : undef } @$seps] |
|
210
|
|
|
|
|
443
|
|
324
|
|
|
|
|
|
|
); |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
# reverse format compilation (used by colrange()) |
328
|
|
|
|
|
|
|
sub _recover_separators { |
329
|
16
|
|
|
16
|
|
20
|
my $format = shift; |
330
|
16
|
|
|
|
|
108
|
my @seps = split /(?
|
331
|
16
|
|
|
|
|
33
|
for my $s (@seps) |
332
|
|
|
|
|
|
|
{ |
333
|
48
|
|
|
|
|
65
|
$s =~ s/%%/%/g; |
334
|
|
|
|
|
|
|
} |
335
|
16
|
|
|
|
|
33
|
return \@seps; |
336
|
|
|
|
|
|
|
} |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# select some columns, (optionally if in [...]), and add new separators |
339
|
|
|
|
|
|
|
# (the other table creator) |
340
|
|
|
|
|
|
|
sub select { |
341
|
6
|
|
|
6
|
1
|
16
|
my $tb = shift; |
342
|
6
|
|
|
|
|
20
|
my @args = map $tb->_select_group( $_), @_; |
343
|
|
|
|
|
|
|
# get column selection, checking indices (some have been checked by |
344
|
|
|
|
|
|
|
# _select_group, but not all) |
345
|
6
|
|
|
|
|
14
|
my @sel = map $tb->_check_index( $_), grep !_is_sep( $_), @args; |
346
|
|
|
|
|
|
|
# replace indices with column spec to create subtable |
347
|
6
|
|
|
|
|
11
|
for my $arg (@args) |
348
|
|
|
|
|
|
|
{ |
349
|
10
|
50
|
|
|
|
18
|
if (! _is_sep($arg)) |
350
|
|
|
|
|
|
|
{ |
351
|
10
|
|
|
|
|
18
|
$arg = $tb->_spec->[ $arg]; |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
} |
354
|
6
|
|
|
|
|
22
|
my $sub = ref( $tb)->new( @args); |
355
|
|
|
|
|
|
|
# sneak in data columns |
356
|
6
|
|
|
|
|
13
|
@{ $sub->{ cols}} = map { [ @$_ ] } @{ $tb->_cols}[ @sel]; |
|
6
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
24
|
|
|
6
|
|
|
|
|
9
|
|
357
|
6
|
|
|
|
|
18
|
$sub; |
358
|
|
|
|
|
|
|
} |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# the first non-separator column in the group is tested if it has any data |
361
|
|
|
|
|
|
|
# if so, the group is returned, else nothing |
362
|
|
|
|
|
|
|
sub _select_group { |
363
|
13
|
|
|
13
|
|
24
|
my ( $tb, $group) = @_; |
364
|
13
|
100
|
|
|
|
35
|
return $group unless ref $group eq 'ARRAY'; |
365
|
|
|
|
|
|
|
GROUP_LOOP: |
366
|
5
|
|
|
|
|
10
|
for my $g ( @$group ) { |
367
|
5
|
50
|
|
|
|
10
|
if (_is_sep($g)) |
368
|
|
|
|
|
|
|
{ |
369
|
0
|
|
|
|
|
0
|
next GROUP_LOOP; |
370
|
|
|
|
|
|
|
} |
371
|
5
|
|
|
|
|
15
|
$tb->_check_index($g); |
372
|
|
|
|
|
|
|
|
373
|
5
|
100
|
|
|
|
7
|
if (grep { $_} @{ $tb->_cols->[$g] }) |
|
15
|
|
|
|
|
34
|
|
|
5
|
|
|
|
|
10
|
|
374
|
|
|
|
|
|
|
{ |
375
|
2
|
|
|
|
|
7
|
return @$group; |
376
|
|
|
|
|
|
|
} |
377
|
3
|
|
|
|
|
9
|
return; # no more tries after non-sep was found |
378
|
|
|
|
|
|
|
} |
379
|
0
|
|
|
|
|
0
|
return; # no column index in group, no select |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
# check index for validity, return arg if returns at all |
383
|
|
|
|
|
|
|
sub _check_index { |
384
|
15
|
|
|
15
|
|
23
|
my $tb = shift; |
385
|
15
|
|
|
|
|
23
|
my ( $i) = @_; |
386
|
15
|
|
|
|
|
32
|
my $n = $tb->n_cols; |
387
|
15
|
|
|
|
|
19
|
my $ok = eval { |
388
|
6
|
|
|
6
|
|
12674
|
use warnings FATAL => 'numeric'; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
3953
|
|
389
|
15
|
50
|
|
|
|
52
|
-$n <= $i and $i < $n; # in range of column array? |
390
|
|
|
|
|
|
|
}; |
391
|
15
|
50
|
33
|
|
|
47
|
_warn( "Invalid column index '$_'") if $@ or not $ok; |
392
|
15
|
|
|
|
|
30
|
shift; |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
### data entry |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub _clear_cache { |
398
|
157
|
|
|
157
|
|
247
|
my ($tb) = @_; |
399
|
|
|
|
|
|
|
|
400
|
157
|
|
|
|
|
335
|
$tb->_blank(undef()); |
401
|
157
|
|
|
|
|
293
|
$tb->_lines(undef()); |
402
|
|
|
|
|
|
|
|
403
|
157
|
|
|
|
|
200
|
return; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
# add one data line or split the line into follow-up lines |
407
|
|
|
|
|
|
|
sub add { |
408
|
57
|
|
|
57
|
1
|
99
|
my $tb = shift; |
409
|
|
|
|
|
|
|
|
410
|
57
|
100
|
|
|
|
95
|
if (! $tb->n_cols) { |
411
|
4
|
|
|
|
|
18
|
$tb->_entitle( [ ('') x @_] ); |
412
|
|
|
|
|
|
|
} |
413
|
|
|
|
|
|
|
|
414
|
57
|
|
|
|
|
104
|
foreach my $row ( |
415
|
|
|
|
|
|
|
_transpose( |
416
|
|
|
|
|
|
|
[ |
417
|
143
|
100
|
|
|
|
444
|
map { [ defined() ? split( /\n/ ) : '' ] } @_ |
418
|
|
|
|
|
|
|
] |
419
|
|
|
|
|
|
|
) |
420
|
|
|
|
|
|
|
) |
421
|
|
|
|
|
|
|
{ |
422
|
59
|
|
|
|
|
110
|
$tb->_add(@$row); |
423
|
|
|
|
|
|
|
} |
424
|
57
|
|
|
|
|
162
|
$tb->_clear_cache; |
425
|
|
|
|
|
|
|
|
426
|
57
|
|
|
|
|
98
|
return $tb; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
# add one data line |
430
|
|
|
|
|
|
|
sub _add { |
431
|
59
|
|
|
59
|
|
81
|
my $tb = shift; |
432
|
|
|
|
|
|
|
|
433
|
59
|
|
|
|
|
80
|
foreach my $col ( @{ $tb->_cols} ) { |
|
59
|
|
|
|
|
94
|
|
434
|
165
|
|
|
|
|
194
|
push @{$col}, shift(@_); |
|
165
|
|
|
|
|
286
|
|
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
59
|
|
|
|
|
125
|
$tb->_clear_cache; |
438
|
|
|
|
|
|
|
|
439
|
59
|
|
|
|
|
84
|
return $tb; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
# add one or more data lines |
443
|
|
|
|
|
|
|
sub load { |
444
|
21
|
|
|
21
|
1
|
1346
|
my $tb = shift; |
445
|
21
|
|
|
|
|
42
|
foreach my $row ( @_ ) { |
446
|
49
|
50
|
|
|
|
97
|
if (!defined($row)) { |
447
|
0
|
|
|
|
|
0
|
$row = ''; |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
$tb->add( |
450
|
49
|
100
|
|
|
|
158
|
(ref($row) eq 'ARRAY') ? (@$row) : (split ' ',$row) |
451
|
|
|
|
|
|
|
) |
452
|
|
|
|
|
|
|
} |
453
|
21
|
|
|
|
|
61
|
$tb; |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
sub clear { |
457
|
2
|
|
|
2
|
1
|
5
|
my $tb = shift; |
458
|
|
|
|
|
|
|
|
459
|
2
|
|
|
|
|
3
|
foreach my $col (@{ $tb->_cols} ) |
|
2
|
|
|
|
|
6
|
|
460
|
|
|
|
|
|
|
{ |
461
|
5
|
|
|
|
|
10
|
$col = []; |
462
|
|
|
|
|
|
|
} |
463
|
|
|
|
|
|
|
|
464
|
2
|
|
|
|
|
13
|
$tb->_clear_cache; |
465
|
|
|
|
|
|
|
|
466
|
2
|
|
|
|
|
5
|
return $tb; |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
### access to output area |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
## sizes |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
# number of table columns |
474
|
540
|
|
|
540
|
1
|
1202
|
sub n_cols { scalar @{ $_[0]->{ spec}} } |
|
540
|
|
|
|
|
1392
|
|
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
# number of title lines |
477
|
225
|
100
|
|
225
|
1
|
363
|
sub title_height { $_[ 0]->n_cols and scalar @{ $_[ 0]->_titles->[ 0]} } |
|
221
|
|
|
|
|
408
|
|
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
# number of data lines |
480
|
|
|
|
|
|
|
sub body_height |
481
|
|
|
|
|
|
|
{ |
482
|
209
|
|
|
209
|
1
|
323
|
my ($tb) = @_; |
483
|
|
|
|
|
|
|
|
484
|
209
|
|
100
|
|
|
316
|
return ($tb->n_cols && scalar @{ $tb->_cols->[0] }); |
485
|
|
|
|
|
|
|
} |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
# total height |
488
|
|
|
|
|
|
|
sub table_height |
489
|
|
|
|
|
|
|
{ |
490
|
130
|
|
|
130
|
1
|
180
|
my $tb = shift; |
491
|
130
|
|
|
|
|
223
|
return $tb->title_height + $tb->body_height; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
6
|
|
|
6
|
|
11951
|
BEGIN { *height = \&table_height; } # alias |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
# number of characters in each table line. need to build the table to know |
497
|
|
|
|
|
|
|
sub width |
498
|
|
|
|
|
|
|
{ |
499
|
38
|
|
|
38
|
1
|
91
|
my ($tb) = @_; |
500
|
|
|
|
|
|
|
|
501
|
38
|
|
66
|
|
|
82
|
return $tb->height && (length( ($tb->table(0))[0] ) - 1); |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
sub _normalize_col_index |
505
|
|
|
|
|
|
|
{ |
506
|
16
|
|
|
16
|
|
29
|
my ($tb, $col_index) = @_; |
507
|
|
|
|
|
|
|
|
508
|
16
|
|
100
|
|
|
48
|
$col_index ||= 0; |
509
|
|
|
|
|
|
|
|
510
|
16
|
100
|
|
|
|
37
|
if ($col_index < 0) |
511
|
|
|
|
|
|
|
{ |
512
|
2
|
|
|
|
|
7
|
$col_index += $tb->n_cols; |
513
|
|
|
|
|
|
|
} |
514
|
|
|
|
|
|
|
|
515
|
16
|
50
|
|
|
|
41
|
if ($col_index < 0) |
|
|
100
|
|
|
|
|
|
516
|
|
|
|
|
|
|
{ |
517
|
0
|
|
|
|
|
0
|
$col_index = 0; |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
elsif ($col_index > $tb->n_cols) |
520
|
|
|
|
|
|
|
{ |
521
|
2
|
|
|
|
|
5
|
$col_index = $tb->n_cols; |
522
|
|
|
|
|
|
|
} |
523
|
|
|
|
|
|
|
|
524
|
16
|
|
|
|
|
24
|
return $col_index; |
525
|
|
|
|
|
|
|
} |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
# start and width of each column |
528
|
|
|
|
|
|
|
sub colrange { |
529
|
16
|
|
|
16
|
1
|
55
|
my ( $tb, $proto_col_index) = @_; |
530
|
|
|
|
|
|
|
|
531
|
16
|
|
|
|
|
34
|
my $col_index = $tb->_normalize_col_index($proto_col_index); |
532
|
|
|
|
|
|
|
|
533
|
16
|
50
|
|
|
|
32
|
return ( 0, 0) unless $tb->width; # width called, $tb->_blank() exists now |
534
|
16
|
|
|
|
|
28
|
my @widths = map { length } @{ $tb->_blank}, ''; |
|
48
|
|
|
|
|
77
|
|
|
16
|
|
|
|
|
29
|
|
535
|
16
|
|
|
|
|
43
|
@widths = @widths[ 0 .. $col_index]; |
536
|
|
|
|
|
|
|
|
537
|
16
|
|
|
|
|
25
|
my $width = pop @widths; |
538
|
16
|
|
100
|
|
|
61
|
my $pos = sum(@widths) || 0; |
539
|
|
|
|
|
|
|
|
540
|
16
|
|
|
|
|
29
|
my $seps_aref = _recover_separators( $tb->_forms->[ 0]); |
541
|
|
|
|
|
|
|
|
542
|
16
|
|
|
|
|
33
|
my $sep_sum = 0; |
543
|
16
|
|
|
|
|
37
|
foreach my $sep (@$seps_aref[ 0 .. $col_index]) |
544
|
|
|
|
|
|
|
{ |
545
|
34
|
|
|
|
|
49
|
$sep_sum += length($sep); |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
16
|
|
|
|
|
107
|
return ( $pos+$sep_sum, $width ) ; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
## printable output |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
# whole table |
554
|
|
|
|
|
|
|
sub table { |
555
|
49
|
|
|
49
|
1
|
75
|
my $tb = shift; |
556
|
|
|
|
|
|
|
|
557
|
49
|
|
|
|
|
80
|
return $tb->_table_portion( $tb->height, 0, @_); |
558
|
|
|
|
|
|
|
} |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
# only titles |
561
|
|
|
|
|
|
|
sub title { |
562
|
14
|
|
|
14
|
1
|
54
|
my $tb = shift; |
563
|
|
|
|
|
|
|
|
564
|
14
|
|
|
|
|
47
|
return $tb->_table_portion( $tb->title_height, 0, @_); |
565
|
|
|
|
|
|
|
} |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
# only body |
568
|
|
|
|
|
|
|
sub body { |
569
|
9
|
|
|
9
|
1
|
26
|
my $tb = shift; |
570
|
|
|
|
|
|
|
|
571
|
9
|
|
|
|
|
22
|
return $tb->_table_portion( $tb->body_height, $tb->title_height, @_); |
572
|
|
|
|
|
|
|
} |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
sub stringify |
575
|
|
|
|
|
|
|
{ |
576
|
12
|
|
|
12
|
1
|
39
|
my ($tb) = @_; |
577
|
|
|
|
|
|
|
|
578
|
12
|
|
|
|
|
27
|
return (scalar ( $tb->table() )); |
579
|
|
|
|
|
|
|
} |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
### common internals |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
# common representation of table(), title() and body() |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
sub _table_portion_as_aref |
586
|
|
|
|
|
|
|
{ |
587
|
72
|
|
|
72
|
|
96
|
my $tb = shift; |
588
|
|
|
|
|
|
|
|
589
|
72
|
|
|
|
|
97
|
my $total = shift; |
590
|
72
|
|
|
|
|
90
|
my $offset = shift; |
591
|
|
|
|
|
|
|
|
592
|
72
|
|
|
|
|
129
|
my ( $from, $n) = ( 0, $total); # if no parameters |
593
|
|
|
|
|
|
|
|
594
|
72
|
100
|
|
|
|
141
|
if ( @_ ) { |
595
|
51
|
|
|
|
|
67
|
$from = shift; |
596
|
51
|
50
|
|
|
|
98
|
$n = @_ ? shift : 1; # one line if not given |
597
|
|
|
|
|
|
|
} |
598
|
|
|
|
|
|
|
|
599
|
72
|
|
|
|
|
173
|
( $from, $n) = _limit_range( $total, $from, $n); |
600
|
|
|
|
|
|
|
|
601
|
72
|
|
|
|
|
134
|
my $limit = $tb->title_height; # title format below |
602
|
72
|
|
|
|
|
103
|
$from += $offset; |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
return |
605
|
|
|
|
|
|
|
[ |
606
|
72
|
|
|
|
|
218
|
map $tb->_assemble_line( $_ >= $limit, $tb->_table_line( $_), 0), |
607
|
|
|
|
|
|
|
$from .. $from + $n - 1 |
608
|
|
|
|
|
|
|
]; |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
sub _table_portion |
612
|
|
|
|
|
|
|
{ |
613
|
72
|
|
|
72
|
|
124
|
my $tb = shift; |
614
|
|
|
|
|
|
|
|
615
|
72
|
|
|
|
|
137
|
my $lines_aref = $tb->_table_portion_as_aref(@_); |
616
|
|
|
|
|
|
|
|
617
|
72
|
100
|
|
|
|
533
|
return (wantarray ? @$lines_aref : join('', @$lines_aref)); |
618
|
|
|
|
|
|
|
} |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
sub _limit_range |
621
|
|
|
|
|
|
|
{ |
622
|
72
|
|
|
72
|
|
129
|
my ( $total, $from, $n) = @_; |
623
|
|
|
|
|
|
|
|
624
|
72
|
|
100
|
|
|
272
|
$from ||= 0; |
625
|
72
|
100
|
|
|
|
137
|
$from += $total if $from < 0; |
626
|
72
|
50
|
|
|
|
131
|
$n = $total unless defined $n; |
627
|
|
|
|
|
|
|
|
628
|
72
|
100
|
66
|
|
|
230
|
return ( 0, 0) if $from + $n < 0 or $from >= $total; |
629
|
|
|
|
|
|
|
|
630
|
69
|
50
|
|
|
|
131
|
$from = 0 if $from < 0; |
631
|
69
|
50
|
|
|
|
135
|
$n = $total - $from if $n > $total - $from; |
632
|
|
|
|
|
|
|
|
633
|
69
|
|
|
|
|
136
|
return( $from, $n); |
634
|
|
|
|
|
|
|
} |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
# get table line (formatted, including titles). fill cache if needed |
637
|
|
|
|
|
|
|
sub _table_line { |
638
|
113
|
|
|
113
|
|
183
|
my ($tb, $idx) = @_; |
639
|
|
|
|
|
|
|
|
640
|
113
|
100
|
|
|
|
176
|
if (! $tb->_lines) |
641
|
|
|
|
|
|
|
{ |
642
|
32
|
|
|
|
|
59
|
$tb->_lines([ $tb->_build_table_lines ]); |
643
|
|
|
|
|
|
|
} |
644
|
|
|
|
|
|
|
|
645
|
113
|
|
|
|
|
213
|
return $tb->_lines->[$idx]; |
646
|
|
|
|
|
|
|
} |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
# build array of lines of justified data items |
649
|
|
|
|
|
|
|
sub _build_table_lines { |
650
|
32
|
|
|
32
|
|
50
|
my $tb = shift; |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
# copy data columns, replacing undef with '' |
653
|
|
|
|
|
|
|
my @cols = |
654
|
|
|
|
|
|
|
map |
655
|
70
|
100
|
|
|
|
120
|
{ [ map { defined($_) ? $_ : '' } @$_ ] } |
|
159
|
|
|
|
|
345
|
|
656
|
32
|
|
|
|
|
50
|
@{ $tb->_cols() } ; |
|
32
|
|
|
|
|
46
|
|
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
# add set of empty strings for blank line (needed to build horizontal rules) |
659
|
32
|
|
|
|
|
80
|
foreach my $col (@cols) |
660
|
|
|
|
|
|
|
{ |
661
|
70
|
|
|
|
|
150
|
push @$col, ''; |
662
|
|
|
|
|
|
|
} |
663
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
# add samples for minimum alignment |
665
|
32
|
|
|
|
|
79
|
foreach my $col_idx (0 .. $#cols) |
666
|
|
|
|
|
|
|
{ |
667
|
70
|
|
|
|
|
84
|
push @{$cols[$col_idx]}, @{$tb->_spec->[$col_idx]->{sample}}; |
|
70
|
|
|
|
|
97
|
|
|
70
|
|
|
|
|
169
|
|
668
|
|
|
|
|
|
|
} |
669
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
# align to style |
671
|
32
|
|
|
|
|
67
|
foreach my $col_idx (0 .. $#cols) |
672
|
|
|
|
|
|
|
{ |
673
|
70
|
|
|
|
|
53508
|
align($tb->_spec->[$col_idx]->{align}, @{$cols[$col_idx]}); |
|
70
|
|
|
|
|
187
|
|
674
|
|
|
|
|
|
|
} |
675
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
# trim off samples, but leave blank line |
677
|
32
|
|
|
|
|
36089
|
foreach my $col (@cols) |
678
|
|
|
|
|
|
|
{ |
679
|
70
|
|
|
|
|
100
|
splice( @{$col}, 1 + $tb->body_height ); |
|
70
|
|
|
|
|
133
|
|
680
|
|
|
|
|
|
|
} |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
# include titles |
683
|
32
|
|
|
|
|
89
|
foreach my $col_idx (0 .. $#cols) |
684
|
|
|
|
|
|
|
{ |
685
|
70
|
|
|
|
|
92
|
unshift @{$cols[$col_idx]}, @{$tb->_titles->[$col_idx]}; |
|
70
|
|
|
|
|
96
|
|
|
70
|
|
|
|
|
111
|
|
686
|
|
|
|
|
|
|
} |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
# align title and body portions of columns |
689
|
|
|
|
|
|
|
# blank line will be there even with no data |
690
|
32
|
|
|
|
|
70
|
foreach my $col_idx (0 .. $#cols) |
691
|
|
|
|
|
|
|
{ |
692
|
70
|
|
|
|
|
23160
|
align($tb->_spec->[$col_idx]->{align_title}, @{$cols[$col_idx]}); |
|
70
|
|
|
|
|
161
|
|
693
|
|
|
|
|
|
|
} |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
# deposit a blank line, pulling it off the columns. |
696
|
|
|
|
|
|
|
# *_rule() knows this is done |
697
|
32
|
|
|
|
|
15341
|
my @blank; |
698
|
|
|
|
|
|
|
|
699
|
32
|
|
|
|
|
67
|
foreach my $col (@cols) |
700
|
|
|
|
|
|
|
{ |
701
|
70
|
|
|
|
|
144
|
push @blank, pop(@$col); |
702
|
|
|
|
|
|
|
} |
703
|
|
|
|
|
|
|
|
704
|
32
|
|
|
|
|
88
|
$tb->_blank(\@blank); |
705
|
|
|
|
|
|
|
|
706
|
32
|
|
|
|
|
64
|
return _transpose_n( $tb->height, \@cols); # bye-bye, @cols |
707
|
|
|
|
|
|
|
} |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
# destructively transpose a number of lines/cols from an array of arrayrefs |
710
|
|
|
|
|
|
|
sub _transpose_n { |
711
|
89
|
|
|
89
|
|
158
|
my ($n, $cols) = @_; |
712
|
|
|
|
|
|
|
|
713
|
89
|
|
|
|
|
161
|
return map { [ map { shift @$_ } @$cols] } 1 .. $n; |
|
151
|
|
|
|
|
217
|
|
|
401
|
|
|
|
|
830
|
|
714
|
|
|
|
|
|
|
} |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
# like _transpose_n, but find the number to transpose from max of given |
717
|
|
|
|
|
|
|
sub _transpose |
718
|
|
|
|
|
|
|
{ |
719
|
57
|
|
|
57
|
|
123
|
my ($cols) = @_; |
720
|
|
|
|
|
|
|
|
721
|
57
|
|
|
|
|
94
|
my $m = max ( map { scalar(@$_) } @$cols, []); |
|
200
|
|
|
|
|
311
|
|
722
|
|
|
|
|
|
|
|
723
|
57
|
|
|
|
|
121
|
return _transpose_n( $m, $cols); |
724
|
|
|
|
|
|
|
} |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
# make a line from a number of formatted data elements |
727
|
|
|
|
|
|
|
sub _assemble_line { |
728
|
125
|
|
|
125
|
|
254
|
my ($tb, $in_body, $line_aref, $replace_spaces) = @_; |
729
|
|
|
|
|
|
|
|
730
|
125
|
|
|
|
|
178
|
my $format = $tb->_forms->[ !!$in_body]; |
731
|
|
|
|
|
|
|
|
732
|
125
|
100
|
|
|
|
228
|
if ($replace_spaces) |
733
|
|
|
|
|
|
|
{ |
734
|
2
|
|
|
|
|
11
|
$format =~ s/\s/=/g; |
735
|
|
|
|
|
|
|
} |
736
|
|
|
|
|
|
|
|
737
|
125
|
|
|
|
|
630
|
return sprintf($format, @$line_aref) . "\n"; |
738
|
|
|
|
|
|
|
} |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
sub _text_rule |
741
|
|
|
|
|
|
|
{ |
742
|
10
|
|
|
10
|
|
22
|
my ($tb, $rule, $char, $alt) = @_; |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
# replace blanks with $char. If $alt is given, replace nonblanks |
745
|
|
|
|
|
|
|
# with $alt |
746
|
10
|
100
|
|
|
|
21
|
if ( defined $alt ) |
747
|
|
|
|
|
|
|
{ |
748
|
4
|
100
|
|
|
|
23
|
$rule =~ s/(.)/$1 eq ' ' ? $char : $alt/ge; |
|
99
|
|
|
|
|
249
|
|
749
|
|
|
|
|
|
|
} |
750
|
|
|
|
|
|
|
else |
751
|
|
|
|
|
|
|
{ |
752
|
6
|
100
|
|
|
|
17
|
$rule =~ s/ /$char/g if $char ne ' '; |
753
|
|
|
|
|
|
|
} |
754
|
|
|
|
|
|
|
|
755
|
10
|
|
|
|
|
71
|
return $rule; |
756
|
|
|
|
|
|
|
} |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
# build a rule line |
759
|
|
|
|
|
|
|
sub _rule { |
760
|
12
|
|
|
12
|
|
21
|
my $tb = shift; |
761
|
|
|
|
|
|
|
|
762
|
12
|
50
|
|
|
|
39
|
return + (!$tb->width) ? '' : $tb->_positive_width_rule(@_); |
763
|
|
|
|
|
|
|
} |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
sub _positive_width_rule |
766
|
|
|
|
|
|
|
{ |
767
|
12
|
|
|
12
|
|
36
|
my ($tb, $in_body, $char, $alt) = @_; |
768
|
|
|
|
|
|
|
|
769
|
12
|
100
|
|
|
|
29
|
my $rule = $tb->_assemble_line( $in_body, $tb->_blank, |
770
|
|
|
|
|
|
|
((ref($char) eq "CODE") ? 1 : 0), |
771
|
|
|
|
|
|
|
); |
772
|
|
|
|
|
|
|
|
773
|
12
|
|
|
|
|
40
|
return $tb->_render_rule($rule, $char, $alt); |
774
|
|
|
|
|
|
|
} |
775
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
sub _render_rule |
777
|
|
|
|
|
|
|
{ |
778
|
12
|
|
|
12
|
|
25
|
my ($tb, $rule, $char, $alt) = @_; |
779
|
|
|
|
|
|
|
|
780
|
12
|
100
|
|
|
|
38
|
if (ref($char) eq "CODE") |
781
|
|
|
|
|
|
|
{ |
782
|
2
|
|
|
|
|
10
|
return $tb->_render_rule_with_callbacks($rule, $char, $alt); |
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
else |
785
|
|
|
|
|
|
|
{ |
786
|
10
|
|
|
|
|
27
|
_default_if_empty(\$char, ' '); |
787
|
|
|
|
|
|
|
|
788
|
10
|
|
|
|
|
25
|
return $tb->_text_rule($rule, $char, $alt); |
789
|
|
|
|
|
|
|
} |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
sub _get_fixed_len_string |
793
|
|
|
|
|
|
|
{ |
794
|
14
|
|
|
14
|
|
84
|
my ($s, $len) = @_; |
795
|
|
|
|
|
|
|
|
796
|
14
|
|
|
|
|
26
|
$s = substr($s, 0, $len); |
797
|
14
|
|
|
|
|
24
|
$s .= ' ' x ($len - length($s)); |
798
|
|
|
|
|
|
|
|
799
|
14
|
|
|
|
|
56
|
return $s; |
800
|
|
|
|
|
|
|
} |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
sub _render_rule_with_callbacks |
803
|
|
|
|
|
|
|
{ |
804
|
2
|
|
|
2
|
|
22
|
my ($tb, $rule, $char, $alt) = @_; |
805
|
|
|
|
|
|
|
|
806
|
2
|
|
|
|
|
14
|
my %callbacks = |
807
|
|
|
|
|
|
|
( |
808
|
|
|
|
|
|
|
'char' => { cb => $char, idx => 0, }, |
809
|
|
|
|
|
|
|
'alt' => { cb => $alt, idx => 0, }, |
810
|
|
|
|
|
|
|
); |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
my $calc_substitution = sub { |
813
|
14
|
|
|
14
|
|
29
|
my $s = shift; |
814
|
|
|
|
|
|
|
|
815
|
14
|
|
|
|
|
19
|
my $len = length($s); |
816
|
|
|
|
|
|
|
|
817
|
14
|
100
|
|
|
|
91
|
my $which = (($s =~ /\A /) ? 'char' : 'alt'); |
818
|
14
|
|
|
|
|
26
|
my $rec = $callbacks{$which}; |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
return _get_fixed_len_string( |
821
|
14
|
|
|
|
|
32
|
scalar ($rec->{cb}->($rec->{idx}++, $len)), |
822
|
|
|
|
|
|
|
$len, |
823
|
|
|
|
|
|
|
); |
824
|
2
|
|
|
|
|
10
|
}; |
825
|
|
|
|
|
|
|
|
826
|
2
|
|
|
|
|
29
|
$rule =~ s/((.)\2*)/$calc_substitution->($1)/ge; |
|
14
|
|
|
|
|
25
|
|
827
|
|
|
|
|
|
|
|
828
|
2
|
|
|
|
|
21
|
return $rule; |
829
|
|
|
|
|
|
|
} |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
sub rule { |
832
|
11
|
|
|
11
|
1
|
66
|
my $tb = shift; |
833
|
11
|
|
|
|
|
43
|
return $tb->_rule( 0, @_); |
834
|
|
|
|
|
|
|
} |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
sub body_rule { |
837
|
1
|
|
|
1
|
1
|
3
|
my $tb = shift; |
838
|
1
|
|
|
|
|
4
|
return $tb->_rule( 1, @_); |
839
|
|
|
|
|
|
|
} |
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
### warning behavior |
842
|
6
|
|
|
6
|
|
51
|
use Carp; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
2279
|
|
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
{ |
845
|
|
|
|
|
|
|
my ( $warn, $fatal) = ( 0, 0); |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
sub warnings |
848
|
|
|
|
|
|
|
{ |
849
|
|
|
|
|
|
|
# Ignore the class/object. |
850
|
0
|
|
|
0
|
1
|
|
my (undef, $toggle) = @_; |
851
|
|
|
|
|
|
|
|
852
|
0
|
|
0
|
|
|
|
$toggle ||= 'on'; |
853
|
0
|
0
|
|
|
|
|
if ( $toggle eq 'off' ) |
|
|
0
|
|
|
|
|
|
854
|
|
|
|
|
|
|
{ |
855
|
0
|
|
|
|
|
|
($warn, $fatal) = (0, 0); |
856
|
|
|
|
|
|
|
} |
857
|
|
|
|
|
|
|
elsif ( $toggle eq 'fatal' ) |
858
|
|
|
|
|
|
|
{ |
859
|
0
|
|
|
|
|
|
($warn, $fatal) = (1, 1); |
860
|
|
|
|
|
|
|
} |
861
|
|
|
|
|
|
|
else |
862
|
|
|
|
|
|
|
{ |
863
|
0
|
|
|
|
|
|
($warn, $fatal) = (1, 0); |
864
|
|
|
|
|
|
|
} |
865
|
0
|
0
|
|
|
|
|
return $fatal ? 'fatal' : $warn ? 'on' : 'off'; |
|
|
0
|
|
|
|
|
|
866
|
|
|
|
|
|
|
} |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
sub _warn |
869
|
|
|
|
|
|
|
{ |
870
|
0
|
|
|
0
|
|
|
my $msg = shift; |
871
|
|
|
|
|
|
|
|
872
|
0
|
0
|
|
|
|
|
return unless $warn; |
873
|
|
|
|
|
|
|
|
874
|
0
|
0
|
|
|
|
|
if ($fatal) |
875
|
|
|
|
|
|
|
{ |
876
|
0
|
|
|
|
|
|
croak( $msg) |
877
|
|
|
|
|
|
|
} |
878
|
|
|
|
|
|
|
|
879
|
0
|
|
|
|
|
|
carp( $msg); |
880
|
|
|
|
|
|
|
|
881
|
0
|
|
|
|
|
|
return; |
882
|
|
|
|
|
|
|
} |
883
|
|
|
|
|
|
|
} |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
=pod |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=encoding utf-8 |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
=head1 NAME |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
Text::Table - Organize Data in Tables |
892
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
=head1 VERSION |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
version 1.134 |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=head1 SYNOPSIS |
898
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
use Text::Table; |
900
|
|
|
|
|
|
|
my $tb = Text::Table->new( |
901
|
|
|
|
|
|
|
"Planet", "Radius\nkm", "Density\ng/cm^3" |
902
|
|
|
|
|
|
|
); |
903
|
|
|
|
|
|
|
$tb->load( |
904
|
|
|
|
|
|
|
[ "Mercury", 2360, 3.7 ], |
905
|
|
|
|
|
|
|
[ "Venus", 6110, 5.1 ], |
906
|
|
|
|
|
|
|
[ "Earth", 6378, 5.52 ], |
907
|
|
|
|
|
|
|
[ "Jupiter", 71030, 1.3 ], |
908
|
|
|
|
|
|
|
); |
909
|
|
|
|
|
|
|
print $tb; |
910
|
|
|
|
|
|
|
|
911
|
|
|
|
|
|
|
This prints a table from the given title and data like this: |
912
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
Planet Radius Density |
914
|
|
|
|
|
|
|
km g/cm^3 |
915
|
|
|
|
|
|
|
Mercury 2360 3.7 |
916
|
|
|
|
|
|
|
Venus 6110 5.1 |
917
|
|
|
|
|
|
|
Earth 6378 5.52 |
918
|
|
|
|
|
|
|
Jupiter 71030 1.3 |
919
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
Note that two-line titles work, and that the planet names are aligned |
921
|
|
|
|
|
|
|
differently than the numbers. |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
=head1 DESCRIPTION |
924
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
Organization of data in table form is a time-honored and useful |
926
|
|
|
|
|
|
|
method of data representation. While columns of data are trivially |
927
|
|
|
|
|
|
|
generated by computer through formatted output, even simple tasks |
928
|
|
|
|
|
|
|
like keeping titles aligned with the data columns are not trivial, |
929
|
|
|
|
|
|
|
and the one-shot solutions one comes up with tend to be particularly |
930
|
|
|
|
|
|
|
hard to maintain. Text::Table allows you to create and maintain |
931
|
|
|
|
|
|
|
tables that adapt to alignment requirements as you use them. |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
=head2 Overview |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
The process is simple: you create a table (a Text::Table object) by |
936
|
|
|
|
|
|
|
describing the columns the table is going to have. Then you load |
937
|
|
|
|
|
|
|
lines of data into the table, and finally print the resulting output |
938
|
|
|
|
|
|
|
lines. Alignment of data and column titles is handled dynamically |
939
|
|
|
|
|
|
|
in dependence on the data present. |
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
=head2 Table Creation |
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
In the simplest case, if all you want is a number of (untitled) columns, |
944
|
|
|
|
|
|
|
you create an unspecified table and start adding data to it. The number |
945
|
|
|
|
|
|
|
of columns is taken from the first line of data. |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
To specify a table you specify its columns. A column description |
948
|
|
|
|
|
|
|
can contain a title and alignment requirements for the data, both |
949
|
|
|
|
|
|
|
optional. Additionally, you can specify how the title is aligned with |
950
|
|
|
|
|
|
|
the body of a column, and how the lines of a multiline title are |
951
|
|
|
|
|
|
|
aligned among themselves. |
952
|
|
|
|
|
|
|
|
953
|
|
|
|
|
|
|
The columns are collected in the table in the |
954
|
|
|
|
|
|
|
order they are given. On data entry, each column corresponds to |
955
|
|
|
|
|
|
|
one data item, and in column selection columns are indexed left to |
956
|
|
|
|
|
|
|
right, starting from 0. |
957
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
Each title can be a multiline string which will be blank-filled to |
959
|
|
|
|
|
|
|
the length of the longest partial line. The largest number of title |
960
|
|
|
|
|
|
|
lines in a column determines how many title lines the table has as a |
961
|
|
|
|
|
|
|
whole, including the case that no column has any titles. |
962
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
On output, columns are separated by a single blank. You can control |
964
|
|
|
|
|
|
|
what goes between columns by specifying separators between (or before, |
965
|
|
|
|
|
|
|
or after) columns. Separators don't contain any data and don't count |
966
|
|
|
|
|
|
|
in column indexing. They also don't accumulate: in a sequence of only |
967
|
|
|
|
|
|
|
separators and no columns, only the last one counts. |
968
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
=head2 Status Information |
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
The width (in characters), height (in lines), number of columns, and |
972
|
|
|
|
|
|
|
similar data about the table is available. |
973
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
=head2 Data Loading |
975
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
Table data is entered line-wise, each time specifying data entries |
977
|
|
|
|
|
|
|
for all table columns. A bulk loader for many lines at once is also |
978
|
|
|
|
|
|
|
available. You can clear the data from the table for re-use (though |
979
|
|
|
|
|
|
|
you will more likely just create another table). |
980
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
Data can contain colorizing escape sequences (as provided by |
982
|
|
|
|
|
|
|
C) without upsetting the alignment. |
983
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
=head2 Table Output |
985
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
The output area of a table is divided in the title and the body. |
987
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
The title contains the combined titles from the table columns, if |
989
|
|
|
|
|
|
|
any. Its content never changes with a given table, but it may be |
990
|
|
|
|
|
|
|
spread out differently on the page through alignment with the data. |
991
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
The body contains the data lines, aligned column-wise as specified, |
993
|
|
|
|
|
|
|
and left-aligned with the column title. |
994
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
Each of these is arranged like a Perl array (counting from 0) and can |
996
|
|
|
|
|
|
|
be accessed in portions by specifying a first line and the number |
997
|
|
|
|
|
|
|
of following lines. Also like an array, giving a negative first line |
998
|
|
|
|
|
|
|
counts from the end of the area. The whole table, the title followed |
999
|
|
|
|
|
|
|
by the body, can also be accessed in this manner. |
1000
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
The subdivisions are there so you can repeat the title (or parts of |
1002
|
|
|
|
|
|
|
it) along with parts of the body on output, whether for screen paging |
1003
|
|
|
|
|
|
|
or printout. |
1004
|
|
|
|
|
|
|
|
1005
|
|
|
|
|
|
|
A rule line is also available, which is the horizontal counterpart to |
1006
|
|
|
|
|
|
|
the separator columns you specify with the table. |
1007
|
|
|
|
|
|
|
It is basically a table line as it would appear if all data entries |
1008
|
|
|
|
|
|
|
in the line were empty, that is, a blank line except for where the |
1009
|
|
|
|
|
|
|
column separators have non-blank entries. If you print it between |
1010
|
|
|
|
|
|
|
data lines, it will not disrupt the vertical separator structure |
1011
|
|
|
|
|
|
|
as a plain blank line would. You can also request a solid rule |
1012
|
|
|
|
|
|
|
consisting of any character, and even one with the non-blank column |
1013
|
|
|
|
|
|
|
separators replaced by a character of your choice. This way you can |
1014
|
|
|
|
|
|
|
get the popular representation of line-crossings like so: |
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
| |
1017
|
|
|
|
|
|
|
----+--- |
1018
|
|
|
|
|
|
|
| |
1019
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
=head2 Warning Control |
1021
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
On table creation, some parameters are checked and warnings issued |
1023
|
|
|
|
|
|
|
if you allow warnings. You can also turn warnings into fatal errors. |
1024
|
|
|
|
|
|
|
|
1025
|
|
|
|
|
|
|
=head1 VERSION |
1026
|
|
|
|
|
|
|
|
1027
|
|
|
|
|
|
|
version 1.134 |
1028
|
|
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
=head1 SPECIFICATIONS |
1030
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
=head2 Column Specification |
1032
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
Each column specification is a single scalar. Columns can be either proper |
1034
|
|
|
|
|
|
|
data columns or column separators. Both can be specified either as |
1035
|
|
|
|
|
|
|
(possibly multi-line) strings, or in a more explicit form as hash-refs. |
1036
|
|
|
|
|
|
|
In the string form, proper columns are given as plain strings, and |
1037
|
|
|
|
|
|
|
separators are given as scalar references to strings. In hash form, |
1038
|
|
|
|
|
|
|
separators have a true value in the field C while proper columns |
1039
|
|
|
|
|
|
|
don't have this field. |
1040
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
=over 4 |
1042
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
=item Columns as strings |
1044
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
A column is given as a column title (any number of lines), |
1046
|
|
|
|
|
|
|
optionally followed by alignment requirements. Alignment requirements |
1047
|
|
|
|
|
|
|
start with a line that begins with an ampersand "&". However, only the |
1048
|
|
|
|
|
|
|
last such line counts as such, so if you have title lines that begin |
1049
|
|
|
|
|
|
|
with "&", just append an ampersand on a line by itself as a dummy |
1050
|
|
|
|
|
|
|
alignment section if you don't have one anyway. |
1051
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
What follows the ampersand on its line is the alignment style (like |
1053
|
|
|
|
|
|
|
I, I, ... as described in L<"Alignment">), you want for |
1054
|
|
|
|
|
|
|
the data in this column. If nothing follows, the general default I |
1055
|
|
|
|
|
|
|
is used. If you specify an invalid alignment style, it falls back to |
1056
|
|
|
|
|
|
|
left alignment. |
1057
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
The lines that follow can contain sample data for this column. These |
1059
|
|
|
|
|
|
|
are considered for alignment in the column, but never actually appear |
1060
|
|
|
|
|
|
|
in the output. The effect is to guarantee a minimum width for the |
1061
|
|
|
|
|
|
|
column even if the current data doesn't require it. This helps dampen |
1062
|
|
|
|
|
|
|
the oscillations in the appearance of dynamically aligned tables. |
1063
|
|
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
=item Columns as Hashes |
1065
|
|
|
|
|
|
|
|
1066
|
|
|
|
|
|
|
The format is |
1067
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
{ |
1069
|
|
|
|
|
|
|
title => $title, |
1070
|
|
|
|
|
|
|
align => $align, |
1071
|
|
|
|
|
|
|
sample => $sample, |
1072
|
|
|
|
|
|
|
align_title => $align_title, |
1073
|
|
|
|
|
|
|
align_title_lines => $align_title_lines, |
1074
|
|
|
|
|
|
|
} |
1075
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
$title contains the title lines and $sample the sample data. Both can |
1077
|
|
|
|
|
|
|
be given as a string or as an array-ref to the list of lines. $align contains |
1078
|
|
|
|
|
|
|
the alignment style (without a leading ampersand), usually as a string. |
1079
|
|
|
|
|
|
|
You can also give a regular expression here, which specifies regex alignment. |
1080
|
|
|
|
|
|
|
A regex can only be specified in the hash form of a column specification. |
1081
|
|
|
|
|
|
|
|
1082
|
|
|
|
|
|
|
In hash form you can also specify how the title of a column is aligned |
1083
|
|
|
|
|
|
|
with its body. To do this, you specify the keyword C with |
1084
|
|
|
|
|
|
|
C, C or C. Other alignment specifications are not |
1085
|
|
|
|
|
|
|
valid here. The default is C. |
1086
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
C also specifies how the lines of a multiline title are |
1088
|
|
|
|
|
|
|
aligned among themselves. If you want a different alignment, you |
1089
|
|
|
|
|
|
|
can specify it with the key C. Again, only C, |
1090
|
|
|
|
|
|
|
C or C are allowed. |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
Do not put other keys than those mentioned above (I, I, |
1093
|
|
|
|
|
|
|
I, I, and I) into a hash that |
1094
|
|
|
|
|
|
|
specifies a column. Most would be ignored, but some would confuse the |
1095
|
|
|
|
|
|
|
interpreter (in particular, I has to be avoided). |
1096
|
|
|
|
|
|
|
|
1097
|
|
|
|
|
|
|
=item Separators as strings |
1098
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
A separator must be given as a reference to a string (often a literal, |
1100
|
|
|
|
|
|
|
like C<\' | '>), any string that is given directly describes a column. |
1101
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
It is usually just a (short) string that will be printed between |
1103
|
|
|
|
|
|
|
table columns on all table lines instead of the default single |
1104
|
|
|
|
|
|
|
blank. If you specify two separators (on two lines), the first one |
1105
|
|
|
|
|
|
|
will be used in the title and the other in the body of the table. |
1106
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
=item Separators as Hashes |
1108
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
The hash representation of a separator has the format |
1110
|
|
|
|
|
|
|
|
1111
|
|
|
|
|
|
|
{ |
1112
|
|
|
|
|
|
|
is_sep => 1, |
1113
|
|
|
|
|
|
|
title => $title, |
1114
|
|
|
|
|
|
|
body => $body, |
1115
|
|
|
|
|
|
|
} |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
$title is the separator to be used in the title area and $body |
1118
|
|
|
|
|
|
|
the one for the body. If only one is given, it will be used for |
1119
|
|
|
|
|
|
|
both. If none is given, a blank is used. If one is shorter than |
1120
|
|
|
|
|
|
|
the other, it is blank filled on the right. |
1121
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
The value of C must be set to a true value, this is the |
1123
|
|
|
|
|
|
|
distinguishing feature of a separator. |
1124
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
=back |
1126
|
|
|
|
|
|
|
|
1127
|
|
|
|
|
|
|
=head2 Alignment |
1128
|
|
|
|
|
|
|
|
1129
|
|
|
|
|
|
|
The original documentation to L contains all the details |
1130
|
|
|
|
|
|
|
on alignment specification, but here is the rundown: |
1131
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
The possible alignment specifications are I, I, I, |
1133
|
|
|
|
|
|
|
I and I (which are synonyms), and I. The first |
1134
|
|
|
|
|
|
|
three explain themselves. |
1135
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
I (and I) align the decimal point in the data, which is |
1137
|
|
|
|
|
|
|
assumed to the right if none is present. Strings that aren't |
1138
|
|
|
|
|
|
|
numbers are treated the same way, that is, they appear aligned |
1139
|
|
|
|
|
|
|
with the integers unless they contain a ".". Instead of the |
1140
|
|
|
|
|
|
|
decimal point ".", you can also specify any other string in |
1141
|
|
|
|
|
|
|
the form I, for instance. The string in parentheses |
1142
|
|
|
|
|
|
|
is aligned in the data. The synonym I for I may be |
1143
|
|
|
|
|
|
|
more appropriate in contexts that deal with arbitrary |
1144
|
|
|
|
|
|
|
strings, as in I)> (which might be used to align certain |
1145
|
|
|
|
|
|
|
bits of Perl code). |
1146
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
I is a more sophisticated form of point alignment. |
1148
|
|
|
|
|
|
|
If you specify a regular expression, as delivered by C, the start |
1149
|
|
|
|
|
|
|
of the match is used as the alignment point. If the regex contains |
1150
|
|
|
|
|
|
|
capturing parentheses, the last submatch counts. [The usefulness of |
1151
|
|
|
|
|
|
|
this feature is under consideration.] |
1152
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
I alignment combines numeric alignment with left alignment. |
1154
|
|
|
|
|
|
|
Data items that look like numbers, and those that don't, form two |
1155
|
|
|
|
|
|
|
virtual columns and are aligned accordingly: C for numbers and |
1156
|
|
|
|
|
|
|
C for other strings. These columns are left-aligned with |
1157
|
|
|
|
|
|
|
each other (i.e. the narrower one is blank-filled) to form the |
1158
|
|
|
|
|
|
|
final alignment. |
1159
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
This way, a column that happens to have only numbers in the data gets |
1161
|
|
|
|
|
|
|
I alignment, a column with no numbers appears I-aligned, |
1162
|
|
|
|
|
|
|
and mixed data is presented in a reasonable way. |
1163
|
|
|
|
|
|
|
|
1164
|
|
|
|
|
|
|
=head2 Column Selection |
1165
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
Besides creating tables from scratch, they can be created by |
1167
|
|
|
|
|
|
|
selecting columns from an existing table. Tables created this |
1168
|
|
|
|
|
|
|
way contain the data from the columns they were built from. |
1169
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
This is done by specifying the columns to select by their index |
1171
|
|
|
|
|
|
|
(where negative indices count backward from the last column). |
1172
|
|
|
|
|
|
|
The same column can be selected more than once and the sequence |
1173
|
|
|
|
|
|
|
of columns can be arbitrarily changed. Separators don't travel |
1174
|
|
|
|
|
|
|
with columns, but can be specified between the columns at selection |
1175
|
|
|
|
|
|
|
time. |
1176
|
|
|
|
|
|
|
|
1177
|
|
|
|
|
|
|
You can make the selection of one or more columns dependent on |
1178
|
|
|
|
|
|
|
the data content of one of them. If you specify some of the columns |
1179
|
|
|
|
|
|
|
in angle brackets [...], the whole group is only included in the |
1180
|
|
|
|
|
|
|
selection if the first column in the group contains any data that |
1181
|
|
|
|
|
|
|
evaluates to boolean true. That way you can de-select parts of a |
1182
|
|
|
|
|
|
|
table if it contains no interesting data. Any column separators |
1183
|
|
|
|
|
|
|
given in brackets are selected or deselected along with the rest |
1184
|
|
|
|
|
|
|
of it. |
1185
|
|
|
|
|
|
|
|
1186
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
1187
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
=head2 Table Creation |
1189
|
|
|
|
|
|
|
|
1190
|
|
|
|
|
|
|
=over 4 |
1191
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
=item new() |
1193
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
my $tb = Text::Table->new( $column, ... ); |
1195
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
creates a table with the columns specified. A column can be proper column |
1197
|
|
|
|
|
|
|
which contains and displays data, or a separator which tells how to fill |
1198
|
|
|
|
|
|
|
the space between columns. The format of the parameters is described under |
1199
|
|
|
|
|
|
|
L<"Column Specification">. Specifying an invalid alignment for a column |
1200
|
|
|
|
|
|
|
results in a warning if these are allowed. |
1201
|
|
|
|
|
|
|
|
1202
|
|
|
|
|
|
|
If no columns are specified, the number of columns is taken from the first |
1203
|
|
|
|
|
|
|
line of data added to the table. The effect is as if you had specified |
1204
|
|
|
|
|
|
|
Cnew( ( '') x $n)>, where C<$n> is the number of |
1205
|
|
|
|
|
|
|
columns. |
1206
|
|
|
|
|
|
|
|
1207
|
|
|
|
|
|
|
=item select() |
1208
|
|
|
|
|
|
|
|
1209
|
|
|
|
|
|
|
my $sub = $tb->select( $column, ...); |
1210
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
creates a table from the listed columns of the table $tb, including |
1212
|
|
|
|
|
|
|
the data. Columns are specified as integer indices which refer to |
1213
|
|
|
|
|
|
|
the data columns of $tb. Columns can be repeated and specified in any |
1214
|
|
|
|
|
|
|
order. Negative indices count from the last column. If an invalid |
1215
|
|
|
|
|
|
|
index is specified, a warning is issued, if allowed. |
1216
|
|
|
|
|
|
|
|
1217
|
|
|
|
|
|
|
As with L<"new()">, separators can be interspersed among the column |
1218
|
|
|
|
|
|
|
indices and will be used between the columns of the new table. |
1219
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
If you enclose some of the arguments (column indices or separators) in |
1221
|
|
|
|
|
|
|
angle brackets C<[...]> (technically, you specify them inside an |
1222
|
|
|
|
|
|
|
arrayref), they form a group for conditional selection. The group is |
1223
|
|
|
|
|
|
|
only included in the resulting table if the first actual column inside |
1224
|
|
|
|
|
|
|
the group contains any data that evaluate to a boolean true. This way |
1225
|
|
|
|
|
|
|
you can exclude groups of columns that wouldn't contribute anything |
1226
|
|
|
|
|
|
|
interesting. Note that separators are selected and de-selected with |
1227
|
|
|
|
|
|
|
their group. That way, more than one separator can appear between |
1228
|
|
|
|
|
|
|
adjacent columns. They don't add up, but only the rightmost separator |
1229
|
|
|
|
|
|
|
is used. A group that contains only separators is never selected. |
1230
|
|
|
|
|
|
|
[Another feature whose usefulness is under consideration.] |
1231
|
|
|
|
|
|
|
|
1232
|
|
|
|
|
|
|
=back |
1233
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
=head2 Status Information |
1235
|
|
|
|
|
|
|
|
1236
|
|
|
|
|
|
|
=over 4 |
1237
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
=item n_cols() |
1239
|
|
|
|
|
|
|
|
1240
|
|
|
|
|
|
|
$tb->n_cols |
1241
|
|
|
|
|
|
|
|
1242
|
|
|
|
|
|
|
returns the number of columns in the table. |
1243
|
|
|
|
|
|
|
|
1244
|
|
|
|
|
|
|
=item width() |
1245
|
|
|
|
|
|
|
|
1246
|
|
|
|
|
|
|
$tb->width |
1247
|
|
|
|
|
|
|
|
1248
|
|
|
|
|
|
|
returns the width (in characters) of the table. All table lines have |
1249
|
|
|
|
|
|
|
this length (not counting a final "\n" in the line), as well as the |
1250
|
|
|
|
|
|
|
separator lines returned by $tb->rule() and $b->body_rule(). |
1251
|
|
|
|
|
|
|
The width of a table can potentially be influenced by any data item |
1252
|
|
|
|
|
|
|
in it. |
1253
|
|
|
|
|
|
|
|
1254
|
|
|
|
|
|
|
=item height() |
1255
|
|
|
|
|
|
|
|
1256
|
|
|
|
|
|
|
$tb->height |
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
returns the total number of lines in a table, including title lines |
1259
|
|
|
|
|
|
|
and body lines. For orthogonality, the synonym table_height() also |
1260
|
|
|
|
|
|
|
exists. |
1261
|
|
|
|
|
|
|
|
1262
|
|
|
|
|
|
|
=item table_height() |
1263
|
|
|
|
|
|
|
|
1264
|
|
|
|
|
|
|
Same as C<< $table->height() >>. |
1265
|
|
|
|
|
|
|
|
1266
|
|
|
|
|
|
|
=item title_height() |
1267
|
|
|
|
|
|
|
|
1268
|
|
|
|
|
|
|
$tb->title_height |
1269
|
|
|
|
|
|
|
|
1270
|
|
|
|
|
|
|
returns the number of title lines in a table. |
1271
|
|
|
|
|
|
|
|
1272
|
|
|
|
|
|
|
=item body_height() |
1273
|
|
|
|
|
|
|
|
1274
|
|
|
|
|
|
|
$tb->body_height |
1275
|
|
|
|
|
|
|
|
1276
|
|
|
|
|
|
|
returns the number of lines in the table body. |
1277
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
=item colrange() |
1279
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
$tb->colrange( $i) |
1281
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
returns the start position and width of the $i-th column (counting from 0) |
1283
|
|
|
|
|
|
|
of the table. If $i is negative, counts from the end of the table. If $i |
1284
|
|
|
|
|
|
|
is larger than the greatest column index, an imaginary column of width 0 |
1285
|
|
|
|
|
|
|
is assumed right of the table. |
1286
|
|
|
|
|
|
|
|
1287
|
|
|
|
|
|
|
=back |
1288
|
|
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
=head2 Data Loading |
1290
|
|
|
|
|
|
|
|
1291
|
|
|
|
|
|
|
=over 4 |
1292
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
=item add() |
1294
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
$tb->add( $col1, ..., $colN) |
1296
|
|
|
|
|
|
|
|
1297
|
|
|
|
|
|
|
adds a data line to the table, returns the table. |
1298
|
|
|
|
|
|
|
|
1299
|
|
|
|
|
|
|
C<$col1>, ..., C<$colN> are scalars that |
1300
|
|
|
|
|
|
|
correspond to the table columns. Undefined entries are converted to '', |
1301
|
|
|
|
|
|
|
and extra data beyond the number of table columns is ignored. |
1302
|
|
|
|
|
|
|
|
1303
|
|
|
|
|
|
|
Data entries can be multi-line strings. The partial strings all go into |
1304
|
|
|
|
|
|
|
the same column. The corresponding fields of other columns remain empty |
1305
|
|
|
|
|
|
|
unless there is another multi-line entry in that column that fills the |
1306
|
|
|
|
|
|
|
fields. Adding a line with multi-line entries is equivalent to adding |
1307
|
|
|
|
|
|
|
multiple lines. |
1308
|
|
|
|
|
|
|
|
1309
|
|
|
|
|
|
|
Every call to C increases the body height of the table by the |
1310
|
|
|
|
|
|
|
number of effective lines, one in the absence of multiline entries. |
1311
|
|
|
|
|
|
|
|
1312
|
|
|
|
|
|
|
=item load() |
1313
|
|
|
|
|
|
|
|
1314
|
|
|
|
|
|
|
$tb->load( $line, ...) |
1315
|
|
|
|
|
|
|
|
1316
|
|
|
|
|
|
|
loads the data lines given into the table, returns the table. |
1317
|
|
|
|
|
|
|
|
1318
|
|
|
|
|
|
|
Every argument to C represents a data line to be added to the |
1319
|
|
|
|
|
|
|
table. The line can be given as an array(ref) containing the data |
1320
|
|
|
|
|
|
|
items, or as a string, which is split on whitespace to retrieve the |
1321
|
|
|
|
|
|
|
data. If an undefined argument is given, it is treated as an |
1322
|
|
|
|
|
|
|
empty line. |
1323
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
=item clear() |
1325
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
$tb->clear; |
1327
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
deletes all data from the table and resets it to the state after |
1329
|
|
|
|
|
|
|
creation. Returns the table. The body height of a table is 0 after |
1330
|
|
|
|
|
|
|
C. |
1331
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
=back |
1333
|
|
|
|
|
|
|
|
1334
|
|
|
|
|
|
|
=head2 Table Output |
1335
|
|
|
|
|
|
|
|
1336
|
|
|
|
|
|
|
The three methods C, C, and C are very similar.
1337
|
|
|
|
|
|
|
They access different parts of the printable output lines of a table with |
1338
|
|
|
|
|
|
|
similar methods. The details are described with the C method.
1339
|
|
|
|
|
|
|
|
1340
|
|
|
|
|
|
|
=over 4 |
1341
|
|
|
|
|
|
|
|
1342
|
|
|
|
|
|
|
=item table() |
1343
|
|
|
|
|
|
|
|
1344
|
|
|
|
|
|
|
The C method returns lines from the entire table, starting
1345
|
|
|
|
|
|
|
with the first title line and ending with the last body line. |
1346
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
In array context, the lines are returned separately, in scalar context |
1348
|
|
|
|
|
|
|
they are joined together in a single string. |
1349
|
|
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
my @lines = $tb->table; |
1351
|
|
|
|
|
|
|
my $line = $tb->table( $line_number); |
1352
|
|
|
|
|
|
|
my @lines = $tb->table( $line_number, $n); |
1353
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
The first call returns all the lines in the table. The second call |
1355
|
|
|
|
|
|
|
returns one line given by $line_number. The third call returns $n |
1356
|
|
|
|
|
|
|
lines, starting with $line_number. If $line_number is negative, it |
1357
|
|
|
|
|
|
|
counts from the end of the array. Unlike the C |
1358
|
|
|
|
|
|
|
C (and its sister methods C and C) is
1359
|
|
|
|
|
|
|
protected against large negative line numbers, it truncates the |
1360
|
|
|
|
|
|
|
range described by $line_number and $n to the existing lines. If |
1361
|
|
|
|
|
|
|
$n is 0 or negative, no lines are returned (an empty string in scalar |
1362
|
|
|
|
|
|
|
context). |
1363
|
|
|
|
|
|
|
|
1364
|
|
|
|
|
|
|
=item stringify() |
1365
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
Returns a string representation of the table. This method is called for |
1367
|
|
|
|
|
|
|
stringification by overload. |
1368
|
|
|
|
|
|
|
|
1369
|
|
|
|
|
|
|
my @table_strings = map { $_->stringify() } @tables; |
1370
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
=item title() |
1372
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
Returns lines from the title area of a table, where the column titles |
1374
|
|
|
|
|
|
|
are rendered. Parameters and response to context are as with C,
1375
|
|
|
|
|
|
|
but no lines are returned from outside the title area. |
1376
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
=item body() |
1378
|
|
|
|
|
|
|
|
1379
|
|
|
|
|
|
|
Returns lines from the body area of a table, that is the part where |
1380
|
|
|
|
|
|
|
the data content is rendered, so that $tb->body( 0) is the first data |
1381
|
|
|
|
|
|
|
line. Parameters and response to context are as with C.
1382
|
|
|
|
|
|
|
|
1383
|
|
|
|
|
|
|
=item rule() |
1384
|
|
|
|
|
|
|
|
1385
|
|
|
|
|
|
|
$tb->rule; |
1386
|
|
|
|
|
|
|
$tb->rule( $char); |
1387
|
|
|
|
|
|
|
$tb->rule( $char, $char1); |
1388
|
|
|
|
|
|
|
$tb->rule( sub { my ($index, $len) = @_; }, |
1389
|
|
|
|
|
|
|
sub { my ($index, $len) = @_; }, |
1390
|
|
|
|
|
|
|
); |
1391
|
|
|
|
|
|
|
|
1392
|
|
|
|
|
|
|
Returns a rule for the table. |
1393
|
|
|
|
|
|
|
|
1394
|
|
|
|
|
|
|
A rule is a line of table width that can be used between table lines |
1395
|
|
|
|
|
|
|
to provide visual horizontal divisions, much like column separators |
1396
|
|
|
|
|
|
|
provide vertical visual divisions. In its basic form (returned by the |
1397
|
|
|
|
|
|
|
first call) it looks like a table line with no data, hence a blank |
1398
|
|
|
|
|
|
|
line except for the non-blank parts of any column-separators. If |
1399
|
|
|
|
|
|
|
one character is specified (the second call), it replaces the blanks |
1400
|
|
|
|
|
|
|
in the first form, but non-blank column separators are retained. If |
1401
|
|
|
|
|
|
|
a second character is specified, it replaces the non-blank parts of |
1402
|
|
|
|
|
|
|
the separators. So specifying the same character twice gives a solid |
1403
|
|
|
|
|
|
|
line of table width. Another useful combo is C<$tb-Erule( '-', '+')>, |
1404
|
|
|
|
|
|
|
together with separators that contain a single nonblank "|", for a |
1405
|
|
|
|
|
|
|
popular representation of line crossings. |
1406
|
|
|
|
|
|
|
|
1407
|
|
|
|
|
|
|
C uses the column separators for the title section if there |
1408
|
|
|
|
|
|
|
is a difference. |
1409
|
|
|
|
|
|
|
|
1410
|
|
|
|
|
|
|
If callbacks are specified instead of the characters, then they receive the |
1411
|
|
|
|
|
|
|
index of the section of the rule they need to render and its desired length in |
1412
|
|
|
|
|
|
|
characters, and should return the string to put there. The indexes given |
1413
|
|
|
|
|
|
|
are 0 based (where 0 is either the left column separator or the leftmost |
1414
|
|
|
|
|
|
|
cell) and the strings will be trimmed or extended in the replacement. |
1415
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
=item body_rule() |
1417
|
|
|
|
|
|
|
|
1418
|
|
|
|
|
|
|
C works like , except the rule is generated using |
1419
|
|
|
|
|
|
|
the column separators for the table body. |
1420
|
|
|
|
|
|
|
|
1421
|
|
|
|
|
|
|
=back |
1422
|
|
|
|
|
|
|
|
1423
|
|
|
|
|
|
|
=head2 Warning Control |
1424
|
|
|
|
|
|
|
|
1425
|
|
|
|
|
|
|
=over 4 |
1426
|
|
|
|
|
|
|
|
1427
|
|
|
|
|
|
|
=item warnings() |
1428
|
|
|
|
|
|
|
|
1429
|
|
|
|
|
|
|
Text::Table->warnings(); |
1430
|
|
|
|
|
|
|
Text::Table->warnings( 'on'); |
1431
|
|
|
|
|
|
|
Text::Table->warnings( 'off'): |
1432
|
|
|
|
|
|
|
Text::Table->warnings( 'fatal'): |
1433
|
|
|
|
|
|
|
|
1434
|
|
|
|
|
|
|
The C method is used to control the appearance of warning |
1435
|
|
|
|
|
|
|
messages while tables are manipulated. When Text::Table starts, warnings |
1436
|
|
|
|
|
|
|
are disabled. The default action of C is to turn warnings |
1437
|
|
|
|
|
|
|
on. The other possible arguments are self-explanatory. C |
1438
|
|
|
|
|
|
|
can also be called as an object method (C<$tb-Ewarnings( ...)>). |
1439
|
|
|
|
|
|
|
|
1440
|
|
|
|
|
|
|
=back |
1441
|
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
=head1 VERSION |
1443
|
|
|
|
|
|
|
|
1444
|
|
|
|
|
|
|
This document pertains to Text::Table version 1.127 |
1445
|
|
|
|
|
|
|
|
1446
|
|
|
|
|
|
|
=head1 BUGS |
1447
|
|
|
|
|
|
|
|
1448
|
|
|
|
|
|
|
=over 4 |
1449
|
|
|
|
|
|
|
|
1450
|
|
|
|
|
|
|
=item o |
1451
|
|
|
|
|
|
|
|
1452
|
|
|
|
|
|
|
I alignment doesn't support alternative characters for the decimal |
1453
|
|
|
|
|
|
|
point. This is actually a bug in the underlying Text::Aligner by the |
1454
|
|
|
|
|
|
|
same author. |
1455
|
|
|
|
|
|
|
|
1456
|
|
|
|
|
|
|
=back |
1457
|
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
=head1 AUTHOR |
1459
|
|
|
|
|
|
|
|
1460
|
|
|
|
|
|
|
=head2 MAINTAINER |
1461
|
|
|
|
|
|
|
|
1462
|
|
|
|
|
|
|
Shlomi Fish, L - CPAN ID: "SHLOMIF". |
1463
|
|
|
|
|
|
|
|
1464
|
|
|
|
|
|
|
=head2 ORIGINAL AUTHOR |
1465
|
|
|
|
|
|
|
|
1466
|
|
|
|
|
|
|
Anno Siegel |
1467
|
|
|
|
|
|
|
CPAN ID: ANNO |
1468
|
|
|
|
|
|
|
siegel@zrz.tu-berlin.de |
1469
|
|
|
|
|
|
|
http://www.tu-berlin.de/~siegel |
1470
|
|
|
|
|
|
|
|
1471
|
|
|
|
|
|
|
=head1 COPYRIGHT |
1472
|
|
|
|
|
|
|
|
1473
|
|
|
|
|
|
|
Copyright (c) 2002 Anno Siegel. All rights reserved. |
1474
|
|
|
|
|
|
|
This program is free software; you can redistribute |
1475
|
|
|
|
|
|
|
it and/or modify it under the terms of the ISC license. |
1476
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
(This program had been licensed under the same terms as Perl itself up to |
1478
|
|
|
|
|
|
|
version 1.118 released on 2011, and was relicensed by permission of its |
1479
|
|
|
|
|
|
|
originator). |
1480
|
|
|
|
|
|
|
|
1481
|
|
|
|
|
|
|
The full text of the license can be found in the |
1482
|
|
|
|
|
|
|
LICENSE file included with this module. |
1483
|
|
|
|
|
|
|
|
1484
|
|
|
|
|
|
|
=head1 SEE ALSO |
1485
|
|
|
|
|
|
|
|
1486
|
|
|
|
|
|
|
L, L . |
1487
|
|
|
|
|
|
|
|
1488
|
|
|
|
|
|
|
=head1 EXAMPLES |
1489
|
|
|
|
|
|
|
|
1490
|
|
|
|
|
|
|
=head2 center align and Unicode output |
1491
|
|
|
|
|
|
|
|
1492
|
|
|
|
|
|
|
#!/usr/bin/perl |
1493
|
|
|
|
|
|
|
|
1494
|
|
|
|
|
|
|
use strict; |
1495
|
|
|
|
|
|
|
use warnings; |
1496
|
|
|
|
|
|
|
use utf8; |
1497
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
use Text::Table (); |
1499
|
|
|
|
|
|
|
|
1500
|
|
|
|
|
|
|
binmode STDOUT, ':encoding(utf8)'; |
1501
|
|
|
|
|
|
|
|
1502
|
|
|
|
|
|
|
my @cols = qw/First Last/; |
1503
|
|
|
|
|
|
|
push @cols, |
1504
|
|
|
|
|
|
|
+{ |
1505
|
|
|
|
|
|
|
title => "Country", |
1506
|
|
|
|
|
|
|
align => "center", |
1507
|
|
|
|
|
|
|
}; |
1508
|
|
|
|
|
|
|
my $sep = \'│'; |
1509
|
|
|
|
|
|
|
|
1510
|
|
|
|
|
|
|
my $major_sep = \'║'; |
1511
|
|
|
|
|
|
|
my $tb = Text::Table->new( $sep, " Number ", $major_sep, |
1512
|
|
|
|
|
|
|
( map { +( ( ref($_) ? $_ : " $_ " ), $sep ) } @cols ) ); |
1513
|
|
|
|
|
|
|
|
1514
|
|
|
|
|
|
|
my $num_cols = @cols; |
1515
|
|
|
|
|
|
|
|
1516
|
|
|
|
|
|
|
$tb->load( [ 1, "Mark", "Twain", "USA", ] ); |
1517
|
|
|
|
|
|
|
$tb->load( [ 2, "Charles", "Dickens", "Great Britain", ] ); |
1518
|
|
|
|
|
|
|
$tb->load( [ 3, "Jules", "Verne", "France", ] ); |
1519
|
|
|
|
|
|
|
|
1520
|
|
|
|
|
|
|
my $make_rule = sub { |
1521
|
|
|
|
|
|
|
my ($args) = @_; |
1522
|
|
|
|
|
|
|
|
1523
|
|
|
|
|
|
|
my $left = $args->{left}; |
1524
|
|
|
|
|
|
|
my $right = $args->{right}; |
1525
|
|
|
|
|
|
|
my $main_left = $args->{main_left}; |
1526
|
|
|
|
|
|
|
my $middle = $args->{middle}; |
1527
|
|
|
|
|
|
|
|
1528
|
|
|
|
|
|
|
return $tb->rule( |
1529
|
|
|
|
|
|
|
sub { |
1530
|
|
|
|
|
|
|
my ( $index, $len ) = @_; |
1531
|
|
|
|
|
|
|
|
1532
|
|
|
|
|
|
|
return ( '─' x $len ); |
1533
|
|
|
|
|
|
|
}, |
1534
|
|
|
|
|
|
|
sub { |
1535
|
|
|
|
|
|
|
my ( $index, $len ) = @_; |
1536
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
my $char = ( |
1538
|
|
|
|
|
|
|
( $index == 0 ) ? $left |
1539
|
|
|
|
|
|
|
: ( $index == 1 ) ? $main_left |
1540
|
|
|
|
|
|
|
: ( $index == $num_cols + 1 ) ? $right |
1541
|
|
|
|
|
|
|
: $middle |
1542
|
|
|
|
|
|
|
); |
1543
|
|
|
|
|
|
|
|
1544
|
|
|
|
|
|
|
return $char x $len; |
1545
|
|
|
|
|
|
|
}, |
1546
|
|
|
|
|
|
|
); |
1547
|
|
|
|
|
|
|
}; |
1548
|
|
|
|
|
|
|
|
1549
|
|
|
|
|
|
|
my $start_rule = $make_rule->( |
1550
|
|
|
|
|
|
|
{ |
1551
|
|
|
|
|
|
|
left => '┌', |
1552
|
|
|
|
|
|
|
main_left => '╥', |
1553
|
|
|
|
|
|
|
right => '┐', |
1554
|
|
|
|
|
|
|
middle => '┬', |
1555
|
|
|
|
|
|
|
} |
1556
|
|
|
|
|
|
|
); |
1557
|
|
|
|
|
|
|
|
1558
|
|
|
|
|
|
|
my $mid_rule = $make_rule->( |
1559
|
|
|
|
|
|
|
{ |
1560
|
|
|
|
|
|
|
left => '├', |
1561
|
|
|
|
|
|
|
main_left => '╫', |
1562
|
|
|
|
|
|
|
right => '┤', |
1563
|
|
|
|
|
|
|
middle => '┼', |
1564
|
|
|
|
|
|
|
} |
1565
|
|
|
|
|
|
|
); |
1566
|
|
|
|
|
|
|
|
1567
|
|
|
|
|
|
|
my $end_rule = $make_rule->( |
1568
|
|
|
|
|
|
|
{ |
1569
|
|
|
|
|
|
|
left => '└', |
1570
|
|
|
|
|
|
|
main_left => '╨', |
1571
|
|
|
|
|
|
|
right => '┘', |
1572
|
|
|
|
|
|
|
middle => '┴', |
1573
|
|
|
|
|
|
|
} |
1574
|
|
|
|
|
|
|
); |
1575
|
|
|
|
|
|
|
|
1576
|
|
|
|
|
|
|
print $start_rule, $tb->title, |
1577
|
|
|
|
|
|
|
( map { $mid_rule, $_, } $tb->body() ), $end_rule; |
1578
|
|
|
|
|
|
|
|
1579
|
|
|
|
|
|
|
This emits the following output: |
1580
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
┌────────╥───────┬───────┬─────────────┐ |
1582
|
|
|
|
|
|
|
│ Number ║ First │ Last │Country │ |
1583
|
|
|
|
|
|
|
├────────╫───────┼───────┼─────────────┤ |
1584
|
|
|
|
|
|
|
│1 ║Mark │Twain │ USA │ |
1585
|
|
|
|
|
|
|
├────────╫───────┼───────┼─────────────┤ |
1586
|
|
|
|
|
|
|
│2 ║Charles│Dickens│Great Britain│ |
1587
|
|
|
|
|
|
|
├────────╫───────┼───────┼─────────────┤ |
1588
|
|
|
|
|
|
|
│3 ║Jules │Verne │ France │ |
1589
|
|
|
|
|
|
|
└────────╨───────┴───────┴─────────────┘ |
1590
|
|
|
|
|
|
|
|
1591
|
|
|
|
|
|
|
=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan |
1592
|
|
|
|
|
|
|
|
1593
|
|
|
|
|
|
|
=head1 SUPPORT |
1594
|
|
|
|
|
|
|
|
1595
|
|
|
|
|
|
|
=head2 Websites |
1596
|
|
|
|
|
|
|
|
1597
|
|
|
|
|
|
|
The following websites have more information about this module, and may be of help to you. As always, |
1598
|
|
|
|
|
|
|
in addition to those websites please use your favorite search engine to discover more resources. |
1599
|
|
|
|
|
|
|
|
1600
|
|
|
|
|
|
|
=over 4 |
1601
|
|
|
|
|
|
|
|
1602
|
|
|
|
|
|
|
=item * |
1603
|
|
|
|
|
|
|
|
1604
|
|
|
|
|
|
|
MetaCPAN |
1605
|
|
|
|
|
|
|
|
1606
|
|
|
|
|
|
|
A modern, open-source CPAN search engine, useful to view POD in HTML format. |
1607
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
L |
1609
|
|
|
|
|
|
|
|
1610
|
|
|
|
|
|
|
=item * |
1611
|
|
|
|
|
|
|
|
1612
|
|
|
|
|
|
|
RT: CPAN's Bug Tracker |
1613
|
|
|
|
|
|
|
|
1614
|
|
|
|
|
|
|
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. |
1615
|
|
|
|
|
|
|
|
1616
|
|
|
|
|
|
|
L |
1617
|
|
|
|
|
|
|
|
1618
|
|
|
|
|
|
|
=item * |
1619
|
|
|
|
|
|
|
|
1620
|
|
|
|
|
|
|
CPANTS |
1621
|
|
|
|
|
|
|
|
1622
|
|
|
|
|
|
|
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. |
1623
|
|
|
|
|
|
|
|
1624
|
|
|
|
|
|
|
L |
1625
|
|
|
|
|
|
|
|
1626
|
|
|
|
|
|
|
=item * |
1627
|
|
|
|
|
|
|
|
1628
|
|
|
|
|
|
|
CPAN Testers |
1629
|
|
|
|
|
|
|
|
1630
|
|
|
|
|
|
|
The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions. |
1631
|
|
|
|
|
|
|
|
1632
|
|
|
|
|
|
|
L |
1633
|
|
|
|
|
|
|
|
1634
|
|
|
|
|
|
|
=item * |
1635
|
|
|
|
|
|
|
|
1636
|
|
|
|
|
|
|
CPAN Testers Matrix |
1637
|
|
|
|
|
|
|
|
1638
|
|
|
|
|
|
|
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. |
1639
|
|
|
|
|
|
|
|
1640
|
|
|
|
|
|
|
L |
1641
|
|
|
|
|
|
|
|
1642
|
|
|
|
|
|
|
=item * |
1643
|
|
|
|
|
|
|
|
1644
|
|
|
|
|
|
|
CPAN Testers Dependencies |
1645
|
|
|
|
|
|
|
|
1646
|
|
|
|
|
|
|
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. |
1647
|
|
|
|
|
|
|
|
1648
|
|
|
|
|
|
|
L |
1649
|
|
|
|
|
|
|
|
1650
|
|
|
|
|
|
|
=back |
1651
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
=head2 Bugs / Feature Requests |
1653
|
|
|
|
|
|
|
|
1654
|
|
|
|
|
|
|
Please report any bugs or feature requests by email to C, or through |
1655
|
|
|
|
|
|
|
the web interface at L. You will be automatically notified of any |
1656
|
|
|
|
|
|
|
progress on the request by the system. |
1657
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
=head2 Source Code |
1659
|
|
|
|
|
|
|
|
1660
|
|
|
|
|
|
|
The code is open to the world, and available for you to hack on. Please feel free to browse it and play |
1661
|
|
|
|
|
|
|
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull |
1662
|
|
|
|
|
|
|
from your repository :) |
1663
|
|
|
|
|
|
|
|
1664
|
|
|
|
|
|
|
L |
1665
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
git clone git://github.com/shlomif/Text-Table.git |
1667
|
|
|
|
|
|
|
|
1668
|
|
|
|
|
|
|
=head1 AUTHOR |
1669
|
|
|
|
|
|
|
|
1670
|
|
|
|
|
|
|
Shlomi Fish |
1671
|
|
|
|
|
|
|
|
1672
|
|
|
|
|
|
|
=head1 BUGS |
1673
|
|
|
|
|
|
|
|
1674
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website |
1675
|
|
|
|
|
|
|
L |
1676
|
|
|
|
|
|
|
|
1677
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
1678
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
1679
|
|
|
|
|
|
|
feature. |
1680
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
1682
|
|
|
|
|
|
|
|
1683
|
|
|
|
|
|
|
This software is Copyright (c) 2002 by Anno Siegel and others. |
1684
|
|
|
|
|
|
|
|
1685
|
|
|
|
|
|
|
This is free software, licensed under: |
1686
|
|
|
|
|
|
|
|
1687
|
|
|
|
|
|
|
The ISC License |
1688
|
|
|
|
|
|
|
|
1689
|
|
|
|
|
|
|
=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan |
1690
|
|
|
|
|
|
|
|
1691
|
|
|
|
|
|
|
=head1 SUPPORT |
1692
|
|
|
|
|
|
|
|
1693
|
|
|
|
|
|
|
=head2 Websites |
1694
|
|
|
|
|
|
|
|
1695
|
|
|
|
|
|
|
The following websites have more information about this module, and may be of help to you. As always, |
1696
|
|
|
|
|
|
|
in addition to those websites please use your favorite search engine to discover more resources. |
1697
|
|
|
|
|
|
|
|
1698
|
|
|
|
|
|
|
=over 4 |
1699
|
|
|
|
|
|
|
|
1700
|
|
|
|
|
|
|
=item * |
1701
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
MetaCPAN |
1703
|
|
|
|
|
|
|
|
1704
|
|
|
|
|
|
|
A modern, open-source CPAN search engine, useful to view POD in HTML format. |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
L |
1707
|
|
|
|
|
|
|
|
1708
|
|
|
|
|
|
|
=item * |
1709
|
|
|
|
|
|
|
|
1710
|
|
|
|
|
|
|
RT: CPAN's Bug Tracker |
1711
|
|
|
|
|
|
|
|
1712
|
|
|
|
|
|
|
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. |
1713
|
|
|
|
|
|
|
|
1714
|
|
|
|
|
|
|
L |
1715
|
|
|
|
|
|
|
|
1716
|
|
|
|
|
|
|
=item * |
1717
|
|
|
|
|
|
|
|
1718
|
|
|
|
|
|
|
CPANTS |
1719
|
|
|
|
|
|
|
|
1720
|
|
|
|
|
|
|
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. |
1721
|
|
|
|
|
|
|
|
1722
|
|
|
|
|
|
|
L |
1723
|
|
|
|
|
|
|
|
1724
|
|
|
|
|
|
|
=item * |
1725
|
|
|
|
|
|
|
|
1726
|
|
|
|
|
|
|
CPAN Testers |
1727
|
|
|
|
|
|
|
|
1728
|
|
|
|
|
|
|
The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions. |
1729
|
|
|
|
|
|
|
|
1730
|
|
|
|
|
|
|
L |
1731
|
|
|
|
|
|
|
|
1732
|
|
|
|
|
|
|
=item * |
1733
|
|
|
|
|
|
|
|
1734
|
|
|
|
|
|
|
CPAN Testers Matrix |
1735
|
|
|
|
|
|
|
|
1736
|
|
|
|
|
|
|
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. |
1737
|
|
|
|
|
|
|
|
1738
|
|
|
|
|
|
|
L |
1739
|
|
|
|
|
|
|
|
1740
|
|
|
|
|
|
|
=item * |
1741
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
CPAN Testers Dependencies |
1743
|
|
|
|
|
|
|
|
1744
|
|
|
|
|
|
|
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. |
1745
|
|
|
|
|
|
|
|
1746
|
|
|
|
|
|
|
L |
1747
|
|
|
|
|
|
|
|
1748
|
|
|
|
|
|
|
=back |
1749
|
|
|
|
|
|
|
|
1750
|
|
|
|
|
|
|
=head2 Bugs / Feature Requests |
1751
|
|
|
|
|
|
|
|
1752
|
|
|
|
|
|
|
Please report any bugs or feature requests by email to C, or through |
1753
|
|
|
|
|
|
|
the web interface at L. You will be automatically notified of any |
1754
|
|
|
|
|
|
|
progress on the request by the system. |
1755
|
|
|
|
|
|
|
|
1756
|
|
|
|
|
|
|
=head2 Source Code |
1757
|
|
|
|
|
|
|
|
1758
|
|
|
|
|
|
|
The code is open to the world, and available for you to hack on. Please feel free to browse it and play |
1759
|
|
|
|
|
|
|
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull |
1760
|
|
|
|
|
|
|
from your repository :) |
1761
|
|
|
|
|
|
|
|
1762
|
|
|
|
|
|
|
L |
1763
|
|
|
|
|
|
|
|
1764
|
|
|
|
|
|
|
git clone git://github.com/shlomif/Text-Table.git |
1765
|
|
|
|
|
|
|
|
1766
|
|
|
|
|
|
|
=head1 AUTHOR |
1767
|
|
|
|
|
|
|
|
1768
|
|
|
|
|
|
|
Shlomi Fish |
1769
|
|
|
|
|
|
|
|
1770
|
|
|
|
|
|
|
=head1 BUGS |
1771
|
|
|
|
|
|
|
|
1772
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website |
1773
|
|
|
|
|
|
|
L |
1774
|
|
|
|
|
|
|
|
1775
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
1776
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
1777
|
|
|
|
|
|
|
feature. |
1778
|
|
|
|
|
|
|
|
1779
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
1780
|
|
|
|
|
|
|
|
1781
|
|
|
|
|
|
|
This software is Copyright (c) 2002 by Anno Siegel and others. |
1782
|
|
|
|
|
|
|
|
1783
|
|
|
|
|
|
|
This is free software, licensed under: |
1784
|
|
|
|
|
|
|
|
1785
|
|
|
|
|
|
|
The ISC License |
1786
|
|
|
|
|
|
|
|
1787
|
|
|
|
|
|
|
=cut |
1788
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
__END__ |
| | | | | |