HTML::Table(3pm)
HTML::Table π
the HTML::Table manual, bestie edition β it's a User Contributed Perl Documentation page and it produces HTML tables, no cap.
β¨ NAME β¨
HTML::Table β produces HTML tables π
β¨ SYNOPSIS β¨
okay so here's the whole vibe, literally:
use HTML::Table;
$table1 = new HTML::Table($rows, $cols);
or
$table1 = new HTML::Table(-rows=>26,
-cols=>2,
-align=>'center',
-rules=>'rows',
-border=>0,
-bgcolor=>'blue',
-width=>'50%',
-spacing=>0,
-padding=>0,
-style=>'color: blue',
-class=>'myclass',
-evenrowclass=>'even',
-oddrowclass=>'odd',
-head=> ['head1', 'head2'],
-data=> [ ['1:1', '1:2'], ['2:1', '2:2'] ] );
or
$table1 = new HTML::Table( [ ['1:1', '1:2'], ['2:1', '2:2'] ] );
$table1->setCell($cellrow, $cellcol, 'This is Cell 1');
$table1->setCellBGColor('blue');
$table1->setCellColSpan(1, 1, 2);
$table1->setRowHead(1);
$table1->setColHead(1);
$table1->print;
$table2 = new HTML::Table;
$table2->addRow(@cell_values);
$table2->addCol(@cell_values2);
$table1->setCell(1,1, "$table2->getTable");
$table1->print;
β¨ REQUIRES β¨
Perl5.002 β yes she's old, she's still iconic π
β¨ EXPORTS β¨
Nothing. literally nothing. not one thing. π«Ά
β¨ DESCRIPTION β¨
So HTML::Table is what you use to generate HTML tables for CGI scripts. Using the methods it hands you, you can create and manipulate fairly complex tables and then print them straight out of Perl scripts. And it makes nesting tables inside tables β from Perl, bestie, not from a nightmare pile of tags β so much easier. You can build a whole entire table with the methods it gives you and never touch an HTML tag. not even once. πΈ
HTML::Table also does dynamically sized tables through its addRow and addCol methods. These two? they auto-resize the table if you pass them more cell values than fit in the current table grid. she just grows. π
There are methods for nearly all valid table, row, and cell tags specified for HTML 3.0.
Also there's a Japanese translation of the documentation, right here:
http://member.nifty.ne.jp/hippo2000/perltips/html/table.htm
β¨ METHODS β¨
quick housekeeping before we dive in, iykyk:
[] indicate optional parameters. default value will
be used if no value is specified
row_num indicates that a row number is required.
Rows are numbered from 1. To refer to the last row use the value -1.
col_num indicates that a col number is required.
Cols are numbered from 1. To refer to the last col use the value -1.
Sections
From version 2.07 onwards, HTML::Table supports table sections: THEAD, TFOOT & TBODY. β¨
Every section can have its own attributes (id, class, etc.) set, and each one holds 1 or more rows. Section numbering starts at 0, and only tbody is allowed to have more than one section. (yes really, tbody is the main character here.)
Methods for manipulating sections and their data exist and follow this general shape:
setSectionCell ( section, section_num, row_num, col_num, data );
Like, for example, this adds a row to the first body section:
addSectionRow ( 'tbody', 0, "Cell 1", "Cell 2", "Cell 3" );
For backwards compatibility, methods with Section in their name will default to manipulating the first body section.
So like, this sets the class for the first row in the first body section:
setRowClass ( 1, 'row_class' );
which is semantically equivalent to:
setSectionRowClass ( 'tbody', 0, 1, 'row_class' );
Creation
new HTML::Table([num_rows, num_cols])β makes a brand new HTML table object. If you specify rows and columns, the table gets initialized to that size. Row and Column numbers start at1,1. And0,0is considered an empty table. π«§new HTML::Table([-rows=>num_rows, -cols=>num_cols, -border=>border_width, -align=>table_alignment, -style=>table_style, -class=>table_class, -evenrowclass=>'even', -oddrowclass=>'odd', -bgcolor=>back_colour, -width=>table_width, -spacing=>cell_spacing, -padding=>cell_padding])β same deal, makes a new HTML table object, and if rows and columns are specified the table gets initialized to that size. Row and Column numbers start at1,1.0,0is considered an empty table.
and listen: if evenrowclass or oddrowclass is specified, those classes get applied to even and odd rows respectively β unless those rows have a specific class applied to them already. so don't come for her. π
Table Level Methods
setBorder([pixels])β sets the table Border Width. πΌοΈsetWidth([pixels|percentofscreen])β sets the table width.
$table->setWidth(500);
or
$table->setWidth('100%');
setCellSpacing([pixels])β spacing between your cells bestie. πsetCellPadding([pixels])β padding inside your cells. π§ΈsetCaption("CaptionText" [, top|bottom])β gives the table a caption, and you can picktoporbottom.setBGColor([colorname|colortriplet])β background colour, name or triplet. π¨autoGrow([1|true|on|anything|0|false|off|no|disable])β switches on (that's the default) or off the automatic growing of the table when row or column values passed tosetCellexceed the current table size. she grows when told. π±setAlign ( [ left , center , right ] )β sets the table alignment.setRules ( [ rows , cols , all, both , groups ] )β sets the table rules.setStyle ( 'css style' )β sets the tablestyleattribute. β¨setClass ( 'css class' )β sets the tableclassattribute.setEvenRowClass ( 'css class' )β sets theclassattribute of even rows in the table.setOddRowClass ( 'css class' )β sets theclassattribute of odd rows in the table.setAttr ( 'user attribute' )β sets a user defined attribute for the table. super useful for whenHTML::Tablehasn't implemented a particular attribute yet. πsort ( [sort_col_num, sort_type, sort_order, num_rows_to_skip] )
or
sort( -sort_col => sort_col_num,
-sort_type => sort_type,
-sort_order => sort_order,
-skip_rows => num_rows_to_skip,
-strip_html => strip_html,
-strip_non_numeric => strip_non_numeric,
-presort_func => \&filter_func )
sort_type in { ALPHA | NUMERIC },
sort_order in { ASC | DESC },
strip_html in { 0 | 1 }, defaults to 1,
strip_non_numeric in { 0 | 1 }, defaults to 1
Sorts all rows on a given column (and you can optionally skip table header rows by specifying num_rows_to_skip). π€
By default sorting ignores HTML Tags and  , and setting the strip_html parameter to 0 disables this behaviour.
By default numeric Sorting ignores non numeric chararacters, and setting the strip_non_numeric parameter to 0 disables this behaviour.
You can provide your own pre-sort function β genuinely so useful for pre-processing the cell contents before sorting, like, say, dates. ποΈ
getTableRowsβ returns the number of rows in the table. π’getTableColsβ returns the number of columns in the table. π’getStyleβ returns the table'sstyleattribute.
Section Level Methods
setSectionId ( [tbody|thead|tfoot], section_num, 'id' )β sets theidattribute for the section.setSectionClass ( [tbody|thead|tfoot], section_num, 'class' )β sets theclassattribute for the section.setSectionStyle ( [tbody|thead|tfoot], section_num, 'style' )β sets thestyleattribute for the section.setSectionAlign ( [tbody|thead|tfoot], section_num, [center|right|left] )β sets the horizontal alignment for the section.setSectionValign ( [tbody|thead|tfoot], section_num, [center|top|bottom|middle|baseline] )β sets the vertical alignment for the section.setSectionAttr ( [tbody|thead|tfoot], section_num, 'user attribute' )β sets a user defined attribute for the cell. useful for whenHTML::Tablehasn't implemented a particular attribute yet. π«Ά
Cell Level Methods
setCell(row_num, col_num, "content")β sets the content of a table cell. This could be any string, even another table object via thegetTablemethod. And if the row and/or column numbers are outside the existing table boundaries, extra rows and/or columns get created automatically. π±setSectionCell([tbody|thead|tfoot], section_num, row_num, col_num, "content")β same assetCell, but you get to specify which section to act on.setCellAlign(row_num, col_num, [center|right|left])β sets the horizontal alignment for the cell.setSectionCellAlign([tbody|thead|tfoot], section_num, row_num, col_num, [center|right|left])β same assetCellAlign, but you get to specify which section to act on.setCellVAlign(row_num, col_num, [center|top|bottom|middle|baseline])β sets the vertical alignment for the cell.setSectionCellVAlign([tbody|thead|tfoot], section_num, row_num, col_num, [center|top|bottom|middle|baseline])β same assetCellVAlign, but you get to specify which section to act on.setCellWidth(row_num, col_num, [pixels|percentoftable])β sets the width of the cell.setSectionCellWidth([tbody|thead|tfoot], section_num, row_num, col_num, [pixels|percentoftable])β same assetCellWidth, but you get to specify which section to act on.setCellHeight(row_num, col_num, [pixels])β sets the height of the cell.setSectionCellHeight([tbody|thead|tfoot], section_num, row_num, col_num, [pixels])β same assetCellHeight, but you get to specify which section to act on.setCellHead(row_num, col_num, [0|1])β sets cell to be of type head (Ie<th></th>). πsetSectionCellHead([tbody|thead|tfoot], section_num, row_num, col_num, [0|1])β same assetCellHead, but you get to specify which section to act on.setCellNoWrap(row_num, col_num, [0|1])β sets the NoWrap attribute of the cell.setSectionCellNoWrap([tbody|thead|tfoot], section_num, row_num, col_num, [0|1])β same assetCellNoWrap, but you get to specify which section to act on.setCellBGColor(row_num, col_num, [colorname|colortriplet])β sets the background colour for the cell. π¨setSectionCellBGColor([tbody|thead|tfoot], section_num, row_num, col_num, [colorname|colortriplet])β same assetCellBGColor, but you get to specify which section to act on.setCellRowSpan(row_num, col_num, num_cells)β causes the cell to overlap a number of cells below it. If the overlap number is greater than number of cells below the cell, a false value will be returned. πsetSectionCellRowSpan([tbody|thead|tfoot], section_num, row_num, col_num, num_cells)β same assetCellRowSpan, but you get to specify which section to act on.setCellColSpan(row_num, col_num, num_cells)β causes the cell to overlap a number of cells to the right. If the overlap number is greater than number of cells to the right of the cell, a false value will be returned. πsetSectionCellColSpan([tbody|thead|tfoot], section_num, row_num, col_num, num_cells)β same energy assetCellColSpan, except you also get to say which section you're acting on πsetCellSpan(row_num, col_num, num_rows, num_cols)β merges a whole block of cells together, starting at the cell you point at. The joined area ends upnum_colswide andnum_rowsdeep. one cell, but make it huge β¨setSectionCellSpan([tbody|thead|tfoot], section_num, row_num, col_num, num_rows, num_cols)βsetCellSpanbut you pick the section, bestie π«ΆsetCellFormat(row_num, col_num, start_string, end_string)βstart_stringhas to be a string of valid HTML, and it gets output before the cell contents.end_stringis valid HTML that gets output after the cell contents. Boom, now you can format whatever's inside the cell π
$table->setCellFormat(1, 2, '<b>', '</b>');
setSectionCellFormat([tbody|thead|tfoot], section_num, row_num, col_num, start_string, end_string)βsetCellFormatwith section-picking powers β¨setCellStyle (row_num, col_num, 'css style')β sets the cell's style attribute πsetSectionCellStyle([tbody|thead|tfoot], section_num, row_num, col_num, 'css style')βsetCellStyle, but you choose the section πΈsetCellClass (row_num, col_num, 'css class')β sets the cell's class attribute πsetSectionCellClass([tbody|thead|tfoot], section_num, row_num, col_num, 'css class')βsetCellClass, but section-selectable π¦setCellAttr (row_num, col_num, 'user attribute')β sets a user defined attribute on the cell. super handy for whenHTML::Tablehasn't implemented a particular attribute yet, so you just do it yourself πsetSectionCellAttr([tbody|thead|tfoot], section_num, row_num, col_num, 'css class')βsetCellAttr, but you can say which section it lands in πsetLastCell*β every single one of thesetCellmethods has a matchingsetLastCellmethod. These don't takerow_numandcol_numat all β they just automatically apply to the last row and last col of the table. lazy girl method, and honestly? iconic π
NB. Only works on thesetCell*methods, not on thesetSectionCell*methods. Don't try it, it's not gonna go π«
getCell(row_num, col_num)β hands you back the contents of the cell you asked for, as a string π¦getSectionCell([tbody|thead|tfoot], section_num, row_num, col_num)βgetCell, but with section selection π«getCellStyle(row_num, col_num)β returns that cell's style attribute β¨getSectionCellStyle([tbody|thead|tfoot], section_num, row_num, col_num)βgetCellStyle, but section-specified π
πΈ Column Level Methods πΈ
addCol("cell 1 content" [, "cell 2 content", ...])β adds a column to the right end of the table. And listen, it assumes that if you pass more values than there are rows, you're lowkey asking it to increase the number of rows for you πaddSectionCol([tbody|thead|tfoot], section_num, "cell 1 content" [, "cell 2 content", ...])βaddColbut you get to specify which section to act on πsetColAlign(col_num, [center|right|left])β runssetCellAlignover the entire column, no cap π«ΆsetSectionColAlign([tbody|thead|tfoot], section_num, col_num, [center|right|left])βsetColAlignbut able to specify which section to act on β¨setColVAlign(col_num, [center|top|bottom|middle|baseline])β runssetCellVAlignover the entire column πsetSectionColVAlign([tbody|thead|tfoot], section_num, col_num, [center|top|bottom|middle|baseline])βsetColVAlignbut able to specify which section to act on πsetColWidth(col_num, [pixels|percentoftable])β runssetCellWidthover the entire column πsetSectionColWidth([tbody|thead|tfoot], section_num, col_num, [pixels|percentoftable])βsetColWidthbut able to specify which section to act on π¦setColHeight(col_num, [pixels])β runssetCellHeightover the entire column πsetSectionColHeight([tbody|thead|tfoot], section_num, col_num, [pixels])βsetColHeightbut able to specify which section to act on πΈsetColHead(col_num, [0|1])β runssetCellHeadover the entire column πsetSectionColHead([tbody|thead|tfoot], section_num, col_num, [0|1])βsetColHeadbut able to specify which section to act on βsetColNoWrap(col_num, [0|1])β runssetCellNoWrapover the entire column π«setSectionColNoWrap([tbody|thead|tfoot], section_num, col_num, [0|1])βsetColNoWrapbut able to specify which section to act on πsetColBGColor(row_num, [colorname|colortriplet])β runssetCellBGColorover the entire column π¨setSectionColBGColor([tbody|thead|tfoot], section_num, col_num, [colorname|colortriplet])βsetColBGColorbut able to specify which section to act on πsetColFormat(col_num, start_string, end_sting)β runssetCellFormatover the entire column πsetSectionColFormat([tbody|thead|tfoot], section_num, col_num, start_string, end_sting)βsetColFormatbut able to specify which section to act on πsetColStyle (col_num, 'css style')β runssetCellStyleover the entire column β¨setSectionColStyle([tbody|thead|tfoot], section_num, col_num, 'css style')βsetColStylebut able to specify which section to act on π«ΆsetColClass (col_num, 'css class')β runssetCellClassover the entire column πsetSectionColClass([tbody|thead|tfoot], section_num, col_num, 'css class')βsetColClassbut able to specify which section to act on πΈsetColAttr (col_num, 'user attribute')β runssetCellAttrover the entire column π¦setSectionColAttr([tbody|thead|tfoot], section_num, col_num, 'user attribute')βsetColAttrbut able to specify which section to act on βsetLastCol*β everysetColmethod has a matchingsetLastColmethod. It doesn't take acol_numparameter at all β it just automatically applies to the last col of the table. auto-pilot, we love her π«
NB. Only works on thesetCol*methods, not on thesetSectionCol*methods. Fr fr, don't π
getColStyle(col_num)β returns the column's style attribute. Ngl it's only really useful after you've set a column's style viasetColStyle()πgetSectionColStyle([tbody|thead|tfoot], section_num, col_num)βgetColStylebut able to specify which section to act on π
π Row Level Methods π
addRow("cell 1 content" [, "cell 2 content", ...])β adds a row to the bottom of the first body section of the table.
It also adds a row to the bottom of the table. And it assumes that if you pass more values than there are columns, you want the number of columns increased π
addSectionRow([tbody|thead|tfoot], section_num, "cell 1 content" [, "cell 2 content", ...])βaddRowbut able to specify which section to act on π«ΆdelRow(row_num)β deletes a row from the first body section of the table. And if you pass-1asrow_num, the last row in the section gets deleted. bye bestie πdelSectionRow([tbody|thead|tfoot], section_num, row_num)βdelRowbut able to specify which section to act on β¨setRowAlign(row_num, [center|right|left])β sets theAlignattribute of the row πsetSectionRowAlign([tbody|thead|tfoot], section_num, row_num, [center|right|left])βsetRowAlignbut able to specify which section to act on πΈsetRowVAlign(row_num, [center|top|bottom|middle|baseline])β sets theVAlignattribute of the row πsetSectionRowVAlign([tbody|thead|tfoot], section_num, row_num, [center|top|bottom|middle|baseline])βsetRowVAlignbut able to specify which section to act on π¦setRowNoWrap(col_num, [0|1])β sets theNoWrapattribute of the row π«setSectionRowNoWrap([tbody|thead|tfoot], section_num, row_num, [0|1])βsetRowNoWrapbut able to specify which section to act on βsetRowBGColor(row_num, [colorname|colortriplet])β sets theBGColorattribute of the row π¨setSectionRowBGColor([tbody|thead|tfoot], section_num, row_num, [colorname|colortriplet])βsetRowBGColorbut able to specify which section to act on πsetRowStyle (row_num, 'css style')β sets theStyleattribute of the row πsetSectionRowStyle([tbody|thead|tfoot], section_num, row_num, 'css style')βsetRowStylebut able to specify which section to act on πsetRowClass (row_num, 'css class')β sets theClassattribute of the row πsetSectionRowClass([tbody|thead|tfoot], section_num, row_num, 'css class')βsetRowClassbut able to specify which section to act on β¨setRowAttr (row_num, 'user attribute')β sets theAttrattribute of the row πsetSectionRowAttr([tbody|thead|tfoot], section_num, row_num, 'user attribute')βsetRowAttrbut able to specify which section to act on π«ΆsetRCellsWidth(row_num, [pixels|percentoftable])- **
setRowWidth(row_num, [pixels|percentoftable]) ** Deprecated **** β runssetCellWidth` over the entire row π setSectionRCellsWidth([tbody|thead|tfoot], section_num, row_num, [pixels|percentoftable])- **
setSectionRowWidth([tbody|thead|tfoot], section_num, row_num, [pixels|percentoftable]) ** Deprecated **** β same assetRowWidth`, but able to specify which section to act on π¦ setRCellsHeight(row_num, [pixels])- **
setRowHeight(row_num, [pixels]) ** Deprecated **** β runssetCellHeight` over the entire row π setSectionRCellsHeight([tbody|thead|tfoot], section_num, row_num, [pixels])- **
setSectionRowHeight([tbody|thead|tfoot], section_num, row_num, [pixels]) ** Deprecated **** β same assetRowHeight`, but able to specify which section to act on πΈ setRCellsHead(row_num, [0|1])- **
setRowHead(row_num, [0|1]) ** Deprecated **** β runssetCellHead` over the entire row π setSectionRCellsHead([tbody|thead|tfoot], section_num, row_num, [0|1])- **
setSectionRowHead([tbody|thead|tfoot], section_num, row_num, [0|1]) ** Deprecated **** β same assetRowHead`, but able to specify which section to act on β setRCellsFormat(row_num, start_string, end_string)- **
setRowFormat(row_num, start_string, end_string) ** Deprecated **** β runssetCellFormat` over the entire row π setSectionRCellsFormat([tbody|thead|tfoot], section_num, row_num, start_string, end_string)- **
setSectionRowFormat([tbody|thead|tfoot], section_num, row_num, start_string, end_string) ** Deprecated **** β same assetRowFormat`, but able to specify which section to act on π
Heads up bestie: all those ** Deprecated ** ones? Yeah, they're deprecated. Still documented, still real, but maybe don't build your whole personality around them π
setLastRow*β all of thesetRowmethods have a correspondingsetLastRowmethod. It doesn't accept therow_numparameter β it just automatically applies to the last row of the table. main character of auto-targeting β¨
NB. Only works on thesetRow*methods, not on thesetSectionRow*methods. Periodt π«
getRowStyle(row_num)β returns the row's style attribute πgetSectionRowStyle([tbody|thead|tfoot], section_num, row_num)β literally the same energy asgetRowStylebestie, except you get to say which section you're talking about. So specific. So demure. π
β¨ Output Methods β¨ π
π getTable π
Returns a string containing the HTML representation of the table. That's it. That's the whole girl. β¨
The same effect can also be achieved by using the object reference in a string scalar context β no cap, she just knows when she's being printed. π
For example...
This code snippet:
$table = new HTML::Table(2, 2);
print '<p>Start</p>';
print $table->getTable;
print '<p>End</p>';
would produce the same output as:
$table = new HTML::Table(2, 2);
print "<p>Start</p>$table<p>End</p>";
Not me putting the object straight into the string and it just WORKING. Two roads, one destination, both iconic. π¦
π«Ά print π«Ά
Prints HTML representation of the table to STDOUT. Direct. No middleman. It's giving main character energy fr fr. πΈ
β CLASS VARIABLES β
Okay so this heading is here, and the manpage puts absolutely nothing under it. Not a single line. Not a crumb. We leave it exactly as it is β I'm not out here inventing facts about class variables, that's not the vibe. π
π HISTORY π
This module was originally created in 1997 by Stacy Lacy and whose last version was uploaded to CPAN in 1998. The module was adopted in July 2000 by Anthony Peacock in order to distribute a revised version. This adoption took place without the explicit consent of Stacy Lacy as it proved impossible to contact them at the time. Explicit consent for the adoption has since been received. πβ¨
The lore is REAL, bestie. An entire glow-up arc from '97 to consent-received. Character development fr. π
π AUTHOR π
Anthony Peacock, a.peacock@chime.ucl.ac.uk
Stacy Lacy (Original author)
π CONTRIBUTIONS π
- Douglas Riordan <doug.riordan@gmail.com> β For get methods for Style attributes. π
- Jay Flaherty,
fty@mediapulse.comβ For ROW, COL & CELL HEAD methods. Modified the new method to allow hash of values. π₯ - John Stumbles,
john@uk.stumbles.orgβ For autogrow behaviour ofsetCell, and allowing alignment specifications to be case insensitive. β¨ - Arno Teunisse,
Arno.Teunisse@Simac.nlβ For the methods adding rules, styles and table alignment attributes. πΈ - Ville SkyttΓ€,
ville.skytta@iki.fiβ For general fixes. π§ - Paul Vernaza,
vernaza@stwing.upenn.eduβ For thesetLast...methods. β - David Link,
dvlink@yahoo.comβ For thesortmethod. π - Tommi Maekitalo,
t.maekitalo@epgmbh.deβ For adding the'head'parameter to thenewmethod and for adding the initialisation from an array ref to thenewmethod. π¦ - Chris Weyl,
cweyl@alumni.drew.eduβ For adding the even/odd row class support. π
The way this is a whole group chat of legends contributing to one Perl module. We love a collaborative slay. π«Ά
π COPYRIGHT π
Copyright (c) 2000-2007 Anthony Peacock, CHIME. Copyright (c) 1997 Stacy Lacy
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Periodt. π
π₯ SEE ALSO π₯
And that's the whole page bestie!! This little beauty is perl v5.36.0 and got its glow-up date of 2023-01-22 β go read the docs, build some tables, and slay responsibly. Byeeee πβ¨π
π¦ where this page comes from
- debian bookworm: libhtml-table-perl 2.08a-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhtml-table-perl 2.08a-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-table-perl 2.08a-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-table-perl 2.08a-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libhtml-table-perl 2.08a-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms