1

我有一个 Perl CGI 脚本。我想根据来自简单 HTML 表单的查询信息制作一个动态、大小合适的表格:http: //jsfiddle.net/wBgBZ/4/。我想使用HTML::Table但服务器没有安装模块。管理员也不会安装。因此,我必须以旧时尚的方式来做。

这是我到目前为止所拥有的。

#!/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{$}']),
             }
             ]
           )
 );

这样,如果用户在搜索栏中输入以下内容:Tyrone;Tommy;Marshall

CGI 应该产生类似于以下内容的内容

期望的输出

http://jsfiddle.net/PrLvU/


如果用户输入 just Marshall;Tommy,表格应该是 3x2。

它不起作用。我需要一种动态添加行的方法动态添加到表中的方法。

4

1 回答 1

1

这是未经测试的,但我认为这就是你想要的。您可能需要根据需要更改某些表属性。

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 );
于 2013-05-11T01:39:21.947 回答