#!/usr/local/bin/perl
 use CGI::Form;
 $q = new CGI::Form;
 print $q->header();
 print $q->start_html(-title=>`A Very Simple Calculator');
 print "<H1>A Very Simple Calculator</H1>\n";
 if ($q->cgi->var(`REQUEST_METHOD') eq `GET') {
    $val=0;
    &printForm($q,$val);
 } else {
    $val=$q->param(`hiddenValue');
    $modifier=$q->param(`Modifier');
    if ($modifier=~/^[\d]+$/) {
       $op=$q->param(`Action');
       if ($op eq "Add") {
          $val+=$modifier;
       } elsif ($op eq "Subtract") {
          $val-=$modifier;
       } elsif ($op eq "Multiply") {
          $val*=$modifier;
       } elsif ($op eq "Divide") {
          $val/=$modifier;
       }
    } else {
       print "<P><STRONG>Please enter a numeric value!</STRONG><BR><BR>\n";
    }
    $q->param(`hiddenValue',$val);
    &printForm($q,$val);
 }
 print $q->end_html();
 sub printForm {
    my($q,$val)=@_;
    print "<P>The current value is: $val\n";
    print "<P>Please enter a value and select an operation.\n<BR>";
    print $q->start_multipart_form();
    print $q->hidden(-name=>`hiddenValue',-value=>$val);
    print "<TABLE><TR><TD COLSPAN=4>\n";
    print $q->textfield(-name=>`Modifier',-size=>12,-maxlength=>5);
    print "</TD></TR>\n<TR><TD>\n";
    print $q->submit(-name=>`Action',-value=>`Add');
    print "\n</TD><TD>\n";
    print $q->submit(-name=>`Action',-value=>`Subtract');
    print "\n</TD><TD>\n";
    print $q->submit(-name=>`Action',-value=>`Multiply');
    print "\n</TD><TD>\n";
    print $q->submit(-name=>`Action',-value=>`Divide');
    print "\n</TD><TD>\n";
    print "</TR></TABLE>\n";
    print $q->end_form;
 }
