O'Reilly logo

Writing Apache Modules with Perl and C by Lincoln Stein, Doug MacEachern

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

F.1. Dynamic Tables

One very powerful feature of Embperl is its ability to process dynamic tables. This feature was designed mainly to display Perl arrays (one- or two-dimensional, regular and irregular), but it can also be used in other ways.

Here is an example that displays a Perl array:

[- @a = ( 'A', 'B', 'C') ; -]
<TABLE BORDER=1>
   <TR>
       <TD> [+ $a[$row] +] </TD>
  </TR>
</TABLE>

This example simply displays a table with three rows containing A, B, and C. The trick is done by using the magical variable $row which contains the row count and is incremented for every row. The table ends if the expression that contains $row returns undef. The same can be done with $col for columns. You can also use $cnt to create a table that wraps after a certain number of elements. This works with TABLE, SELECT, MENU, OL, DL, and DIR.

Here is a simple DBI example that displays the result of a query as a two-dimensional table with field names as headings in the first row:

[-
# connect to database
 $dbh = DBI->connect($DSN) ;

# prepare the sql select
$sth = $dbh -> prepare ("SELECT * from $table") ;

# excute the query
$sth -> execute ;

# get the fieldnames for the heading in $head
$head = $sth -> {NAME} ;

# get the result in $dat
$dat = $sth -> fetchall_arrayref ;
-]

<table>
   <tr><th>[+ $head->[$col] +]</th></tr>
   <tr><td>[+ $dat -> [$row][$col] +]</td></tr>
</table>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required