如何在 Perl 中生成 HTML 表格?

发布于 2024-08-25 08:53:29 字数 870 浏览 8 评论 0 原文

我需要创建 2 个 HTML 格式的表。每个有 5 行:

第一个表

  • 第一行有水果,占据所有列
  • 第二行有一月(月),占据所有列
  • 第三行有一些 6 种水果的名称(苹果,橙子,葡萄,...)这些名称做不改变。所以这一行有 6 列,
  • 第四行有每种水果的比率(10,20,30..),所以这有 6 列。
  • 第五行对于每种水果都有相应的消息,显示为可用或不可用。

第二个表

(如果可用)单元格的背景颜色应为绿色,如果不可用,则应为红色。

  • 第一行有蔬菜,占据所有列
  • 第二行有二月(月),占据所有列
  • 第三行有一些 6 种蔬菜的名称(番茄,土豆..)这些名称不会改变。所以这一行有 6 列,
  • 第四行有每种蔬菜的比率(10,20,30..),所以这有 6 列。
  • 第五行对于每种蔬菜都有相应的消息,显示为可用或不可用。如果可用,则单元格的背景颜色应为绿色,如果不可用,则为红色。

所有这些数据都是从具有特定格式的文件中读取的,

<name of fruit/vegetable> price <available or not>

水果和蔬菜的名称不会改变,两个表都是相同的。 然而,可能不存在特定水果/蔬菜的数据。 如果不存在,则该列应显示 N/A 并带有白色背景。

我不能为此使用 MIME:Lite 。 需要使用print <

I need to create a 2 tables in HTML format. Each has 5 rows:

1st Table

  • 1st row has FRUITS in it, occupying all columns
  • 2nd row has January(month), occupying all columns
  • 3rd row has names of some 6 fruits (apple, orange, grapes,...)These names do not change. so this row has 6 columns
  • 4th row has rates for each fruit ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each fruit showing as Available or not.

2nd Table

If it is available the background color for the cell should be green and if not RED.

  • 1st row has VEGETABLES in it, occupying all columns
  • 2nd row has February(month), occupying all columns
  • 3rd row has names of some 6 vegetables (tomato, potato..)These names do not change. so this row has 6 columns
  • 4th row has rates for each vegetable ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each vegetable showing as Available or not.If it is available the background color for the cell should be green and if not RED.

All this data is read from a file having a particular format, it is

<name of fruit/vegetable> price <available or not>

The names of fruits and vegetable do not change , it will be same for both the tables.
However, it might be possible that data for a particular fruit/vegetable is not present.
if it is not present the column for that should show N/A with white background.

I cannot use MIME:Lite for this.
Need to use print <<ENDHTML;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2024-09-01 08:53:29
use HTML::TagTree;
use strict;

my $html = HTML::TagTree->new('html');
$html->head->title('Fruits');

my $table = $html->body->table();
$table->tr->td('Fruits','colspan=6');
$table->tr->td('February','colspan=6');
my @fruits = qw(apples bananas coconut dates figs guava);
my @rates = (10,20,30,40,50,60);
my $tr_fruits = $table->tr;
my $tr_rates = $table->tr;
my $tr_available = $table->tr;
for (my $col=0; $col<6; $col++) {
   $tr_fruits->td($fruits[$col]);
   $tr_rates->td($rates[$col]);
   # Randomly make available
   my $available = int(rand(1000)/500 + .5);
   if ($available) {
      $tr_available->td('Available','style=background-color:green');
   } else {
      $tr_available->td('Out of Stock','style=background-color:red');
   }
}
print $html->print_html();
use HTML::TagTree;
use strict;

my $html = HTML::TagTree->new('html');
$html->head->title('Fruits');

my $table = $html->body->table();
$table->tr->td('Fruits','colspan=6');
$table->tr->td('February','colspan=6');
my @fruits = qw(apples bananas coconut dates figs guava);
my @rates = (10,20,30,40,50,60);
my $tr_fruits = $table->tr;
my $tr_rates = $table->tr;
my $tr_available = $table->tr;
for (my $col=0; $col<6; $col++) {
   $tr_fruits->td($fruits[$col]);
   $tr_rates->td($rates[$col]);
   # Randomly make available
   my $available = int(rand(1000)/500 + .5);
   if ($available) {
      $tr_available->td('Available','style=background-color:green');
   } else {
      $tr_available->td('Out of Stock','style=background-color:red');
   }
}
print $html->print_html();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文