开发者

How do I create a simple HTML table with hyperlinks from a hash in Perl?

I have a Perl hash of "people" like this:

my $data = {
    124535 => {
        NAME     => "abe",
        AGE      => 100,
        SEX      => "m",
        HOMEPAGE => qw (http://abe.knaan.old)
    },
    54478 => {
        NAME     => "joe",
        AGE      => 18,
        SEX      => "m",
        HOMEPAGE => qw (http://slappy.joe.com)
    },
    54478 => {
        NAME     => "jane",
        AGE      => 20,
        SEX      => "f",
        HOMEPAGE => qw (http://i.am.jane/jane.html)
    },
};

I would like to print an HTML page with a table of all people, one row per person, with all its data including the hash key (i.e. 5 columns), including a hyperlinks to it's homepage.

I can write a long piece of ugly code that prints all the HTML headers etc., but is there a nicer, cleaner way to that? Perhaps using some module开发者_如何学Gos for this, I guess, quite popular task?

I found HTML::QuickTable but I'm not sure how to convert my structure to an appropriate one.


This should do it:

my @names = qw(NAME AGE SEX HOMEPAGE);
my @data  = [@names, 'KEY'];

for my $k (keys %$data) {
    my @t = @{$data->{$k}}{@names};
    $t[-1] = qq{<a href="$t[-1]">$t[-1]</a>};        
    push @data, [@t, $k]
}        

use HTML::QuickTable;
my $qt = HTML::QuickTable->new(... labels => 1); 
print $qt->render(\@data); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜