Term::Table(3) | User Contributed Perl Documentation | Term::Table(3) |
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 generic 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 Flinstone', 2000000, 'black'], ['Wilma Flinstone', 1999995, 'red'], ... ], ); say $_ for $table->render;
This prints a table like this:
+-----------------+---------+------------+ | name | age | hair color | +-----------------+---------+------------+ | Fred Flinstone | 2000000 | black | | Wilma Flinstone | 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 http://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 http://dev.perl.org/licenses/
2019-11-18 | perl v5.34.0 |