.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "TabularDisplay 3pm" .TH TabularDisplay 3pm "2022-10-22" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Text::TabularDisplay \- Display text in formatted table output .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Text::TabularDisplay; \& \& my $table = Text::TabularDisplay\->new(@columns); \& $table\->add(@row) \& while (@row = $sth\->fetchrow); \& print $table\->render; \& \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | id | name | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | 1 | Tom | \& | 2 | Dick | \& | 3 | Barry | \& | | (aka Bazza) | \& | 4 | Harry | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Text::TabularDisplay simplifies displaying textual data in a table. The output is identical to the columnar display of query results in the mysql text monitor. For example, this data: .PP .Vb 3 \& 1, "Tom Jones", "(666) 555\-1212" \& 2, "Barnaby Jones", "(666) 555\-1213" \& 3, "Bridget Jones", "(666) 555\-1214" .Ve .PP Used like so: .PP .Vb 5 \& my $t = Text::TabularDisplay\->new(qw(id name phone)); \& $t\->add(1, "Tom Jones", "(666) 555\-1212"); \& $t\->add(2, "Barnaby Jones", "(666) 555\-1213"); \& $t\->add(3, "Bridget Jones", "(666) 555\-1214"); \& print $t\->render; .Ve .PP Produces: .PP .Vb 7 \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | id | name | phone | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | 1 | Tom Jones | (666) 555\-1212 | \& | 2 | Barnaby Jones | (666) 555\-1213 | \& | 3 | Bridget Jones | (666) 555\-1214 | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .Ve .SH "METHODS" .IX Header "METHODS" Text::TabularDisplay has four primary methods: \fBnew()\fR, \fBcolumns()\fR, \&\fBadd()\fR, and \fBrender()\fR. \fBnew()\fR creates a new Text::TabularDisplay instance; \fBcolumns()\fR sets the column headers in the output table; \&\fBadd()\fR adds data to the instance; and \fBrender()\fR returns a formatted string representation of the instance. .PP There are also a few auxiliary convenience methods: \fBclone()\fR, \fBitems()\fR, \&\fBreset()\fR, \fBpopulate()\fR, and \fBpaginate()\fR. .IP "\fBnew\fR" 4 .IX Item "new" A Text::TabularDisplay instance can be created with column names passed as constructor args, so these two calls produce similar objects: .Sp .Vb 2 \& my $t1 = Text::TabularDisplay\->new; \& $t1\->columns(qw< one two >); \& \& my $t2 = Text::TabularDisplay\->new(qw< one two >); .Ve .Sp Calling \fBnew()\fR on a Text::TabularDisplay instance returns a clone of the object. See \*(L"clone\*(R" in Text::TabularDisplay. .IP "\fBcolumns\fR" 4 .IX Item "columns" Gets or sets the column names for an instance. This method is called automatically by the constructor with any parameters that are passed to the constructor (if any are passed). .Sp When called in scalar context, \fBcolumns()\fR returns the \fInumber of columns in the instance\fR, rather than the columns themselves. In list context, copies of the columns names are returned; the names of the columns cannot be modified this way. .IP "\fBadd\fR" 4 .IX Item "add" Takes a list of items and appends it to the list of items to be displayed. \fBadd()\fR can also take a reference to an array, so that large arrays don't need to be copied. .Sp As elements are processed, \fBadd()\fR maintains the width of each column so that the resulting table has the correct dimensions. .Sp \&\fBadd()\fR returns \f(CW$self\fR, so that calls to \fBadd()\fR can be chained: .Sp .Vb 1 \& $t\->add(@one)\->add(@two)\->add(@three); .Ve .IP "\fBrender\fR" 4 .IX Item "render" \&\fBrender()\fR does most of the actual work. It returns a string containing the data added via \fBadd()\fR, formatted as a table, with a header containing the column names. .Sp \&\fBrender()\fR does not change the state of the object; it can be called multiple times, with identical output (including identical running time: the output of render is not cached). .Sp If there are no columns defined, then the output table does not contains a row of column names. Compare these two sequences: .Sp .Vb 4 \& my $t = Text::TabularDisplay\->new; \& $t\->add(qw< 1 2 3 4 >); \& $t\->add(qw< 5 6 7 8 >); \& print $t\->render; \& \& $t\->columns(qw< one two three four >); \& print $t\->render; \& \& # Example 1 output \& +\-\-\-+\-\-\-+\-\-\-+\-\-\-+ \& | 1 | 2 | 3 | 4 | \& | 5 | 6 | 7 | 8 | \& +\-\-\-+\-\-\-+\-\-\-+\-\-\-+ \& \& # Example 2 output \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ \& | one | two | three | four | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ \& | 1 | 2 | 3 | 4 | \& | 5 | 6 | 7 | 8 | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ .Ve .Sp \&\fBrender()\fR takes optional \f(CW$start\fR and \f(CW$end\fR arguments; these indicate the start and end \fIindexes\fR for the data to be rendered. This can be used for paging and the like: .Sp .Vb 3 \& $t\->add(1, 2, 3)\->add(4, 5, 6)\->add(7, 8, 9)\->add(10, 11, 12); \& print $t\->render(0, 1), "\en"; \& print $t\->render(2, 3), "\en"; .Ve .Sp Produces: .Sp .Vb 6 \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | First | Second | Third | \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | 1 | 2 | 3 | \& | 4 | 5 | 6 | \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | First | Second | Third | \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | 7 | 8 | 9 | \& | 10 | 11 | 12 | \& +\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ .Ve .Sp As an aside, note the chaining of calls to \fBadd()\fR. .Sp The elements in the table are padded such that there is the same number of items in each row, including the header. Thus: .Sp .Vb 2 \& $t\->columns(qw< One Two >); \& print $t\->render; \& \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-+ \& | One | Two | | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-+ \& | 1 | 2 | 3 | \& | 4 | 5 | 6 | \& | 7 | 8 | 9 | \& | 10 | 11 | 12 | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-+ .Ve .Sp And: .Sp .Vb 2 \& $t\->columns(qw< One Two Three Four>); \& print $t\->render; \& \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ \& | One | Two | Three | Four | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ \& | 1 | 2 | 3 | | \& | 4 | 5 | 6 | | \& | 7 | 8 | 9 | | \& | 10 | 11 | 12 | | \& +\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-+ .Ve .SH "OTHER METHODS" .IX Header "OTHER METHODS" .IP "\fBclone()\fR" 4 .IX Item "clone()" The \fBclone()\fR method returns an identical copy of a Text::TabularDisplay instance, completely separate from the cloned instance. .IP "\fBitems()\fR" 4 .IX Item "items()" The \fBitems()\fR method returns the number of elements currently stored in the data structure: .Sp .Vb 1 \& printf "There are %d elements in \e$t.\en", $t\->items; .Ve .IP "\fBreset()\fR" 4 .IX Item "reset()" Reset deletes the data from the instance, including columns. If passed arguments, it passes them to \fBcolumns()\fR, just like \fBnew()\fR. .IP "\fBpopulate()\fR" 4 .IX Item "populate()" \&\fBpopulate()\fR as a special case of \fBadd()\fR; \fBpopulate()\fR expects a reference to an array of references to arrays, such as returned by \s-1DBI\s0's selectall_arrayref method: .Sp .Vb 3 \& $sql = "SELECT " . join(", ", @c) . " FROM mytable"; \& $t\->columns(@c); \& $t\->populate($dbh\->selectall_arrayref($sql)); .Ve .Sp This is for convenience only; the implementation maps this to multiple calls to \fBadd()\fR. .SH "NOTES / ISSUES" .IX Header "NOTES / ISSUES" Text::TabularDisplay assumes it is handling strings, and does stringy things with the data, like \fBlength()\fR and \fBsprintf()\fR. Non-character data can be passed in, of course, but will be treated as strings; this may have ramifications for objects that implement overloading. .PP The biggest issue, though, is that this module duplicates a some of the functionality of Data::ShowTable. Of course, Data::ShowTable is a large, complex monolithic tool that does a lot of things, while Text::TabularDisplay is small and fast. .SH "AUTHOR" .IX Header "AUTHOR" darren chamberlain .SH "CREDITS" .IX Header "CREDITS" The following people have contributed patches, suggestions, tests, feedback, or good karma: .PP .Vb 10 \& David N. Blank\-Edelman \& Eric Cholet \& Ken Youens\-Clark \& Michael Fowler \& Paul Cameron \& Prakash Kailasa \& Slaven Rezic \& Harlan Lieberman\-Berg \& Patrick Kuijvenhoven \& Miko O\*(AqSullivan .Ve .SH "VERSION" .IX Header "VERSION" This documentation describes \f(CW\*(C`Text::TabularDisplay\*(C'\fR version 1.38.