Q:

How to create dynamically sized HTML table?

I have a Perl CGI script. I would like to make a dynamic, appropriately-sized table based on query information from a simple HTML form: http://jsfiddle.net/wBgBZ/4/. I wanted to use HTML::Table but the server doesn't have the module installed. The administrator won't install it either. Therefore, I have to do it the old fashion way.
Here's what I have so far.
#!/usr/bin/perl
use strict; use warnings;
use CGI qw( :standard);

print header;
print start_html(
    -title => 'Creating Tables'
);

# Process an HTTP request
my $query = param("names");
my @students_in_class = split(/;/, $query);
my %attributes = (
    'Tommy'   => 'A star baseball player who has lots of potential to play in the Major League of Baseball. ',
    'Tyrone'  => 'An honor roll athlete. His father is really proud of him. When he graduates, he wents to work at the National Institute for Public Health. His father wants him to become a doctor but he wants to pursue Physics.',
    'Marshall' => 'A professional WWE wrestler.',
);

print table({-border=> undef},
    caption('Students in the class'),
        Tr({-align=>'CENTER',-valign=>'TOP'},
            [ 
             th(['Student', 'List of Attributes']),
             foreach (@students_in_class){       # !!!!! problem line !!!!!!
                 td(['$_' , '$attributes{$}']),
             }
             ]
           )
 );

Such that if the user enters the following into the search bar: Tyrone;Tommy;Marshall
the CGI should produces something similar to the following
Desired Output
http://jsfiddle.net/PrLvU/

If the user enters just Marshall;Tommy, the table should be 3x2.
It doesn't work. I need a way to dynamically add rows to the table.

A:

This is untested, but I think this is what you are wanting. You may need to change some of the table attributes to your desired needs.
use strict; 
use warnings;
use CGI qw( :standard );

print header,
      start_html(-title => 'Creating Tables');

my $query = param('names');

my @headers;
my @students = split(/;/, $query);

my %attributes = (
        Tommy   => 'A star baseball player.',
        Tyrone  => 'An honor roll athlete.',
       Marshall => 'A professional WWE wrestler.',
);

$headers[0] = Tr(th('Student'), th('List of Attributes'));

for my $i (@students) {
  push @headers, Tr( td($i), td($attributes{$i}));
}

print table( {-border => undef}, @headers );

