Apologies for the delay in getting back to you.

The only state information you need to maintain is the current page. Using this you know which data to send to the browser and which to write back to disk. Here is one approach.

It uses CGI::Session (but a hidden field could be used). It also uses HTML::Template.

If you decided to try it be sure to create a dir called 'tmp' in the same dir as the script (used by CGI::Session). It won't work otherwise.

Also, I changed the structure (see below) to a HoHoH. This method alows a 'page' full of data to be easily extracted/displayed/written back to disk.

The HTML is not pretty (!) and will of course need attention. For the sake of this demo I have used a 'back' and 'next' button to move between the pages.

script

#!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 "<Start>\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 "<End>\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; }

Data table written back to disk

Page = 1 <Start> VarName = p1v1 Max = p1v1max Min = p1v1min Value = p1v1valnew <End> <Start> VarName = p1v2 Max = p1v2max Min = p1v2min Value = p1v2valnew <End> Page = 2 <Start> VarName = p2v1 Max = p2v1max Min = p2v1min Value = p2v1valnew <End> <Start> VarName = p2v2 Max = p2v2max Min = p2v2min Value = p2v2valnew <End>

Template file

<html><head><title>test</title></head> <body> <form name="Go" action="session.cgi"> <table> <tr> <td>Page num</td> <td><h2><TMPL_VAR NAME=PAGE></h2></td> <td> <input TYPE="submit" name='back' VALUE="back"> <input TYPE="submit" name='next' VALUE="next"> </td> </tr> </table> <table> <tr> <th>Variable</th> <th>Value</th> <th>Max Value</th> <th>Min Value</th> </tr> <TMPL_LOOP NAME=VALUES> <tr> <td><TMPL_VAR NAME=Name></td> <td><input type="input" name="<TMPL_VAR NAME=Name>" value="<TM +PL_VAR NAME=Value>" /></td> <td><TMPL_VAR NAME=Max></td> <td><TMPL_VAR NAME=Min></td> </tr> </TMPL_LOOP> </table> </form> </body> </html>

HTML output

<html><head><title>test</title></head> <body> <form name="Go" action="session.cgi"> <table> <tr> <td>Page num</td> <td><h2><TMPL_VAR NAME=PAGE></h2></td> <td> <input TYPE="submit" name='back' VALUE="back"> <input TYPE="submit" name='next' VALUE="next"> </td> </tr> </table> <table> <tr> <th>Variable</th> <th>Value</th> <th>Max Value</th> <th>Min Value</th> </tr> <TMPL_LOOP NAME=VALUES> <tr> <td><TMPL_VAR NAME=Name></td> <td><input type="input" name="<TMPL_VAR NAME=Name>" value="<TM +PL_VAR NAME=Value>" /></td> <td><TMPL_VAR NAME=Max></td> <td><TMPL_VAR NAME=Min></td> </tr> </TMPL_LOOP> </table> </form> </body> </html>

Data structrue
(HoHoH)

$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' } } };

In reply to Re^3: web-interface using data from flat files by wfsp
in thread web-interface using data from flat files by sara2005

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.