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

Hi all, I need to write a script that asks the user for a text file, read it into a data structure, ask the user for more info (I made it up to here), and update the data structure (DOH!). Bascially, I need to ask the user twice for information, but the second request is dependant on the first. I realize I can make a seperate script and pass the data structure as a parameter string, but this is highly inefficient. Thanks in advance. Vince

Replies are listed 'Best First'.
Re: interactive CGI script?
by Cody Pendant (Prior) on Mar 04, 2003 at 23:14 UTC
    Why is it highly inefficient? I don't get that part.

    What I do is have a "mode" for each stage.

    The mode would be undef on first loading the page. Submitting the form would include a hidden field or whatever that said "mode is now 'secondstage'" and so on.

    So the code ends up being two or three large blocks like this:

    if($mode eq ''){ # stuff that happens on the first page } if($mode eq 'secondstage'){ # stuff that happens after the upload } if($mode eq 'finalstage'){ # stuff that happens after their secondstage input } if($mode eq 'finished'){ print "Thanks, we're done."; }

    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
        Thanks a bunch. This is exactly what I am looking for. Vince

      Rather than executing a new if() statement for each mode, you might want to check out elsif() instead. This has the advantage that your script will exit quicker as it will not check every single if() statement to see if it is true or not. It will skip to the end of the if/elsif blocks once it has been satisfied in one of the conditionals. Your example would change to the following:

      my $mode = param('mode'); # Main if (not defined $mode) { # ... } # After upload elsif ($mode eq 'secondstage') { # ... } # Final stage elsif ($mode eq 'finalstage') { # ... }

      To create cleaner code, I tend to do something like this:

      sub main { # Main page } sub secondstage { # After upload } sub finalstage { # final stage } sub _invalid { # Output error message: no such mode } my $modes = { '' => \&main, secondstage => \&secondstage, finalstage => \&finalstage, _invalid => \&invalid }; my $mode = param('mode'); $modes->{ exists $modes->{$mode} ? $mode : '_invalid' }->();


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

        Will using this strategy preserve the state of the script (i.e. contents of data strcutures) per new HTTP request. i.e. when I go from one mode to another will the global variables be reset? It seems like they will 'cause when you go through one mode the perl script will exit. i used CGI::Session to get around this problem. Thanks.