1) Create a form that asks a user for his/her name and some comments. Then create two CGI scripts that create a response. The first script displays the information which the user has submitted ("Hello $name. These are your comments: $comments") and asks the user whether he/she really wants to submit the information. The second script is invoked by the first one and displays "Thank you $name. Your comments have been submitted: $comments". Form:
Name:

Comments:

----------------------------------------------------------------- First CGI Script: #!/usr/local/bin/perl use CGI qw(:standard); my $name= param("name"); my $comments = param("comments"); print header(); if ($name =~ /[^\w -]/ or $comments =~ /[^\w -]/ or $name =~ /.{100,}/ or $comments =~ /.{100,}/ or $name eq "" or $comments eq "") { print "Error"; die; } print "Maintaining State
Hello $name! Do you really want to submit these comments?

$comments

"; print end_html(); ----------------------------------------------------------------- 2) Use a cookie instead of hidden text in the previous exercise First CGI file: #!/usr/local/bin/perl use CGI qw(:standard); my $name= param("name"); my $comments = param("comments"); if ($name =~ /[^\w -]/ or $comments =~ /[^\w -]/ or $name =~ /.{100,}/ or $comments =~ /.{100,}/ or $name eq "" or $comments eq "") { die; } $cookiedata = $name."|".$comments; $cookie = cookie(-name=>'nameandcomments',-value=>"$cookiedata", -expires=>'+1h'); print header(-cookie=>$cookie); print "Maintaining State
Hello $name! Do you really want to submit these comments?

$comments

"; print end_html(); ----------------------------------------------------------------- Second CGI file: #!/usr/local/bin/perl use CGI qw(:standard); print header(); print "Maintaining State

Reply

"; $cookiedata = cookie(-name=>'nameandcomments'); ($name, $comments) = split(/\|/,$cookiedata); if ($name =~ /[^\w -]/ or $comments =~ /[^\w -]/ or $name =~ /.{100,}/ or $comments =~ /.{100,}/ or $name eq "" or $comments eq "") { die; } print "

Hello $name. Here are your comments:

$comments"; print end_html(); ----------------------------------------------------------------- 3) First CGI file #!/usr/local/bin/perl use CGI qw(:standard -debug); use CGI::Session; use HTML::Template; my $template_text1 = < Name and Comments There was an error with your form data. Please, go back and try again. EOS my $template_text2 = < Name and Comments Hello Do you really want to submit these comments?

EOS my $cgi = new CGI; my $session = new CGI::Session(undef, $cgi, {Directory=>'/home/yourdir/tmp'}); $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); my $template1 = HTML::Template->new(scalarref => \$template_text1 ); my $template2 = HTML::Template->new(scalarref => \$template_text2, associate => $cgi); if ($cgi->param("name") =~ /[^\w -]/ or $cgi->param("comments") =~ /[^\w -]/ or $cgi->param("name") =~ /.{100,}/ or $cgi->param("comments") =~ /.{100,}/ or $cgi->param("name") eq "" or $cgi->param("comments") eq "") { print $template1->output(); } else { $session->save_param($cgi); print $template2->output(); } ----------------------------------------------------------------- Second CGI file: #!/usr/local/bin/perl use CGI qw(:standard -debug); use CGI::Session; use HTML::Template; ######################################################################## my $template_text = <Maintaining State

Reply

Hello ! Here are your comments:

EOS my $cgi = new CGI; my $session = new CGI::Session(undef, $cgi, {Directory=>'/home/username/tmp'}); print $cgi->header(); my $template = HTML::Template->new(scalarref =>\$template_text, associate => $session); print $template->output(); ----------------------------------------------------------------- 4) Hit counter: create a file that contains only the number "0". Your CGI script must open that file for reading; read the first line of the file into a scalar variable; increase the number by one; close the file; open the file again for writing (not appending); write the number to the file; close the file. #!/usr/local/bin/perl use CGI qw(:standard); print header(); print start_html("Hit Counter"); open(FILE, "output.txt"); $counter = ; $counter++; close(FILE); open(FILE, ">output.txt"); print FILE $counter; close(FILE); print p("You are the $counter visitor of this page"); print end_html();