I'll try to answer your question, but first a few curmudgeonly comments:

  1. 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.
  2. 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.
  3. 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".

    "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


    In reply to Re: Why is my CGI.pm script trying to assign data before pressing submit. by TGI
    in thread Why is my CGI.pm script trying to assign data before pressing submit. by dru145

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  4. Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  5. Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  6. Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  7. Please read these before you post! —
  8. 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
  9. 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;
  10. Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  11. See Writeup Formatting Tips and other pages linked from there for more info.