#!/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: $!"; } ####

Registrations

[%- rc = 1 -%] [%- FOREACH reg IN reg_array -%] [%- rc = rc + 1 -%] [%- END -%]
Last name First name
##
## 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)' };