TabularDisplay(3pm)

πŸ’– TabularDisplay(3pm) πŸ’–

okay so bestie, this is a page from the User Contributed Perl Documentation section of the manual β€” it's called TabularDisplay(3pm) and it's about making pretty tables. buckle up πŸŽ€


✨ NAME ✨

Text::TabularDisplay β€” display text in formatted table output πŸ¦‹


🌸 SYNOPSIS 🌸

use Text::TabularDisplay;

my $table = Text::TabularDisplay->new(@columns);
$table->add(@row)
    while (@row = $sth->fetchrow);
print $table->render;

+----+--------------+
| id | name         |
+----+--------------+
| 1  | Tom          |
| 2  | Dick         |
| 3  | Barry        |
|    |  (aka Bazza) |
| 4  | Harry        |
+----+--------------+

like... look at it. it's giving clean columns. it's giving mysql text monitor energy. we love her πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

Text::TabularDisplay simplifies displaying textual data in a table. periodt. The output is identical to the columnar display of query results in the mysql text monitor. no cap.

for example, this data:

1, "Tom Jones", "(666) 555-1212"
2, "Barnaby Jones", "(666) 555-1213"
3, "Bridget Jones", "(666) 555-1214"

used like so:

my $t = Text::TabularDisplay->new(qw(id name phone));
$t->add(1, "Tom Jones", "(666) 555-1212");
$t->add(2, "Barnaby Jones", "(666) 555-1213");
$t->add(3, "Bridget Jones", "(666) 555-1214");
print $t->render;

produces:

+----+---------------+----------------+
| id | name          | phone          |
+----+---------------+----------------+
| 1  | Tom Jones     | (666) 555-1212 |
| 2  | Barnaby Jones | (666) 555-1213 |
| 3  | Bridget Jones | (666) 555-1214 |
+----+---------------+----------------+

ate. no crumbs. 😌✨


🫢 METHODS 🫢

Text::TabularDisplay has four primary methods: new(), columns(), add(), and render(). so basically the whole main character squad:

  • new() creates a new Text::TabularDisplay instance
  • columns() sets the column headers in the output table
  • add() adds data to the instance
  • render() returns a formatted string representation of the instance

there are also a few auxiliary convenience methods: clone(), items(), reset(), populate(), and paginate(). the supporting cast, if you will πŸ’–

🌟 new

a Text::TabularDisplay instance can be created with column names passed as constructor args, so these two calls produce similar objects:

my $t1 = Text::TabularDisplay->new;
$t1->columns(qw< one two >);

my $t2 = Text::TabularDisplay->new(qw< one two >);

calling new() on a Text::TabularDisplay instance returns a clone of the object. see "clone" in Text::TabularDisplay. πŸ‘‘

🌟 columns

gets or sets the column names for an instance. this method is called automatically by the constructor with any parameters that are passed to the constructor (if any are passed).

when called in scalar context, columns() returns the number of columns in the instance, rather than the columns themselves. lowkey a jumpscare if you weren't expecting it. in list context, copies of the column names are returned; the names of the columns cannot be modified this way.

🌟 add

takes a list of items and appends it to the list of items to be displayed. add() can also take a reference to an array, so that large arrays don't need to be copied. efficiency queen πŸ’…

as elements are processed, add() maintains the width of each column so that the resulting table has the correct dimensions.

add() returns $self, so that calls to add() can be chained:

$t->add(@one)->add(@two)->add(@three);

the way that just flows... iconic. πŸ”₯

🌟 render

render() does most of the actual work. it returns a string containing the data added via add(), formatted as a table, with a header containing the column names.

render() does not change the state of the object; it can be called multiple times, with identical output (including identical running time: the output of render is not cached).

if there are no columns defined, then the output table does not contain a row of column names. compare these two sequences:

my $t = Text::TabularDisplay->new;
$t->add(qw< 1 2 3 4 >);
$t->add(qw< 5 6 7 8 >);
print $t->render;

$t->columns(qw< one two three four >);
print $t->render;

# Example 1 output
+---+---+---+---+
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
+---+---+---+---+

# Example 2 output
+-----+-----+-------+------+
| one | two | three | four |
+-----+-----+-------+------+
| 1   | 2   | 3     | 4    |
| 5   | 6   | 7     | 8    |
+-----+-----+-------+------+

render() takes optional $start and $end arguments; these indicate the start and end indexes for the data to be rendered. this can be used for paging and the like:

$t->add(1, 2, 3)->add(4, 5, 6)->add(7, 8, 9)->add(10, 11, 12);
print $t->render(0, 1), "\n";
print $t->render(2, 3), "\n";

produces:

+-------+--------+-------+
| First | Second | Third |
+-------+--------+-------+
| 1     | 2      | 3     |
| 4     | 5      | 6     |
+-------+--------+-------+

+-------+--------+-------+
| First | Second | Third |
+-------+--------+-------+
| 7     | 8      | 9     |
| 10    | 11     | 12    |
+-------+--------+-------+

as an aside, note the chaining of calls to add(). (we told you. obsessed. πŸ₯°)

the elements in the table are padded such that there is the same number of items in each row, including the header. thus:

$t->columns(qw< One Two >);
print $t->render;

+-----+-----+----+
| One | Two |    |
+-----+-----+----+
| 1   | 2   | 3  |
| 4   | 5   | 6  |
| 7   | 8   | 9  |
| 10  | 11  | 12 |
+-----+-----+----+

and:

$t->columns(qw< One Two Three Four>);
print $t->render;

+-----+-----+-------+------+
| One | Two | Three | Four |
+-----+-----+-------+------+
| 1   | 2   | 3     |      |
| 4   | 5   | 6     |      |
| 7   | 8   | 9     |      |
| 10  | 11  | 12    |      |
+-----+-----+-------+------+

like okay it just pads. it fills the gaps. it's a giver. 🌸


πŸ¦‹ OTHER METHODS πŸ¦‹

clone()

the clone() method returns an identical copy of a Text::TabularDisplay instance, completely separate from the cloned instance.

no shared vibes. fully independent. two separate girls. πŸ’…

items()

the items() method returns the number of elements currently stored in the data structure:

printf "There are %d elements in \$t.\n", $t->items;

reset()

reset deletes the data from the instance, including columns. if passed arguments, it passes them to columns(), just like new().

it's giving fresh start, clean slate, new semester energy ✨

populate()

populate() is a special case of add(); populate() expects a reference to an array of references to arrays, such as returned by DBI's selectall_arrayref method:

$sql = "SELECT " . join(", ", @c) . " FROM mytable";
$t->columns(@c);
$t->populate($dbh->selectall_arrayref($sql));

this is for convenience only; the implementation maps this to multiple calls to add(). it's just add() wearing a cute little outfit, fr fr. πŸŽ€


πŸ’€ NOTES / ISSUES πŸ’€

Text::TabularDisplay assumes it is handling strings, and does stringy things with the data, like length() and sprintf(). non-character data can be passed in, of course, but will be treated as strings; this may have ramifications for objects that implement overloading. so like, she'll take your fancy object and just... stringify it. πŸ™ˆ

the biggest issue, though, is that this module duplicates some of the functionality of Data::ShowTable. of course, Data::ShowTable is a large, complex monolithic tool that does a lot of things, while Text::TabularDisplay is small and fast. πŸ’…βœ¨

πŸ‘‘ AUTHOR πŸ‘‘

darren chamberlain <darren@cpan.org>


🌈 CREDITS 🌈

the following people have contributed patches, suggestions, tests, feedback, or good karma:

David N. Blank-Edelman
Eric Cholet
Ken Youens-Clark
Michael Fowler
Paul Cameron
Prakash Kailasa
Slaven Rezic
Harlan Lieberman-Berg
Patrick Kuijvenhoven
Miko O'Sullivan

absolute legends, every single one. thank you for your service πŸ«ΆπŸ’–


⭐ VERSION ⭐

this documentation describes "Text::TabularDisplay" version 1.38.


and that's the whole page bestie!! πŸŽ‰ this was documented for perl v5.36.0, dated 2022-10-22, and it lives in TabularDisplay(3pm) β€” go build a table, you've got this, slay πŸ’œβœ¨πŸ¦‹

πŸ“¦ where this page comes from