title: Tables using Perl CGI.pm
body: <p>I am trying to create a table using CGI.pm. The table would contain results from
  querying a database. The array final contains record numbers on which the queries will
  be executed. The results for each record should be in a separate row. The result 
  of each query on the record should form a column in the row. Using some code I found 
  <a href="http://cpansearch.perl.org/src/MARKSTOS/CGI.pm-3.59/cgi_docs.html" rel="nofollow">here</a>, I wrote the following
  (which may not be he best way of doing it):</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use CGI;
  use DBI;
  
  ...
  
  my @rows;
  my $rec;
  
  foreach my $el (@final) {
                @rows = ();
      foreach my $query (@queries) {
          my $query_handle = $connxn-&gt;prepare($query);
          $query_handle-&gt;execute($el);
          $query_handle-&gt;bind_columns(undef, \$rec);
          while($query_handle-&gt;fetch()) {
              push(@rows,td($rec));
          }   
      }
      print $q-&gt;table({-border=&gt;undef,-width=&gt;'25%'},
          Tr(\@rows)
         );
  }
  </code></pre>
  
  <p>When I run this, I get a blank page and an error "Undefined subroutine &amp;main::td"
  in the error log. Why do I get this error and I how do I get rid of it? Is there any 
  other way to create a table the table I want using CGI.pm? 
  Any help would be appreciated. Thanks!</p>
  
  <p>EDIT: Using Sinan's suggestion, I modified my code as follows:</p>
  
  <pre><code>#!/usr/bin/perl 
  use strict; 
  use warnings; 
  use DBI;  
  use Carp; 
  use HTML::Template;
  
  ...  
  my @rows; 
  my $rec;  
  foreach my $el (@final) {     
      foreach my $query (@queries) {         
          my $query_handle = $connxn-&gt;prepare($query);         
          $query_handle-&gt;execute($el);         
          $query_handle-&gt;bind_columns(undef, \$rec);         
          while($query_handle-&gt;fetch()) {             
              push(@rows, { CELLS =&gt; [ map { CELL =&gt; $_ }, $rec ] });         
          }        
      } 
  }
  
  my $tmpl = HTML::Template-&gt;new(filehandle =&gt; \*DATA);
  $tmpl-&gt;param(ROWS =&gt; \@rows); 
  print $tmpl-&gt;output;
  
  __DATA__ 
  &lt;table&gt; 
  &lt;TMPL_LOOP ROWS&gt; 
  &lt;tr&gt; 
  &lt;TMPL_LOOP CELLS&gt; 
  &lt;td&gt;&lt;TMPL_VAR CELL&gt;&lt;/td&gt; 
  &lt;/TMPL_LOOP&gt;
  &lt;/tr&gt; 
  &lt;/TMPL_LOOP&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>But now I am getting the result of each query in a separate row, instead of the same one
  (see original question above). I can't seem to fix it on my own. Any ideas?</p>
  
  <p>EDIT2: Though I am accepting Sinan's answer, meanwhile I developed my own solution (in case anyone is interested):</p>
  
  <pre><code>my @rows;
  my @rows1;
  foreach my $el (@final) {
      @rows = ();
      foreach my $query1 (@queries) {
          my $query_handle = $connxn-&gt;prepare($query1);
          $query_handle-&gt;execute($el);
          $query_handle-&gt;bind_columns(undef, \$rec);
          while($query_handle-&gt;fetch()) {
              push(@rows, $rec);              
          }                       
      }
      push(@rows1, { CELLS =&gt; [ map { CELL =&gt; $_ }, @rows ] }); 
  }
  </code></pre>