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

In reply to $cgi->param() values are not updated instantly when read in a subroutine by Lukas

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.