http://www.perlmonks.org?node_id=1026907

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

I am fairly new to perl and really like it. I have been able to use it to write scripts to handle data coming back from a single web page. I am at the edge of my knowledge with a new project. I have multiple data form pages (e. g., Form1.html, Form2.html,.....) that I want to present randomly. I also want to get data from each of these forms, too. An initial form page collects some background info and then the task is to randomly choose and present six forms and to get their data. I have been able to set up the randomization process and to get the pages to serve.....

Everything *seems* works until the first random form. From what I can tell, once that first form is submitted, the script starts anew (i. e., a new process or a new thread). I initially thought/"hoped" that each time the same client called the script in working through the six random pages, the script would take up where it last left off (i. e., serving a page and storing the form data). This is not happening, though

I've done some reading about shared variables and about threads, but I am not sure that this is what I need. Could anyone give me a nudge or point me in the direction I need to go? Thank you...

Replies are listed 'Best First'.
Re: One script, multiple html forms?
by davido (Cardinal) on Apr 03, 2013 at 22:32 UTC

    CGI is a stateless environment. If you wish to maintain state across CGI requests, you'll need two things: Somewhere to store session data, and some way to securely identify which session belongs to who.

    CGI::Session can help with this. I prefer working with the Mojolicious web framework, though, which makes session handling pretty simple.

    I know it's terribly outdated, but CGI Programming with Perl (O'Reilly) does help to explain the nuts and bolts that one isn't fully exposed to when using a more modern framework. Pick up a used copy (one penny, plus shipping on Amazon, for example). 2nd edition is better than 1st, but both are over a decade old. Nevertheless, understanding the mechanics is still relevant even if you're moving beyond bare CGI into modern frameworks sitting on top of Plack.

    Update: With respect to Mojolicious, be sure to take the advice of the tutorials by starting with a Mojolicious::Lite app. Most of the time that's adequate. You can start with the templates embedded directly in the app, and later explode them out to their own separate files. In my experience you don't really need a full Mojolicious app (with router and controllers separated) until the application grows large.


    Dave

Re: One script, multiple html forms?
by TJPride (Pilgrim) on Apr 04, 2013 at 12:23 UTC
    Have the master script check for a GET variable called "session" or whatever. If it doesn't exist, create one (32-character md5_hex of microtime and rand) and forward to a new URL with the session variable tacked on the end. If it does exist, check your database for stored forms with that session ID, and randomly pick from any of the six forms the user hasn't done yet. Pass the form type as a hidden field with each one so when it's submitted you know where to store the data. When you detect that six forms have been stored, display a thank-you message.

    I use similar techniques for multiple-page sessions all the time. Session ID's in the URL are fairly reliable.