I've used Template::Toolkit for a couple of years now, even done complicated things like filters, so a simple registration script that telescopes out as needed should be simple, right?

Apparently not. Please help.

First, the CGI:

#!/usr/bin/perl -w use strict; use warnings; use CGI qw/cgi-lib/; use Template; use Log::Log4perl; use Data::Dumper; our $logger; # Populate the order template. { # Start logging. Log::Log4perl::init('/var/www/perl/ontdist/order4.conf'); $logger = Log::Log4perl->get_logger('order4'); # Enter from a button push. my $cgi = CGI->new; my $vars = $cgi->Vars; $logger->debug("vars is ".Dumper($vars)); # Take data from form and put into AoH. my @reg_array; for ( 1..9 ) { my $last = "last_name$_"; my $first = "first_name$_"; if ( length($vars->{$last}) && length($vars->{$last}) ) { push ( @reg_array, { 'last_name' => $vars->{$last}, 'first_name' => $vars->{$first} } ); } else { # Leave a blank one at the end. push ( @reg_array, { 'last_name' => '', 'first_name' => '' } ); last; } } $logger->debug("reg_array is ".Dumper(\@reg_array)); # Put the AoH back into the vars hashref. $vars->{'reg_array'} = \@reg_array; $logger->debug("After adding 'reg_array' to vars, vars is ".Dumper +($vars)); # Output the resulting page. print $cgi->header; my $template = Template->new({}); $template->process('order4.tt2', $vars) or die "Unable to process order template: $!"; }

And then the template file:

<html> <body> <div align="center"> <form method="GET" action="order4.cgi"> <table border="1" cellpadding="10"> <tr> <td colspan="3"> <h3>Registrations</h3> <table border="1"> <tr> <th>Last name</th> <th>First name</th> </tr> [%- rc = 1 -%] [%- FOREACH reg IN reg_array -%] <tr> <td><input type="text" size="15" name="last_name[%- rc -%]" value="[%- last_name -%]"></td> <td><input type="text" size="15" name="first_name[%- rc -%]" value="[%- first_name -%]"></td> </tr> [%- rc = rc + 1 -%] [%- END -%] </table> </td> </tr> <tr> <td> <input type="submit" value="Recalculate" /> </td> </tr> </table> </form> </div> </body> </html>

I'm hoping that the reg_array AoH will fill up with first and last names from the form, but I can't seem to get the first set of names to come back from the initial form. Instead, Data::Dumper just tells that I have an array at $vars->{'reg_array'} (with a hex address, whoop-de-doo) but doesn't dump it for me. And the template doesn't seem to receive it.

2007-01-28 22:45:00,142 27851 [order4.cgi 48] DEBUG main: reg_array is + $VAR1 = [ { 'first_name' => 'Alex', 'last_name' => 'Beamish' }, { 'first_name' => '', 'last_name' => '' } ]; 2007-01-28 22:45:00,145 27851 [order4.cgi 53] DEBUG main: After adding + 'reg_array' to vars, vars is $VAR1 = { 'last_name1' => 'Beamish', 'first_name1' => 'Alex', 'reg_array' => 'ARRAY(0x839687c)' };

Why?

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


In reply to Can't seem to use an AoH with Template::Toolkit by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.