Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Refreshing a perl script

by wolverina (Beadle)
on Feb 23, 2002 at 06:37 UTC ( [id://147048]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that calls on itself depending on certain parameters. The 1st instance of the script displays several input fields.. when submit is clicked, the same script is run again and output is displayed. The problem i'm having is that when i click refresh to bring up the first input
part of the script, it just keeps displaying the output part.
I'm using a local web server on a win98 machine. I have to restart the web server and close/open the browser to get the first part/input part of the script to display again.
Any Idea's? Here's the script. Thanx, Lisa.
$InputFile = "input.txt"; $TextFile = "story2.htm"; $CGIURL = "weblibs.pl"; print "Content-type: text/html\n\n"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($FORM{$name}) { $FORM{$name} = "$FORM{$name}, $value"; } else { $FORM{$name} = $value; push (@variables,$name); } } if ($FORM{'TextFile'}) { &SendFile; } if (-d $TextFile) { opendir(FILES,$TextFile); @tempfiles = readdir(FILES); closedir(FILES); foreach $file (@tempfiles) { if (-T "$TextFile/$file") { push (@files, $file); } } srand(); $files = @files; $file = @files[int(rand($files))]; $TextFile = $TextFile."/".$file; } open (FILE,"$TextFile"); @LINES = <FILE>; close (FILE); $text = join(' ',@LINES); $text =~ s/\n/ /g; $text =~ s/</\n</g; $text =~ s/>/>\n/g; @text = split('\n',$text); undef (@variables); foreach $line (@text) { if ($line =~ s/<!--(.*)-->/$1/) { push (@variables,$line); } } @sortedvariables = sort (@variables); open (FILE,"$InputFile"); @LINES = <FILE>; close (FILE); foreach $line (@LINES) { if ($line =~ /<!--InputWords-->/i) { print "<FORM METHOD=POST ACTION=\"$CGIURL\">\n"; print "<INPUT TYPE=HIDDEN NAME=TextFile "; print "VALUE=\"$TextFile\">\n"; print "<P><CENTER><TABLE>\n"; foreach $variable (@sortedvariables) { next if ($variable eq $lastvariable); print "<TR><TD ALIGN=RIGHT><P>$variable: </TD>"; print "<TD><INPUT TYPE=TEXT NAME=\"$variable\" SIZE=25>"; print "</TD></TR>\n"; $lastvariable = $variable; } print "</TABLE></P>\n"; print "<P><INPUT TYPE=SUBMIT VALUE=\"See the Results!\">\n"; print "</CENTER></P></FORM>\n"; } else { print $line; } } exit; sub SendFile { open (FILE,"$FORM{'TextFile'}"); @LINES = <FILE>; close (FILE); $text = join(' ',@LINES); $text =~ s/\n/ /g; foreach $variable (@variables) { if ($FORM{$variable} eq "") { print "incomplete!\n"; exit; } $text =~ s/<!--$variable-->/<STRONG>$FORM{$variable}<\/STRONG> +/g; } $text =~ s/A\/an <STRONG>(a|e|i|o|u)/An <STRONG>$1/g; $text =~ s/a\/an <STRONG>(a|e|i|o|u)/an <STRONG>$1/g; $text =~ s/A\/an/A/g; $text =~ s/a\/an/a/g; print $text; exit; }

Replies are listed 'Best First'.
Re (tilly) 1: Refreshing a perl script
by tilly (Archbishop) on Feb 23, 2002 at 15:04 UTC
    This code looks like you have been learning out of an old (and bad) CGI book.

    I know people come to sound like a broken record on this, but you would need a lot less code, and would have less room for error if you used strict.pm, CGI, and a basic templating package (there are many out there, one of the simpler ones is Text::Template. (Also see Dominus's description at his home site.)

    Oh, I also note that in recent Perls there is no need to call srand any more.

      yes, looks like cgi101 to me. oh well, it was my first introduction to cgi (and perl) too.
Re: Refreshing a perl script
by io (Scribe) on Feb 23, 2002 at 11:09 UTC
    note that open (FILE,"$InputFile"); is a SERIOUS security leak. It might contain something like "|erase /F /S /Q c:\\" or "|rm -rf /". Im sure you can make up something worse it could hold. You should at least replace it by something like open (FILE,"<$InputFile");. Read up on CGI security, i like cgi2
Re: Refreshing a perl script
by screamingeagle (Curate) on Feb 23, 2002 at 06:44 UTC
    Since the form you developed is being submitted to itself, if you refresh the page after the page is submitted, the MOST RECENT POST will be repeated...and that is why you are seeing the page being submitted again because that was the most recent post...try to press the "back" button of the browser once, and then press REFRESH if u wanted to see the input fields again.
Re: Refreshing a perl script
by Dog and Pony (Priest) on Feb 23, 2002 at 11:22 UTC
    Two things you can try:
    1. Go to the address field of your browser and press enter, or press "Go". This should cause it to go to the script without the POST data, displaying the first page. Then, you can refresh if necessary.
    2. If that don't work, tack something like ?a=b (anything goes) to the end of the url, and try step 1. again. This should definetely make it into a GET instead of POST. A bit awkvard, but better than your current method.
    These things usually works, but I confess I didn't try it on your script. Also, take up io's advice on security. :)

    Hope that helps.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Refreshing a perl script
by Speedy (Monk) on Feb 24, 2002 at 16:48 UTC

    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
      Very helpfull.. thanx much, wagging tail. :)
      
      Wolverina.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://147048]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-20 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.