#!c:/Perl/bin/Perl.exe use strict; use warnings; use CGI; use HTML::Template; use CGI::Session; # for debugging use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Data::Dumper; # globals my ($file, $fh); my ($current_page, %data); my $q = get_cgi_query(); my $s = get_session(); %data = update_data(); display_data($s); ############################ # subs ############################ sub update_data{ my ($page, $var); # load data from file $file = 'data.txt'; open $fh, '<', $file or die "can't open $file: $!"; while (<$fh>){ chomp; if (/(\w+)\s+=\s+(\w+)/){ if ($1 eq 'Page'){ $page = $2; } elsif ($1 eq 'VarName'){ $var = $2; } else{ $data{$page}{$var}{$1} = $2; } } } close $fh; # update data from CGI query if ($q->param){ my @names = keys %{$data{$current_page}}; for my $name (@names){ $data{$current_page}{$name}{Value} = $q->param($name); } } # write to disk $file = 'data.txt'; open $fh, '>', $file or die "can't open $file: $!"; for my $page (sort keys %data){ print $fh "Page = $page\n\n"; for my $var (sort keys %{$data{$page}}){ print $fh "\n"; print $fh "\tVarName = $var\n"; for my $field (sort keys %{$data{$page}{$var}}){ print $fh "\t$field = $data{$page}{$var}{$field}\n"; } print $fh "\n\n"; } } close $fh; return %data; } sub display_data{ my ($session) = @_; # how many pages? my $page_count = keys %data; # which page to return? if ($q->param('next')){ $current_page++ unless $current_page == $page_count; } elsif ($q->param('back')){ $current_page-- unless $current_page == 1; } # build AoH for H::T my @params; my %page_data = %{$data{$current_page}}; for my $var (sort keys %page_data){ my %var_hash = ( Name => $var, Max => undef, Min => undef, Value => undef, ); for my $val (keys %{$page_data{$var}}){ $var_hash{$val} = $page_data{$var}{$val}; } push @params, \%var_hash; } # create H::T object my $template; { $file = 'tmpl.html'; $template = HTML::Template->new( filename => $file ) or die "can't find $file: $!"; } # load H::T params $template->param( VALUES => \@params, PAGE => $current_page, ); # send session and header to browser $session->param( "page" => $current_page, ); print $session->header(); # print page to browser my $html = $template->output; print $html; # write html to file (debugging) { $file = "session.html"; open $fh, '>', $file or die "can't open $file: $!"; print $fh $html; close $fh; } } sub get_cgi_query{ $CGI::POST_MAX = 1024 * 100; $CGI::DISABLE_UPLOADS = 1; $ENV{PATH} = ""; delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' }; my $q = CGI->new(); return $q; } sub get_session{ my ($q) = @_; my ($session, $sid); # get session cookie if exists $sid = $q->cookie( "CGISESSID" ) or undef; # get/create session $session = CGI::Session->new( undef, $sid, {Directory=>'tmp'} ) or die "can't create session: $!"; # get current page from session if ($session->param('page')){ $current_page = $session->param('page'); } else{ $current_page = 1; } return $session; } #### Page = 1 VarName = p1v1 Max = p1v1max Min = p1v1min Value = p1v1valnew VarName = p1v2 Max = p1v2max Min = p1v2min Value = p1v2valnew Page = 2 VarName = p2v1 Max = p2v1max Min = p2v1min Value = p2v1valnew VarName = p2v2 Max = p2v2max Min = p2v2min Value = p2v2valnew #### test
Page num

Variable Value Max Value Min Value
##
## test
Page num

Variable Value Max Value Min Value
##
## $VAR1 = { '1' => { 'p1v1' => { 'Max' => 'p1v1max', 'Value' => 'p1v1val', 'Min' => 'p1v1min' }, 'p1v2' => { 'Max' => 'p1v2max', 'Value' => 'p1v2val', 'Min' => 'p1v2min' } }, '2' => { 'p2v1' => { 'Max' => 'p2v1max', 'Value' => 'p2v1val', 'Min' => 'p2v1min' }, 'p2v2' => { 'Max' => 'p2v2max', 'Value' => 'p2v2val', 'Min' => 'p2v2min' } } };