Converting a Complex Script to use CGI

Discussion in 'Perl Misc' started by spud, Dec 29, 2006.

  1. spud

    spud Guest

    Hi,
    I have a script that runs as a unix shell script that takes an XML
    file then parses it performing the tasks that are in the file. in the
    event that I need to ask a question or gain user input the question is
    held in the xml file allowing me to ask the question and store the
    result of <STDIN>...
    However I have now been asked to convert this to run as a web page so
    that others that are not trust worthy of unix access may use it :(
    I have never written a CGI script before so the first thing I tried
    was to replace my code that asked questions with a auto generated form
    html form... The trouble is that when I do this my script simply
    rattles through and creates the form then continues on trying to
    execute the commands in the rest of the file without waiting for me to
    post the form back with the information in it :(
    How would I go about creating a script that asks some questions then
    performs commands from them?

    Thanks in advance

    Peter
     
    spud, Dec 29, 2006
    #1
    1. Advertising

  2. spud wrote:
    > However I have now been asked to convert this to run as a web page so
    > that others that are not trust worthy of unix access may use it :(
    > I have never written a CGI script before [...]
    > How would I go about creating a script that asks some questions then
    > performs commands from them?


    The first step would probably be to ask in a NG that actually has something
    to do with CGI and/or web pages.

    jue
     
    Jürgen Exner, Dec 29, 2006
    #2
    1. Advertising

  3. spud

    Guest

    spud wrote:
    > How would I go about creating a script that asks some questions then
    > performs commands from them?


    I presume you are using the CGI.pm module...

    The problem seems to be that you are not separating out your runmodes
    (states). Your program needs to see if any CGI paremeters have been
    set. If not, print (only) the form and exit. If params are defined,
    process them accordingly. For example (an EXTREMELY simplified
    program):

    #!/usr/bin/perl
    use strict; use warnings;
    use CGI qw{ :standard Dump };

    print header();
    if ( param() ) {
    #parameters are defined - do something with them
    print
    "Your parameters are: ",
    p( Dump() );
    }
    else {
    #No parameters are known - show the input form
    print
    start_form(),
    textfield({-name => 'stuff'}),
    submit(),
    end_form();
    }
    print end_html();

    __END__

    The first condition of the if{} block is executed only if parameters
    have been passed to the program. If not, the else{} block is executed,
    which prints the form (only). When the user fills out form elements
    and hits the submit button the program is re-executed (with parameters
    passed to the program), which invokes the processing defined in the
    if{} block.
     
    , Dec 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    588
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    764
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    700
    Robert Mark Bram
    Feb 4, 2007
  4. Guy
    Replies:
    4
    Views:
    861
    Malcolm Dew-Jones
    Dec 11, 2003
  5. kath
    Replies:
    4
    Views:
    702
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page