TEST2::UTIL::TABLE(3pm)

πŸ’… Test2::Util::Table β€” the Test2::Util::Table(3pm) glow-up

Okay so bestie, welcome to Test2::Util::Table(3pm) β€” the little Perl module from the Perl Programmers Reference Guide that literally just formats your header + rows into a pretty little table πŸŽ€

✨ NAME ✨

Test2::Util::Table β€” Format a header and rows into a table. That's it, that's the whole vibe πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea: this module gets used by some failing tests to give you diagnostics about what actually went wrong 😭 Like, when a test flops, this thing is the one pulling up with receipts. It's able to generic format rows of data into tables β€” no cap, that's literally what it does ✨

🌸 SYNOPSIS 🌸

use Test2::Util::Table qw/table/;

my @table = table(
    max_width => 80,
    collapse => 1, # Do not show empty columns
    header => [ 'name', 'age', 'hair color' ],
    rows => [
        [ 'Fred Flinstone',  2000000, 'black' ],
        [ 'Wilma Flinstone', 1999995, 'red' ],
        ...,
    ],
);

# The @table array contains each line of the table, no newlines added.
say $_ for @table;

And this prints a table like this πŸ‘‡

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

Obsessed. Iconic. The alignment? Immaculate 🀌✨

πŸŽ€ EXPORTS πŸŽ€

🌈 @rows = table(...)

The function returns a list of lines, and those lines do not have the newline "\n" character appended. Clean lines only, no trailing baggage πŸ’…

Here come the options, bestie β€” pay attention, there's a lot of them:

If you want a header, specify it here. This takes an arrayref with each column's heading. It's giving... labels πŸ‘‘

rows => [ [...], [...], ... ]

This should be an arrayref containing an arrayref per row. Row inside a row, inception style πŸŒ€

collapse => $bool

Use this if you want to hide empty columns β€” that is, any column that has no data in any row. And heads up: having a header for the column will not effect collapse. So the header doesn't get a say in it, periodt.

max_width => $num

Set the maximum width of the table. The table may not be this big, but it will be no bigger. If none is specified, it'll attempt to find the width of your terminal and use that β€” otherwise it falls back to 80. Safety net energy 🫢

sanitize => $bool

Okay this one's important πŸ”₯ 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. This prevents newlines, tabs, and similar whitespace from disrupting the table. We love a tidy table 😌

Note: newlines are marked as '\n', but a newline is also inserted into the data so that it typically displays in a way that's useful to humans πŸ’–

Example, because examples ate:

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 things at once β€” multitasking icon πŸ’…

mark_tail => $bool

This will replace the last whitespace character of any trailing whitespace with its escape sequence 🌸 This makes it easier to notice trailing whitespace when comparing values. Trailing whitespace is sneaky and must be exposed 😀

⭐ my $cols = term_size()

Attempts to find the width in columns (characters) of the current terminal. Returns 80 as a safe bet if it cannot find it another way. Lowkey the most reliable friend in the group chat 🫢


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

See how cursed that looks? Yeah. Install the module, bestie πŸ™ˆ


πŸ”₯ SOURCE πŸ”₯

The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go star it, iykyk ⭐

πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum <exodist@cpan.org>

πŸ’œ AUTHORS πŸ’œ

Chad Granum <exodist@cpan.org>

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


That's the whole page, bestie, no crumbs left πŸ“ This was perl v5.42.3, dated 2026-11-03, and yes β€” the page you just read is literally called Test2::Util::Table(3pm). Slay πŸ’…βœ¨

πŸ“¦ where this page comes from