http://www.perlmonks.org?node_id=1073726

jms53 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I'm having some trouble displaying a table.

The original code, cgi:    $account->{transactions} = [ map { { line => $_ } } @transactions ]; where @transactions is filled with tab-separated values.

I want to display the elements of @transactions as a nice 2 dimensional table. Hence my new code, with the TMPL_LOOP:

   $account->{transactions} = [ map { { line => $_ } } @transactions ]; where @transactions is populated with array references:  push @transactions, \[$date, $type, $amount, $new_balance];

and the TMPL bit:

<TABLE BORDER="1"> <TMPL_LOOP NAME="transactions"> <TR><TD>Transaction:</TD> <TMPL_LOOP NAME="line"> <TD><TMPL_VAR NAME="date"></TD> <TD><TMPL_VAR NAME="type"></TD> <TD><TMPL_VAR NAME="amount"></TD> <TD><TMPL_VAR NAME="balance"></TD> </TMPL_LOOP> </TR> </TMPL_LOOP> </TABLE>

I don't know wether to look into altering the tmpl file or if my datastructure is just plain wrong?
The error appearing on the browser is the following:

HTML::Template->output() : fatal error in loop output : Can't call met +hod "isa" on unblessed reference at /users/jspinel/mylib/lib/perl5/HT +ML/Template.pm line 2563. at atm_choose.cgi line 15

Thank you!

J -

Replies are listed 'Best First'.
Re: Changing data structure with HTML::Template
by gryphon (Abbot) on Feb 06, 2014 at 21:04 UTC

    Greetings jms53,

    I think you'll need to change your data structure. HTML::Template expects to iterate through hashrefs in an array rather than arrayrefs.

    $account->{transactions} = [ map { { line => $_ } } @transactions ]; push @transactions, { date => $date, type => $type, amount => $amount, balance => $new_balance };
      Thank you gryphon,

      I updated my code as you showed, and the page now loads. However, the html sent to the browser doesn't contain any data for the table.

      <table border="1"> <tbody><tr> <td></td><td>Date</td><td>Type</td><td>Amount</td><td>Balance</ +td> </tr> <tr><td>Transaction:</td> </tr> </tbody></table>

      What I'm supposed to be seeing is more something along the lines of :
       <tr><td>Transaction</td><td>2014-02-06</td><td>credit</td><td>400</td><td>1400</td></tr>

      I have debugged the data to confirm it exists (my first project I was deleting my data on the first run, and then all subsequent runs yielded 0 results), and I'm not getting any complaints from the server. If you have any ideas, they are most welcome :)

      J -