views:

273

answers:

6

Hello everybody,

I'm somewhat new to Perl/CGI, and I'm coming from a Java/JSP background.

I'm writing a small prototype and need to load some "heavy" data (~200MB) into a data structure.

Now, I'd obviously like to avoid loading the data with every request. So far I managed to use a "static" variable (one enclosed in a {} block), but this seems to work for a few requests. After some inactivity time, the next request will have to load the data again.

From my JSP experience, this would seem to be a sort of session variable that remains available until the session expires.

How can I set a "global" or "application" variable? Not sure if these terms apply to CGI... Is it possible to have a variable shared by all sessions of an application?

Btw, I'm just using "use CGI qw(:standard)" at the moment.

Thanks

+5  A: 

CGI programs are run in a separate process for each request. This is part of the CGI protocol.

So this isn't possible so long as you're bound to CGI. Are you sure that you're bound to CGI?

If you're running Apache, and that Apache has mod_perl either compiled in or available as a dynamically loaded module, it's possible to run the perl scripts in process and re-use the data; there's even a compatibility mode wherein you can write your CGI scripts as normal (using "use CGI") and they get automatically mod-perl-ized so that stuff inside BEGIN blocks is only run once.

Daniel Martin
+5  A: 

CGI scripts are executed and then terminate after each request. Your 200 MB variable will be loaded each time.

You should put this data into a database or other structured format that will allow you to load data only as you need it.

Look into something like MLDBM, DBD::SQLite, or DBM::Deep

See Coping with Scoping for info on variable scope in Perl.

Ovid's CGI Course is also a good resource for learning to write CGI scripts in Perl.

daotoad
Not bound by CGI per se, I'm running an Isapi handler from IIS 7 (gasp!), which handles .pl extensions.This is for a fast internal prototype, so a DB solution would be quite more time intensive than I'd like...thanks!
domyalex
+1  A: 

Have you looked at the Storable module and its freeze/thaw methods to freeze your object structure and temporarily store it? CPAN > Storable

Storable would allow you to write and retrieve your structure to and from anything that can be considered a database, including Berkley, or even flat files. While 200 MB is a large chunk to store, you might regroup the data into smaller hashes that easily go back together.

And it is super fast.

Fran Corpier
+1  A: 

Try CGI::Session module.

Note however that like Daniel Martin and daotoad wrote peristency is outside scope of CGI; you would have to emply some kind of extra storage to save state.

Jakub Narębski
A: 

what the function of a subclass?

A: 

I would consider looking into CGI::Fast if I were you. CGI::Fast makes it easy to write a persistent CGI program. Another good option, depending on what your data structure is like, is to use Cache::Memcached (or its faster cousin Cache::Memcached::XS) to move the data structure out of your program but still leave it in memory.

Chas. Owens