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

Hi all,

This is my first post here...

I'm writing a web interface using CGI::Application for a project that houses numerous methods within a dozen modules.

One particular process displays a template to the user with passed in data, and then has four more steps before completing. The result of this procedure will be the modification of an elaborate data structure. The data structure may need to be modified along the way.

Given that I can get through the web GUI portion of the process with just a couple of params, I'd like to find a way to retain the entire data structure without having to re-instantiate it (which requires rebuilding numerous objects etc), or pass each variable within it as a param on each CGI invocation.

Can I, and is it plausible to create a cyclic ref redundancy of the data structure, store the memory location in a variable, pass the variable data as a param, and then recreate the data based on the variable that contains the memory location at the end of the entire CGI process? Is exploiting Perl's garbage collection like this possible, and ok to do?

I'd like to be able to 'carry' the data structure without having to 'store' it to disk, if possible

Steve

Replies are listed 'Best First'.
Re: CGI and persistent data
by porta (Sexton) on Aug 27, 2009 at 23:52 UTC

    CGI::Application::PLugin::Session + Data::Dumper can do the trick?

    You can dump the variables to the session and then eval them each time. If you're using CGI::App you can even use cgiapp_prerun / cgiapp_postrun to handle the dump/eval to/from session and set the values as parameters on CGI::App.

    That way, you only need to get/set the values as params from the $self (or whatever you use to represent your CGI::App object). That sounds simple and clean for me.

      Thanks for the feedback...

      I despise having to create a 'GUI' for what I'm doing, as I'd much prefer to leave things cli-only (wouldn't most of us). Although I have no experience or understanding of 'session' regarding HTTP, I'll take a look at what you've suggested, and review the docs for the Session module.

      ...my question regarding the exploitation of the Perl garbage collection remains however, and it would be interesting to get feedback on that...

      -sb

Re: CGI and persistent data
by astroboy (Chaplain) on Aug 28, 2009 at 00:17 UTC
    I'd like to be able to 'carry' the data structure without having to 'store' it to disk, if possible

    well, you'll have to store the data structures somewhere. Your choices will probably be in a cookie, as a hidden param or as part of your session, And session info will typically either use a flat file or a db. As porta pointed out, you could just use CGI::Session or CGI::Application::Plugin::Session to do the latter. That would be my recommendation too, as it has a nice API for saving and retrieving the data.

      Thanks porta and astroboy,

      I'll look further into keeping sessions via the modules you've recommended, particularly the ones that fall into plugins to CGI:Application. I've found so far that the documentation has been reliable and easy-to-follow for plugins related to this module path.

      Cheers,

      Steve

        If you want to store them in RAM and access them easily, think about storing them as files in /dev/shm. This gives you the advantages of memory (fast, don't care about old data after reboot) and the easiness of file access.
        Whichever solution you're using: Always remember cleaning up old items.
Re: CGI and persistent data
by ganeshk (Monk) on Aug 28, 2009 at 07:17 UTC
    If you are not very much worried about memory usage you can clone the data structure and use the cloned data structure. This way you can easily preserve the original data structure. Please take a look at Clone.