- From your node title, it sounds like you may be making a classic new CGI programmer's thought error. You are not coding a continuous process. Each request your CGI script gets runs as a separate, unique invokation of your script. The last person I taught CGI scripting had to be reminded of this fact about 1000 times before she got it.
- Be very careful taking filenames as input from a cgi. There are many nodes about this. Check out ovid's cgi tutorial for a treatment of this. Also read perlsec.
Are you familiar with the state machine model of CGI programming? (Try a Super Search for "state machine" more info.) The idea is that your CGI script needs a way to remember what part of the interactive process it is presenting.
Your script would probably have three states, let's call them "edit", "preview", and "commit".
- edit: The edit state should be the defualt state. The script will present a form for the user to fill out. Pressing submit will request that the script is run as preview.
- preview: This state can lead to either edit or commit. It should print the user's responses to the form in an easy to read format.
- commit: Writes the file and displays a message, like "file foo saved" probably has a link back to the edit state.
"Well and good, but how do organize this?" you might be thinking. The most common method is to use a parameter with a name like "state" or "action". You can then define the behavior for each state by using a big fat case statement. There is no native case statement in perl, but a chain of elsif statements works very nicely.
# Set state to edit if not defined.
param(state)||param(name=>'state', value=>'edit');
if (param('state') eq 'edit')
{ # display the form
print start_form('POST',self_url());
# blah blah blah
print submit('state','preview'), end_form();
} elsif (param('state' eq 'preview')
{ # display the results, prettily.
print start_form('POST',self_url()),
# A bunch of hidden fields to pass our data along
hidden(parameter-n, value),
# This button commits the data to a file.
submit('state','commit'),
# This button returns to the edit form, to make changes.
submit('state','edit'),
end_form();
} elsif ( param('state') eq 'commit')
{ # Save the file
# Print some nice messages.
} else
{ # General purpose error. You should never wind up here
# Trap it just in case.
}
I personally, prefer to use here docs for embedding HTML in my perl. I find CGI.pm's HTML generation cumbersome. But some people really like it, TIMTOWTDI. I always use CGI.pm for parameter management though.
# A sample here doc
print <<"END_OF_HTML";
Blah laalbalalsdfasdf
asdf
blaha
END_OF_HTML
TGI says moo
- 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
- Link using PerlMonks shortcuts! What shortcuts can I use for linking?
-
See Writeup Formatting Tips and other pages linked from there for more info.