1TabularDisplay(3) User Contributed Perl Documentation TabularDisplay(3)
2
3
4
6 Text::TabularDisplay - Display text in formatted table output
7
9 use Text::TabularDisplay;
10
11 my $table = Text::TabularDisplay->new(@columns);
12 $table->add(@row)
13 while (@row = $sth->fetchrow);
14 print $table->render;
15
16 +----+--------------+
17 ⎪ id ⎪ name ⎪
18 +----+--------------+
19 ⎪ 1 ⎪ Tom ⎪
20 ⎪ 2 ⎪ Dick ⎪
21 ⎪ 3 ⎪ Barry ⎪
22 ⎪ ⎪ (aka Bazza) ⎪
23 ⎪ 4 ⎪ Harry ⎪
24 +----+--------------+
25
27 Text::TabularDisplay simplifies displaying textual data in a table.
28 The output is identical to the columnar display of query results in the
29 mysql text monitor. For example, this data:
30
31 1, "Tom Jones", "(666) 555-1212"
32 2, "Barnaby Jones", "(666) 555-1213"
33 3, "Bridget Jones", "(666) 555-1214"
34
35 Used like so:
36
37 my $t = Text::TabularDisplay->new(qw(id name phone));
38 $t->add(1, "Tom Jones", "(666) 555-1212");
39 $t->add(2, "Barnaby Jones", "(666) 555-1213");
40 $t->add(3, "Bridget Jones", "(666) 555-1214");
41 print $t->render;
42
43 Produces:
44
45 +----+---------------+----------------+
46 ⎪ id ⎪ name ⎪ phone ⎪
47 +----+---------------+----------------+
48 ⎪ 1 ⎪ Tom Jones ⎪ (666) 555-1212 ⎪
49 ⎪ 2 ⎪ Barnaby Jones ⎪ (666) 555-1213 ⎪
50 ⎪ 3 ⎪ Bridget Jones ⎪ (666) 555-1214 ⎪
51 +----+---------------+----------------+
52
54 Text::TabularDisplay has four primary methods: new(), columns(), add(),
55 and render(). new() creates a new Text::TabularDisplay instance; col‐
56 umns() sets the column headers in the output table; add() adds data to
57 the instance; and render() returns a formatted string representation of
58 the instance.
59
60 There are also a few auxilliary convenience methods: clone(), items(),
61 reset(), populate(), and paginate().
62
63 new A Text::TabularDisplay instance can be created with column names
64 passed as constructor args, so these two calls produce similar
65 objects:
66
67 my $t1 = Text::TabularDisplay->new;
68 $t1->columns(qw< one two >);
69
70 my $t2 = Text::TabularDisplay->new(qw< one two >);
71
72 Calling new() on a Text::TabularDisplay instance returns a clone of
73 the object. See "clone" in Text::TabularDisplay.
74
75 columns
76 Gets or sets the column names for an instance. This method is
77 called automatically by the constructor with any parameters that
78 are passed to the constructor (if any are passed).
79
80 When called in scalar context, columns() returns the number of col‐
81 umns in the instance, rather than the columns themselves. In list
82 context, copies of the columns names are returned; the names of the
83 columns cannot be modified this way.
84
85 add Takes a list of items and appends it to the list of items to be
86 displayed. add() can also take a reference to an array, so that
87 large arrays don't need to be copied.
88
89 As elements are processed, add() maintains the width of each column
90 so that the resulting table has the correct dimensions.
91
92 add() returns $self, so that calls to add() can be chained:
93
94 $t->add(@one)->add(@two)->add(@three);
95
96 render
97 render() does most of the actual work. It returns a string contain‐
98 ing the data added via add(), formatted as a table, with a header
99 containing the column names.
100
101 render() does not change the state of the object; it can be called
102 multiple times, with identical output (including identical running
103 time: the output of render is not cached).
104
105 If there are no columns defined, then the output table does not
106 contains a row of column names. Compare these two sequences:
107
108 my $t = Text::TabularDisplay->new;
109 $t->add(qw< 1 2 3 4 >);
110 $t->add(qw< 5 6 7 8 >);
111 print $t->render;
112
113 $t->columns(qw< one two three four >);
114 print $t->render;
115
116 # Example 1 output
117 +---+---+---+---+
118 ⎪ 1 ⎪ 2 ⎪ 3 ⎪ 4 ⎪
119 ⎪ 5 ⎪ 6 ⎪ 7 ⎪ 8 ⎪
120 +---+---+---+---+
121
122 # Example 2 output
123 +-----+-----+-------+------+
124 ⎪ one ⎪ two ⎪ three ⎪ four ⎪
125 +-----+-----+-------+------+
126 ⎪ 1 ⎪ 2 ⎪ 3 ⎪ 4 ⎪
127 ⎪ 5 ⎪ 6 ⎪ 7 ⎪ 8 ⎪
128 +-----+-----+-------+------+
129
130 render() takes optional $start and $end arguments; these indicate
131 the start and end indexes for the data to be rendered. This can be
132 used for paging and the like:
133
134 $t->add(1, 2, 3)->add(4, 5, 6)->add(7, 8, 9)->add(10, 11, 12);
135 print $t->render(0, 1), "\n";
136 print $t->render(2, 3), "\n";
137
138 Produces:
139
140 +-------+--------+-------+
141 ⎪ First ⎪ Second ⎪ Third ⎪
142 +-------+--------+-------+
143 ⎪ 1 ⎪ 2 ⎪ 3 ⎪
144 ⎪ 4 ⎪ 5 ⎪ 6 ⎪
145 +-------+--------+-------+
146
147 +-------+--------+-------+
148 ⎪ First ⎪ Second ⎪ Third ⎪
149 +-------+--------+-------+
150 ⎪ 7 ⎪ 8 ⎪ 9 ⎪
151 ⎪ 10 ⎪ 11 ⎪ 12 ⎪
152 +-------+--------+-------+
153
154 As an aside, note the chaining of calls to add().
155
156 The elements in the table are padded such that there is the same
157 number of items in each row, including the header. Thus:
158
159 $t->columns(qw< One Two >);
160 print $t->render;
161
162 +-----+-----+----+
163 ⎪ One ⎪ Two ⎪ ⎪
164 +-----+-----+----+
165 ⎪ 1 ⎪ 2 ⎪ 3 ⎪
166 ⎪ 4 ⎪ 5 ⎪ 6 ⎪
167 ⎪ 7 ⎪ 8 ⎪ 9 ⎪
168 ⎪ 10 ⎪ 11 ⎪ 12 ⎪
169 +-----+-----+----+
170
171 And:
172
173 $t->columns(qw< One Two Three Four>);
174 print $t->render;
175
176 +-----+-----+-------+------+
177 ⎪ One ⎪ Two ⎪ Three ⎪ Four ⎪
178 +-----+-----+-------+------+
179 ⎪ 1 ⎪ 2 ⎪ 3 ⎪ ⎪
180 ⎪ 4 ⎪ 5 ⎪ 6 ⎪ ⎪
181 ⎪ 7 ⎪ 8 ⎪ 9 ⎪ ⎪
182 ⎪ 10 ⎪ 11 ⎪ 12 ⎪ ⎪
183 +-----+-----+-------+------+
184
186 clone()
187 The clone() method returns an identical copy of a Text::TabularDis‐
188 play instance, completely separate from the cloned instance.
189
190 items()
191 The items() method returns the number of elements currently stored
192 in the data structure:
193
194 printf "There are %d elements in \$t.\n", $t->items;
195
196 reset()
197 Reset deletes the data from the instance, including columns. If
198 passed arguments, it passes them to columns(), just like new().
199
200 populate()
201 populate() as a special case of add(); populate() expects a refer‐
202 ence to an array of references to arrays, such as returned by DBI's
203 selectall_arrayref method:
204
205 $sql = "SELECT " . join(", ", @c) . " FROM mytable";
206 $t->columns(@c);
207 $t->populate($dbh->selectall_arrayref($sql));
208
209 This is for convenience only; the implementation maps this to mul‐
210 tiple calls to add().
211
213 Text::TabularDisplay assumes it is handling strings, and does stringy
214 things with the data, like legnth() and sprintf(). Non-character data
215 can be passed in, of course, but will be treated as strings; this may
216 have ramifications for objects that implement overloading.
217
218 The biggest issue, though, is that this module duplicates a some of the
219 functionality of Data::ShowTable. Of course, Data::ShowTable is a
220 large, complex monolithic tool that does a lot of things, while
221 Text::TabularDisplay is small and fast.
222
224 darren chamberlain <darren@cpan.org>
225
227 The following people have contributed patches, suggestions, tests,
228 feedback, or good karma:
229
230 David N. Blank-Edelman
231 Eric Cholet
232 Ken Youens-Clark
233 Michael Fowler
234 Paul Cameron
235 Prakash Kailasa
236 Slaven Rezic
237
239 This documentation describes "Text::TabularDisplay" version 1.20.
240
241
242
243perl v5.8.8 2006-01-03 TabularDisplay(3)