| Term::Table(3pm) | User Contributed Perl Documentation | Term::Table(3pm) |
Term::Table - Format a header and rows into a table
This is used by some failing tests to provide diagnostics about what has gone wrong. This module is able to format rows of data into tables.
use Term::Table;
my $table = Term::Table->new(
max_width => 80, # Defaults to terminal size
pad => 4, # Extra padding between table and max-width (defaults to 4)
allow_overflow => 0, # Default is 0, when off an exception will be thrown if the table is too big
collapse => 1, # Do not show empty columns
header => ['name', 'age', 'hair color'],
rows => [
['Fred Flintstone', 2000000, 'black'],
['Wilma Flintstone', 1999995, 'red'],
...
],
);
say $_ for $table->render;
This prints a table like this:
+------------------+---------+------------+
| name | age | hair color |
+------------------+---------+------------+
| Fred Flintstone | 2000000 | black |
| Wilma Flintstone | 1999995 | red |
| ... | ... | ... |
+------------------+---------+------------+
use Term::Table;
my $table = Term::Table->new(...);
Note: newlines are marked as "\n", but a newline is also inserted into the data so that it typically displays in a way that is useful to humans.
Example:
my $field = "foo\nbar\nbaz\n";
print join "\n" => table(
sanitize => 1,
rows => [
[$field, 'col2' ],
['row2 col1', 'row2 col2']
]
);
Prints:
+-----------------+-----------+
| foo\n | col2 |
| bar\n | |
| baz\n | |
| | |
| row2 col1 | row2 col2 |
+-----------------+-----------+
So it marks the newlines by inserting the escape sequence, but it also shows the data across as many lines as it would normally display.
Some unicode characters, such as "婧" ("U+5A67") are wider than others. These will render just fine if you "use utf8;" as necessary, and Unicode::GCString is installed, however if the module is not installed there will be anomalies in the table:
+-----+-----+---+
| a | b | c |
+-----+-----+---+
| 婧 | x | y |
| x | y | z |
| x | 婧 | z |
+-----+-----+---+
The source code repository for "Term-Table" can be found at <https://github.com/exodist/Term-Table/>.
Copyright 2016 Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <https://dev.perl.org/licenses/>
| 2023-10-26 | perl v5.36.0 |