0

The CGI to read the data from merch.txt works but how can I put this in a table?

HTML File:

<form action="/cgi-bin/asst5/data.cgi" method="post">
<table border="2" cellspacing="5" cellpadding="5">
<tr>
   <td align="center">SKU: </td>
   <td><input type="text" name="code" size="15"></td>
</tr>
<tr>
   <td align="center">Name: </td>
   <td><input type="text" name="customer" size="15"></td>
</tr>
</table>
</form>

Data.CGI where data is accepted after it's been entered in the HTML

my $sku = param('code');
my $cust = param('customer');

print <<HERE;
<HTML>
<BODY>
SKU: $sku<br>
Name: $cust<br>

HERE
open (ORDERS, ">>merch.txt") or die "File error, $!";
print ORDERS "$sku|$cust\n";
close ORDERS;

Read.CGI This is the CGI file that reads the content from merch.txt

open ( ORDERS, "merch.txt" ) or die "Error opening file, $!";
my @orders = <ORDERS>;
close ORDERS;

print <<here;
<HTML>
<BODY>
here
for (my $i = 0; $i < @orders; $i++ )  {
print "<td><pre>", CGI::escapeHTML($orders[$i])."<br>\n", "</pre></td>\n";
}
print <<MORE;
</BODY>
</HTML>
MORE
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
r2d222
  • 11
  • 3
  • You have missed some quotes and/or string concatenation operators in the `print` line inside your loop. I suggest running this from the command line or checking your server's error log to resolve these first. Try this for starters: `print "
    ", CGI::escapeHTML($orders[$i])."
    \n", "
    \n";`
    – ardavey Oct 31 '16 at 17:56
  • @ardavey. thank you for catching that. What I'm trying to figure out is how to make it look more like a table with columns and rows – r2d222 Oct 31 '16 at 18:16

1 Answers1

2

Using your raw embedded HTML approach, it's not hard. But it is incredibly ugly.

print <<END_OF_HTML;
<html>
<body>
<table>
<tr><th>Sku</th><th>Cust</th></tr>
END_OF_HTML

for (@orders)  {
  my ($sku, $cust) = split /\|/;
  print '<tr><td>', CGI::escapeHTML($sku), '</td>',
            '<td>', CGI::escapeHTML($cust), '</td></tr>';
}

print <<END_OF_HTML;
</table>
</body>
</html>
END_OF_HTML

But mixing raw HTML with your Perl code is a terrible idea. Please use a templating engine instead. I recommend the Template Toolkit.

Here's a simple CGI program using TT to do what you need.

#!/usr/bin/perl

use strict;
use warnings;
use Template;
use CGI qw[header];

my $tt = Template->new;

# Best practice: three-arg open() and lexical filehandles
open my $ord_fh, '<', 'merch.txt' or die $!;
my @orders = <$ord_fh>;
close $ord_fh;

my @order_data = map {
  my ($sku, $cust) = split /\|/;
  { sku => $sku, cust => $cust }
} @orders;

print header;
$tt->process('orders.tt', { orders => \@order_data })
  or dir $tt->error;

And you'll need a template that looks something like this:

<html>
  <head>
    <title>Orders</title>
  </head>
  <body>
    <h1>Orders</h1>
    <table>
      <tr><th>Sku</th><th>Cust</th></tr>
[% FOREACH order IN orders -%]
      <tr><td>[% order.sku | html %]</td><td>[% order.cust | html %]</td></tr>
[% END -%]
    </table>
  </body>
</html>
Dave Cross
  • 62,464
  • 3
  • 46
  • 83