The modules mentioned above like Template::Toolkit and CGI::Application sound good. If you would like to roll your own, you could go about it in this manner:
If you set a hidden form variable differently in each
html file you can create a state machine. Each form could include two lines like this: <input name="mode" value="form1">
<input name="history" value="HISTORYBUF">
inside the its form. The forms would all post to the same Perl program. The program would identify which form has called it with something like use strict;
use CGI;
use MIME::Base64;
my $q = new CGI;
my $mode = $q->param("mode");
if ($mode eq "form1") { &validateform1; }
elsif ...
The history variable would contain a string resulting from
packing a hash of the previous page's form variables. You
unpack this hash, add the current page's variables to it,
and repack.
When you are done processing the form, read the next html
template file from disk and replace the phrase HISTORYBUF
with the history string. This should accumulate all the pages' form variables, that is if you guarantee they all use unique names.
It is possible to do away with packing and unpacking
by just putting the bare word HISTORYBUF in without a surrounding input tag, and replacing that keyword with a whole list of input tags you generate on the spot.
But if you are going to pack a hash into a variable (called
serializing), you could use the popular Storable module which lets you freeze structures for transport and then thaw them out. You also probably want to make sure the
variable doesn't include any nasty characters by using MIME Base 64 encoding.
use Storable;
# unpack history from form, and add latest form's settings
my %formhash = %{ thaw(
decode_base64( $q->param("history"))
) };
foreach $p ($q->params) { $formhash{$p}=$q->param($p}; }
# meat of program
# pack back up. $template contains template file contents.
my $frozen = encode_base64( freeze \%formhash ); # someone check this
+pls
$template =~ s/HISTORYBUF/$frozen/;
print $template; # do a "print $q->header" at top
When I've used this method I haven't serialized the
data, but it should work.
Anyway there are many ways to do it as usual in Perl. You could also make a series of Perl programs with embedded html and which just load the previous page's form variables and don't worry about serialization of the data.
|