|
NAMEText::Table - Organize Data in TablesVERSIONversion 1.134SYNOPSISuse Text::Table; my $tb = Text::Table->new( "Planet", "Radius\nkm", "Density\ng/cm^3" ); $tb->load( [ "Mercury", 2360, 3.7 ], [ "Venus", 6110, 5.1 ], [ "Earth", 6378, 5.52 ], [ "Jupiter", 71030, 1.3 ], ); print $tb; This prints a table from the given title and data like this: Planet Radius Density km g/cm^3 Mercury 2360 3.7 Venus 6110 5.1 Earth 6378 5.52 Jupiter 71030 1.3 Note that two-line titles work, and that the planet names are aligned differently than the numbers. DESCRIPTIONOrganization of data in table form is a time-honored and useful method of data representation. While columns of data are trivially generated by computer through formatted output, even simple tasks like keeping titles aligned with the data columns are not trivial, and the one-shot solutions one comes up with tend to be particularly hard to maintain. Text::Table allows you to create and maintain tables that adapt to alignment requirements as you use them.OverviewThe process is simple: you create a table (a Text::Table object) by describing the columns the table is going to have. Then you load lines of data into the table, and finally print the resulting output lines. Alignment of data and column titles is handled dynamically in dependence on the data present.Table CreationIn the simplest case, if all you want is a number of (untitled) columns, you create an unspecified table and start adding data to it. The number of columns is taken from the first line of data.To specify a table you specify its columns. A column description can contain a title and alignment requirements for the data, both optional. Additionally, you can specify how the title is aligned with the body of a column, and how the lines of a multiline title are aligned among themselves. The columns are collected in the table in the order they are given. On data entry, each column corresponds to one data item, and in column selection columns are indexed left to right, starting from 0. Each title can be a multiline string which will be blank-filled to the length of the longest partial line. The largest number of title lines in a column determines how many title lines the table has as a whole, including the case that no column has any titles. On output, columns are separated by a single blank. You can control what goes between columns by specifying separators between (or before, or after) columns. Separators don't contain any data and don't count in column indexing. They also don't accumulate: in a sequence of only separators and no columns, only the last one counts. Status InformationThe width (in characters), height (in lines), number of columns, and similar data about the table is available.Data LoadingTable data is entered line-wise, each time specifying data entries for all table columns. A bulk loader for many lines at once is also available. You can clear the data from the table for re-use (though you will more likely just create another table).Data can contain colorizing escape sequences (as provided by "Term::AnsiColor") without upsetting the alignment. Table OutputThe output area of a table is divided in the title and the body.The title contains the combined titles from the table columns, if any. Its content never changes with a given table, but it may be spread out differently on the page through alignment with the data. The body contains the data lines, aligned column-wise as specified, and left-aligned with the column title. Each of these is arranged like a Perl array (counting from 0) and can be accessed in portions by specifying a first line and the number of following lines. Also like an array, giving a negative first line counts from the end of the area. The whole table, the title followed by the body, can also be accessed in this manner. The subdivisions are there so you can repeat the title (or parts of it) along with parts of the body on output, whether for screen paging or printout. A rule line is also available, which is the horizontal counterpart to the separator columns you specify with the table. It is basically a table line as it would appear if all data entries in the line were empty, that is, a blank line except for where the column separators have non-blank entries. If you print it between data lines, it will not disrupt the vertical separator structure as a plain blank line would. You can also request a solid rule consisting of any character, and even one with the non-blank column separators replaced by a character of your choice. This way you can get the popular representation of line-crossings like so: | ----+--- | Warning ControlOn table creation, some parameters are checked and warnings issued if you allow warnings. You can also turn warnings into fatal errors.VERSIONversion 1.134SPECIFICATIONSColumn SpecificationEach column specification is a single scalar. Columns can be either proper data columns or column separators. Both can be specified either as (possibly multi-line) strings, or in a more explicit form as hash-refs. In the string form, proper columns are given as plain strings, and separators are given as scalar references to strings. In hash form, separators have a true value in the field "is_sep" while proper columns don't have this field.
AlignmentThe original documentation to Text::Aligner contains all the details on alignment specification, but here is the rundown:The possible alignment specifications are left, right, center, num and point (which are synonyms), and auto. The first three explain themselves. num (and point) align the decimal point in the data, which is assumed to the right if none is present. Strings that aren't numbers are treated the same way, that is, they appear aligned with the integers unless they contain a ".". Instead of the decimal point ".", you can also specify any other string in the form num(,), for instance. The string in parentheses is aligned in the data. The synonym point for num may be more appropriate in contexts that deal with arbitrary strings, as in point(=>) (which might be used to align certain bits of Perl code). regex alignment is a more sophisticated form of point alignment. If you specify a regular expression, as delivered by "qr//", the start of the match is used as the alignment point. If the regex contains capturing parentheses, the last submatch counts. [The usefulness of this feature is under consideration.] auto alignment combines numeric alignment with left alignment. Data items that look like numbers, and those that don't, form two virtual columns and are aligned accordingly: "num" for numbers and "left" for other strings. These columns are left-aligned with each other (i.e. the narrower one is blank-filled) to form the final alignment. This way, a column that happens to have only numbers in the data gets num alignment, a column with no numbers appears left-aligned, and mixed data is presented in a reasonable way. Column SelectionBesides creating tables from scratch, they can be created by selecting columns from an existing table. Tables created this way contain the data from the columns they were built from.This is done by specifying the columns to select by their index (where negative indices count backward from the last column). The same column can be selected more than once and the sequence of columns can be arbitrarily changed. Separators don't travel with columns, but can be specified between the columns at selection time. You can make the selection of one or more columns dependent on the data content of one of them. If you specify some of the columns in angle brackets [...], the whole group is only included in the selection if the first column in the group contains any data that evaluates to boolean true. That way you can de-select parts of a table if it contains no interesting data. Any column separators given in brackets are selected or deselected along with the rest of it. PUBLIC METHODSTable Creation
Status Information
Data Loading
Table OutputThe three methods "table()", "title()", and "body()" are very similar. They access different parts of the printable output lines of a table with similar methods. The details are described with the "table()" method.
Warning Control
VERSIONThis document pertains to Text::Table version 1.127BUGS
AUTHORMAINTAINERShlomi Fish, <http://www.shlomifish.org/> - CPAN ID: "SHLOMIF".ORIGINAL AUTHORAnno Siegel CPAN ID: ANNO siegel@zrz.tu-berlin.de http://www.tu-berlin.de/~siegel COPYRIGHTCopyright (c) 2002 Anno Siegel. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the ISC license.(This program had been licensed under the same terms as Perl itself up to version 1.118 released on 2011, and was relicensed by permission of its originator). The full text of the license can be found in the LICENSE file included with this module. SEE ALSOText::Aligner, perl(1) .EXAMPLEScenter align and Unicode output#!/usr/bin/perl use strict; use warnings; use utf8; use Text::Table (); binmode STDOUT, ':encoding(utf8)'; my @cols = qw/First Last/; push @cols, +{ title => "Country", align => "center", }; my $sep = \'│'; my $major_sep = \'║'; my $tb = Text::Table->new( $sep, " Number ", $major_sep, ( map { +( ( ref($_) ? $_ : " $_ " ), $sep ) } @cols ) ); my $num_cols = @cols; $tb->load( [ 1, "Mark", "Twain", "USA", ] ); $tb->load( [ 2, "Charles", "Dickens", "Great Britain", ] ); $tb->load( [ 3, "Jules", "Verne", "France", ] ); my $make_rule = sub { my ($args) = @_; my $left = $args->{left}; my $right = $args->{right}; my $main_left = $args->{main_left}; my $middle = $args->{middle}; return $tb->rule( sub { my ( $index, $len ) = @_; return ( '─' x $len ); }, sub { my ( $index, $len ) = @_; my $char = ( ( $index == 0 ) ? $left : ( $index == 1 ) ? $main_left : ( $index == $num_cols + 1 ) ? $right : $middle ); return $char x $len; }, ); }; my $start_rule = $make_rule->( { left => '┌', main_left => '╥', right => '┐', middle => '┬', } ); my $mid_rule = $make_rule->( { left => '├', main_left => '╫', right => '┤', middle => '┼', } ); my $end_rule = $make_rule->( { left => '└', main_left => '╨', right => '┘', middle => '┴', } ); print $start_rule, $tb->title, ( map { $mid_rule, $_, } $tb->body() ), $end_rule; This emits the following output: ┌────────╥───────┬───────┬─────────────┐ │ Number ║ First │ Last │Country │ ├────────╫───────┼───────┼─────────────┤ │1 ║Mark │Twain │ USA │ ├────────╫───────┼───────┼─────────────┤ │2 ║Charles│Dickens│Great Britain│ ├────────╫───────┼───────┼─────────────┤ │3 ║Jules │Verne │ France │ └────────╨───────┴───────┴─────────────┘ SUPPORTWebsitesThe following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
Bugs / Feature RequestsPlease report any bugs or feature requests by email to "bug-text-table at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Text-Table>. You will be automatically notified of any progress on the request by the system.Source CodeThe code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)<https://github.com/shlomif/Text-Table> git clone git://github.com/shlomif/Text-Table.git AUTHORShlomi Fish <shlomif@cpan.org>BUGSPlease report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/Text-Table/issues>When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. COPYRIGHT AND LICENSEThis software is Copyright (c) 2002 by Anno Siegel and others.This is free software, licensed under: The ISC License SUPPORTWebsitesThe following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
Bugs / Feature RequestsPlease report any bugs or feature requests by email to "bug-text-table at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Text-Table>. You will be automatically notified of any progress on the request by the system.Source CodeThe code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)<https://github.com/shlomif/Text-Table> git clone git://github.com/shlomif/Text-Table.git AUTHORShlomi Fish <shlomif@cpan.org>BUGSPlease report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/Text-Table/issues>When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. COPYRIGHT AND LICENSEThis software is Copyright (c) 2002 by Anno Siegel and others.This is free software, licensed under: The ISC License
Visit the GSP FreeBSD Man Page Interface. |