1Text::Table::Tiny(3) User Contributed Perl Documentation Text::Table::Tiny(3)
2
3
4
6 Text::Table::Tiny - generate simple text tables from 2D arrays
7
9 use Text::Table::Tiny 1.00 qw/ generate_table /;
10
11 my $rows = [
12 [qw/ Pokemon Type Count /],
13 [qw/ Abra Psychic 5 /],
14 [qw/ Ekans Poison 123 /],
15 [qw/ Feraligatr Water 5678 /],
16 ];
17
18 print generate_table(rows => $rows, header_row => 1), "\n";
19
21 This module provides a single function, "generate_table", which formats
22 a two-dimensional array of data as a text table. There are a number of
23 options for adjusting the output format, but the intention is that the
24 default option is good enough for most uses.
25
26 The example shown in the SYNOPSIS generates the following table:
27
28 +------------+---------+-------+
29 | Pokemon | Type | Count |
30 +------------+---------+-------+
31 | Abra | Psychic | 5 |
32 | Ekans | Poison | 123 |
33 | Feraligatr | Water | 5678 |
34 +------------+---------+-------+
35
36 NOTE: the interface changed with version 0.04, so if you use the
37 "generate_table()" function illustrated above, then you need to require
38 at least version 0.04 of this module, as shown in the SYNOPSIS.
39
40 NOTE 2: some of the options described below were added in version 1.00,
41 so your best bet is to require version 1.00, as per the SYNOPSIS.
42
43 generate_table()
44 The "generate_table" function understands a number of arguments, which
45 are passed as a hash. The only required argument is rows. Where
46 arguments were not supported in the original release, the first
47 supporting version is noted.
48
49 · rows
50
51 Takes an array reference which should contain one or more rows of
52 data, where each row is an array reference.
53
54 · header_row
55
56 If given a true value, the first row in the data will be
57 interpreted as a header row, and separated from the rest of the
58 table with a ruled line.
59
60 · separate_rows
61
62 If given a true value, a separator line will be drawn between every
63 row in the table, and a thicker line will be used for the header
64 separator.
65
66 · top_and_tail
67
68 If given a true value, then the top and bottom border lines will be
69 skipped. This reduces the vertical height of the generated table.
70
71 Added in 0.04.
72
73 · align
74
75 This takes an array ref with one entry per column, to specify the
76 alignment of that column. Legal values are 'l', 'c', and 'r'. You
77 can also specify a single alignment for all columns. ANSI escape
78 codes are handled.
79
80 Added in 1.00.
81
82 · style
83
84 Specifies the format of the output table. The default is
85 'classic', but other options are 'boxrule' and 'norule'.
86
87 If you use the "boxrule" style, you'll probably need to run
88 "binmode(STDOUT, ':utf8')".
89
90 Added in 1.00.
91
92 · indent
93
94 Specify an indent that should be prefixed to every line of the
95 generated table. This can either be a string of spaces, or an
96 integer giving the number of spaces wanted.
97
98 Added in 1.00.
99
100 · compact
101
102 If set to a true value then we omit the single space padding on
103 either side of every column.
104
105 Added in 1.00.
106
107 EXAMPLES
108 If you just pass the data and no other options:
109
110 generate_table(rows => $rows);
111
112 You get minimal ruling:
113
114 +------------+---------+-------+
115 | Pokemon | Type | Count |
116 | Abra | Psychic | 5 |
117 | Ekans | Poison | 123 |
118 | Feraligatr | Water | 5678 |
119 +------------+---------+-------+
120
121 If you want a separate header, set the header_row option to a true
122 value, as shown in the SYNOPSIS.
123
124 To take up fewer lines, you can miss out the top and bottom rules, by
125 setting "top_and_tail" to a true value:
126
127 generate_table(rows => $rows, header_row => 1, top_and_tail => 1);
128
129 This will generate the following:
130
131 | Pokemon | Type | Count |
132 +------------+---------+-------+
133 | Abra | Psychic | 5 |
134 | Ekans | Poison | 123 |
135 | Feraligatr | Water | 5678 |
136
137 If you want a more stylish looking table, set the "style" parameter to
138 'boxrule':
139
140 binmode(STDOUT,':utf8');
141 generate_table(rows => $rows, header_row => 1, style => 'boxrule');
142
143 This uses the ANSI box rule characters. Note that you will need to
144 ensure UTF output.
145
146 ┌────────────┬─────────┬───────┐
147 │ Pokemon │ Type │ Count │
148 ├────────────┼─────────┼───────┤
149 │ Abra │ Psychic │ 5 │
150 │ Ekans │ Poison │ 123 │
151 │ Feraligatr │ Water │ 5678 │
152 └────────────┴─────────┴───────┘
153
154 You might want to right-align numeric values:
155
156 generate_table( ... , align => [qw/ l l r /] );
157
158 The "align" parameter can either take an arrayref, or a string with an
159 alignment to apply to all columns:
160
161 ┌────────────┬─────────┬───────┐
162 │ Pokemon │ Type │ Count │
163 ├────────────┼─────────┼───────┤
164 │ Abra │ Psychic │ 5 │
165 │ Ekans │ Poison │ 123 │
166 │ Feraligatr │ Water │ 5678 │
167 └────────────┴─────────┴───────┘
168
169 If you're using the boxrule style, you might feel you can remove the
170 padding on either side of every column, done by setting "compact" to a
171 true value:
172
173 ┌──────────┬───────┬─────┐
174 │Pokemon │Type │Count│
175 ├──────────┼───────┼─────┤
176 │Abra │Psychic│ 5│
177 │Ekans │Poison │ 123│
178 │Feraligatr│Water │ 5678│
179 └──────────┴───────┴─────┘
180
181 You can also ask for a rule between each row, in which case the header
182 rule becomes stronger. This works best when combined with the boxrule
183 style:
184
185 generate_table( ... , separate_rows => 1 );
186
187 Which results in the following:
188
189 ┌────────────┬─────────┬───────┐
190 │ Pokemon │ Type │ Count │
191 ╞════════════╪═════════╪═══════╡
192 │ Abra │ Psychic │ 5 │
193 ├────────────┼─────────┼───────┤
194 │ Ekans │ Poison │ 123 │
195 ├────────────┼─────────┼───────┤
196 │ Feraligatr │ Water │ 5678 │
197 └────────────┴─────────┴───────┘
198
199 You can use this with the other styles, but I'm not sure you'd want to.
200
201 If you just want columnar output, use the "norule" style:
202
203 generate_table( ... , style => 'norule' );
204
205 which results in:
206
207 Pokemon Type Count
208
209 Abra Psychic 5
210 Ekans Poison 123
211 Feraligatr Water 5678
212
213 Note that everywhere you saw a line on the previous tables, there will
214 be a space character in this version. So you may want to combine the
215 "top_and_tail" option, to suppress the extra blank lines before and
216 after the body of the table.
217
219 My blog post <http://neilb.org/2019/08/06/text-table-tiny-changes.html>
220 where I described changes to formatting; this has more examples.
221
222 There are many modules for formatting text tables on CPAN. A good
223 number of them are listed in the See Also
224 <https://metacpan.org/pod/Text::Table::Manifold#See-Also> section of
225 the documentation for Text::Table::Manifold.
226
228 <https://github.com/neilb/Text-Table-Tiny>
229
231 Neil Bowers <neilb@cpan.org>
232
233 The original version was written by Creighton Higgins
234 <chiggins@chiggins.com>, but the module was entirely rewritten for
235 0.05_01.
236
238 This software is copyright (c) 2020 by Neil Bowers.
239
240 This is free software; you can redistribute it and/or modify it under
241 the same terms as the Perl 5 programming language system itself.
242
243
244
245perl v5.32.0 2020-08-09 Text::Table::Tiny(3)