NAME

Text::Table::Tiny - generate simple text tables from 2D arrays

SYNOPSIS

use Text::Table::Tiny 0.04 qw/ generate_table /;

my $rows = [
    ['Name',  'Rank',     'Serial'],    # header row
    ['alice', 'pvt',      '123456'],    # body rows
    ['bob',   'cpl',      '98765321'],
    ['carol', 'brig gen', '8745'],
];
print generate_table(rows => $rows, header_row => 1);

DESCRIPTION

This module provides a single function, generate_table, which formats a two-dimensional array of data as a text table. There are a number of options for adjusting the output format, but the intention is that the default option is good enough for most uses.

The example shown in the SYNOPSIS generates the following table:

+-------+----------+----------+
| Name  | Rank     | Serial   |
+-------+----------+----------+
| alice | pvt      | 123456   |
| bob   | cpl      | 98765321 |
| carol | brig gen | 8745     |
+-------+----------+----------+

NOTE: the interface changed with version 0.04, so if you use the generate_table() function illustrated above, then you need to require at least version 0.04 of this module, as shown in the SYNOPSIS.

generate_table()

The generate_table function understands a number of arguments, which are passed as a hash. The only required argument is rows.

  • rows

    Takes an array reference which should contain one or more rows of data, where each row is an array reference.

  • header_row

    If given a true value, the first row in the data will be interpreted as a header row, and separated from the rest of the table with a ruled line.

  • separate_rows

    If given a true value, a separator line will be drawn between every row in the table, and a thicker line will be used for the header separator.

  • top_and_tail

    If given a true value, then the top and bottom border lines will be skipped. This reduces the vertical height of the generated table.

  • align

    This takes an array ref with one entry per column, to specify the alignment of that column. Legal values are 'l', 'c', and 'r'.

  • style

    Specifies the format of the output table. The default is 'classic', but other options are 'boxrule' and 'norule'.

  • compact

    If set to a true value then we omit the single space padding on either side of every column.

EXAMPLES

If you just pass the data and no other options:

generate_table(rows => $rows);

You get minimal ruling:

+-------+----------+----------+
| Name  | Rank     | Serial   |
| alice | pvt      | 123456   |
| bob   | cpl      | 98765321 |
| carol | brig gen | 8745     |
+-------+----------+----------+

If you want lines between every row, and also want a separate header:

generate_table(rows => $rows, header_row => 1, separate_rows => 1);

You get the maximally ornate:

+-------+----------+----------+
| Name  | Rank     | Serial   |
O=======O==========O==========O
| alice | pvt      | 123456   |
+-------+----------+----------+
| bob   | cpl      | 98765321 |
+-------+----------+----------+
| carol | brig gen | 8745     |
+-------+----------+----------+

SEE ALSO

My blog post where I described changes to formatting; this has more examples.

There are many modules for formatting text tables on CPAN. A good number of them are listed in the See Also section of the documentation for Text::Table::Manifold.

REPOSITORY

https://github.com/neilb/Text-Table-Tiny

AUTHOR

Neil Bowers <neilb@cpan.org>

The original version was written by Creighton Higgins <chiggins@chiggins.com>, but the module was entirely rewritten for 0.05_01.

COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by Neil Bowers.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.