Lukas has asked for the wisdom of the Perl Monks concerning the following question:

Hey there! I was trying to write a CGI-Application with a form which under circumstances will be sent multiple times to the user until everything is filled out correctly. Valid form values shall be sent back to the user prefilled in the form. (Just what you expect from a good form.) I noticed a problem when reading the form's POST parameters with $cgi->param(). It seems that I must wait 15 seconds between submissions, otherwise the param() function will return an old value that was sent to the server before. This only happens inside a subroutine, not in the "main" part of the script. I created a minimal example:
#!/usr/bin/perl -w use strict; use warnings; use CGI; use utf8; #this perl-file is written in utf-8 binmode STDOUT, ":encoding(utf8)"; #encode all output directed to STDO +UT (e.g. print) to browser as utf-8 use Data::Dumper; #zum debuggen my $cgi = new CGI; $cgi->charset('UTF-8'); # Let CGI.pm write 'Content-Type: text/html; c +harset=UTF-8' in the HTTP header print $cgi->header(); # Write HTTP header print "\$cgi->param('creators_name') (main)= " . $cgi->param('creators +_name') . "<br>"; my $creators_name_content = $cgi->param('creators_name') . ""; print "\$creators_name_content (main)= $creators_name_content<br>"; print_formular($creators_name_content); sub print_formular { print <<"END"; <!DOCTYPE html> <html lang="de-CH"> <head> <title>title</title> </head> <body > END print "\$cgi->param('creators_name') (sub)= " . $cgi->param('c +reators_name') . "<br>"; print "\$creators_name_content (sub)= $creators_name_content<b +r>"; print "\$creators_name_content (arg)= $_[0]<br>"; print <<"END"; <form action="shortingest.pl" accept-charset="utf-8" method="post" enc +type="multipart/form-data"> <table id="mytable"> <tr> <td><input name="creators_name" type="text" size="60" +value=""></td> </tr> <tr> <td><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html> END }
To reproduce do the following: Run the script. Fill in the value 1 and submit. You will see that all values will be 1. If you submit it again within 15 seconds with the value 2, the result will be:
$cgi->param('creators_name') (main)= 2 $creators_name_content (main)= 2 $cgi->param('creators_name') (sub)= 1 $creators_name_content (sub)= 1 $creators_name_content (arg)= 2
If you wait at least 15 seconds, the output will be what you expect. Do you have any pointers whats going on here?

Lukas

Replies are listed 'Best First'.
Re: $cgi->param() values are not updated instantly when read in a subroutine
by Anonymous Monk on Mar 09, 2011 at 13:20 UTC
    Do you have any pointers whats going on here?

    You're getting tricked by your secret mystery webserver :)

      It's an Apache 2.2.9 running on Debian. What can I do about it?
        1) explain how you're running the perl program, mod_cgi, FCGI, mod_perl, mod_perlite.... what? 2) check the logs :)
Re: $cgi->param() values are not updated instantly when read in a subroutine
by raybies (Chaplain) on Mar 09, 2011 at 15:21 UTC
    I'm no cgi expert, as I've only ever done one thing on it, but I seem to recall that you could get a delay between forms, if you didn't call close; after posting the form, because even if you indicate end of HTML, the server still thinks it might need to serve extra stuff... hope that helps.
      Needing to call close on STDOUT only comes into play when forking, and the child outlines the parent (perl will close STDOUT automatically on the parent)