开发者

Display output in HTML format by perl

I have a hashmap with some information(key and value) in a perl file. I want to display them in HTML output and each displayed (key, value) will link to something. When I click the link then there will be some information there. Anyone suggests 开发者_Python百科me how can I do that. Is this similar to creating a CGI file and use CGI.pm? I will update more detail on this question later.


Yes, you can use the excellent CGI module to render HTML content for you, even if you are not processing CGI forms (i.e. use the module only on output, rather than also for input processing):

use CGI;

my $q = CGI->new;
my @html_list = map {
     $q->li($_ . ": " . $hash{$_};
} keys %hash;

print $q->ul($q->li({-type=>'foo'}, @html_list);


Depending on the data you're trying to display, something like HTML::Table may be useful if you want to display it in tabular format and don't want the drugery of assembling the appropriate HTML yourself.

For instance, you could do something like:

my $table = HTML::Table->new(-columns => 2);
for my $key (sort keys %hash) {
    $table->addRow($key, $hash{$key});
}
$table->print;

Also, there is a free Beginning Perl book available online, which has a chapter devoted to CGI scripts, along with a lot of other useful information.

If this is more than just a simple one-off script, you might also wish to consider using one of the many Perl web frameworks like Dancer, Catalyst, Mojo etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜