Test2::Util::Table(3)
π
Test2::Util::Table(3) β hey bestie! π
Okay so welcome to Test2::Util::Table(3), a User Contributed Perl Documentation page. This is the one that formats a header and rows into a table, and itβs lowkey iconic for test diagnostics. π«Ά
β¨ NAME β¨
Test2::Util::Table β Format a header and rows into a table.
πΈ DESCRIPTION πΈ
This module is used by some failing tests to give diagnostics about what has gone wrong. Itβs able to format rows of data into tables in a generic way. No cap. π
π 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;
This prints a table like this:
+-----------------+---------+------------+
| name | age | hair color |
+-----------------+---------+------------+
| Fred Flinstone | 2000000 | black |
| Wilma Flinstone | 1999995 | red |
| ... | ... | ... |
+-----------------+---------+------------+
π¦ EXPORTS π¦
β¨ @rows = table(...) β¨
The function returns a list of lines, and those lines do not have the newline "\n" character appended. Slay.
Options:
header => [ ... ]β If you want a header, specify it here. This takes an arrayref with each columnβs heading.rows => [ [...], [...], ... ]β This should be an arrayref containing an arrayref per row. πcollapse => $boolβ Use this if you want to hide empty columns β that is, any column that has no data in any row. Having a header for the column will not affect collapse.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 will attempt to find the width of your terminal and use that; otherwise it falls back to80.sanitize => $boolβ This will sanitize all the data in the table such that newlines, control characters, and all whitespace except for ASCII20' 'are replaced with escape sequences. This prevents newlines, tabs, and similar whitespace from disrupting the 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 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. Obsessed. π
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. π
π 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.
π§ 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 |
+-----+-----+---+
π SOURCE π
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/.
π MAINTAINERS π
- Chad Granum
<exodist@cpan.org>
β AUTHORS β
- Chad Granum
<exodist@cpan.org>
πΈ COPYRIGHT πΈ
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/
Okay bestie, thatβs the whole thing. This page is from perl v5.42.0, dated 2025-07-28, and itβs Test2::Util::Table(3) β love that for us. Bye! π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms