Term::Table(3perl)

🌸 Term::Table β€” the perl pragma-docs page, but make it cute 🌸

okay bestie, this page lives in the Perl Programmers Reference Guide under section 3perl, and it's about turning a header plus a pile of rows into a table that actually looks good. it's giving spreadsheet but make it ✨aesthetic✨.


πŸ’– NAME πŸ’–

Term::Table β€” Format a header and rows into a table


πŸ¦‹ DESCRIPTION πŸ¦‹

so basically? this module is the one your failing tests call in when they need to spill the tea about what went wrong. it rewrites words into a table. that's the whole vibe. it is able to format rows of data into tables, no cap.


✨ SYNOPSIS ✨

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;

and it prints a table that looks, honestly, iconic:

+------------------+---------+------------+
| name             | age     | hair color |
+------------------+---------+------------+
| Fred Flintstone  | 2000000 | black      |
| Wilma Flintstone | 1999995 | red        |
| ...              | ...     | ...        |
+------------------+---------+------------+

πŸ”₯ INTERFACE πŸ”₯

use Term::Table;
my $table = Term::Table->new(...);

πŸ’… OPTIONS πŸ’…

  • header => [ ... ] β€” want a header? drop it here. it takes an arrayref, and each thing inside is the heading for that column. periodt.
  • rows => [ [...], [...], ... ] β€” this one should be an arrayref that contains one arrayref per row. rows inside rows, layers on layers, we love a nested moment.
  • collapse => $bool β€” turn this on if you want empty columns to just... not exist. a column with no data in any row gets hidden. and ngl, having a header for that column changes nothing β€” collapse does not care about your header.
  • max_width => $num β€” sets the maximum width of the table. the table may not be this big, but it will be no bigger, fr. if you don't specify one it will attempt to find the width of your terminal and use that, otherwise it falls back to the terminal width or 80.
  • pad => $num β€” defaults to 4, and it's extra padding for row width calculations. the default is there for legacy support. set this to 0 to turn padding off entirely.
  • allow_overflow => $bool β€” defaults to 0. if this is off then an exception will be thrown if the table cannot be made to fit inside the max-width. if you set it to 1 then the table will be rendered anyway β€” larger than max-width β€” if it is not possible to stay within the max-width. in other words? this turns max-width from a hard-limit into a soft recommendation. the softest of recommendations tbh.
  • sanitize => $bool β€” this will sanitize all the data in the table such that newlines, control characters, and all whitespace except for ASCII 20 ' ' are replaced with escape sequences. it stops newlines, tabs, and similar whitespace from disrupting the table. protective queen behavior 🫢.
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. both. at the same time. multitasking icon.

  • mark_tail => $bool β€” this will replace the last whitespace character of any trailing whitespace with its escape sequence. it makes it easier to notice trailing whitespace when comparing values β€” and yes, trailing whitespace is the villain of every diff, so this is a public service πŸ’€.
  • show_header => $bool β€” set this to false to hide the header. it defaults to true if the header is set, and false if no header is provided.
  • auto_columns => $bool β€” set this to true to automatically add columns that are not named in the header. it defaults to false if a header is provided, and defaults to true when there is no header. so like, no header = it just goes and grabs them all itself, main character energy.
  • no_collapse => [ $col_num_a, $col_num_b, ... ]
  • no_collapse => [ $col_name_a, $col_name_b, ... ]
  • no_collapse => { $col_num_a => 1, $col_num_b => 1, ... }
  • no_collapse => { $col_name_a => 1, $col_name_b => 1, ... } β€” specify (by number and/or name) columns that should not be removed when empty. heads up: the 'name' form only works when a header is specified. and there is currently no protection to insure that names you specify are actually in the header β€” invalid names are ignored, and patches to fix this will be happily accepted πŸ‘€ (a literal open invitation, besties).

🌈 NOTE ON UNICODE/WIDE CHARACTERS 🌈

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 |
+-----+-----+---+

not the slay we were going for, but okay 😭


πŸŽ€ SOURCE πŸŽ€

the source code repository for Term-Table can be found at https://github.com/exodist/Term-Table/.


πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum <exodist@cpan.org>


⭐ AUTHORS ⭐

Chad Granum <exodist@cpan.org>


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/>


and that's a wrap, bestie 🧚 this was perl v5.44.0, dated 2026-10-17, and the page itself is Term::Table(3perl) β€” go forth and make your diagnostic tables pretty πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms