http://www.perlmonks.org?node_id=147179


in reply to Refreshing a perl script

I'm not quite clear on what the code is trying to accomplish, but I suspect what happens is that the variable list is never cleared. When you come back for the third run through the script, you still populate all the $FORM{$name} values that were present on the second page, so hitting refresh just means the third page will have the values you set on the second page, rather than no values at all.

I think you will have less trouble with the logic if you set up two HTML output pages, one for the initial (empty) condition, and the other for the display condition. Then give each "submit" button a different name so you can test in the code which page the submit came from, and then do the right thing.

In the first (initial) condition the submit button might say:

 <input type="submit" name="initial_page" value="See the Results!">

while the second (display) page button says something like "Try again!", as in:

<input type="submit" name="display_page" value="Try again!">

So following your current coding, you would show the second (value) page only if $FORM{initial_page} has a value; otherwise you would show the first page.


A couple of coding hints. You can simplify presenting HTML documents by using a HERE document; this lets you display HTML unchanged between the starting and ending tag. The 3rd edition of the Camel book Programming Perl has a good description of these on pages 66-67. For example, you could change your code printing out the start form statement to something like:

if ($line =~ /<!--InputWords-->/i) { print <<EOF; <FORM METHOD=POST ACTION=$CGIURL> <INPUT TYPE=HIDDEN NAME=TextFile VALUE="$TextFile"> <P><CENTER> <TABLE> EOF foreach $variable (@sortedvariables) { next if ($variable eq $lastvariable); print <<EOF; <TR><TD ALIGN="RIGHT"><P>$variable: </TD> <TD><INPUT TYPE="TEXT" NAME="$variable" SIZE="25"> </TD></TR> EOF $lastvariable = $variable; } print <<EOF; </TABLE></P>; <P><INPUT TYPE="SUBMIT" VALUE="See the Results!"> </CENTER></P></FORM> EOF } else { print $line; }

All the characters between print <<EOF; and EOF (on a line by itself, flush left, no spaces after, just a \n) will be printed unchanged, including the carriage returns. All Perl variables in the block (like $variable) will be replaced by its current value. Makes life much easier.

Also you really should learn about CGI.pm. Your first lines of code, where you retrieve form values, could be as simple as:

use strict; use CGI; $q=CGI->new(); if ($q->param('initial_page') { [ do the stuff to display the results page ] }

The mere issuing of the command "new CGI" gathers in all of the form field names and their values from the last invocation of the script, does all the necessary decoding, removing +'s and other funny marks, and makes the full list available to you, so that "$variable1 = $q->param('variable1') will return the value of the parameter variable1 (assuming it is a single value and not a multivalue field). And all this without extra work by you! So good modules like this one will save lots of work, prevent lots of errors, and let you concentrate on the new stuff in your code.

Hope this helps.

Live in the moment