Can I have an application scope variable in Perl?
I'm a bit new to Perl / CGI and I'm coming from a Java / JSP background.
I'm writing a small prototype and you need to load some "heavy" data (~ 200MB) into a data structure.
Now, I obviously would like to avoid loading data with every request. So far I've managed to use a "static" variable (one enclosed in a {} block), but this seems to work for multiple queries. After some time of inactivity, the next request will have to download the data again.
From my JSP experience, this is like a session variable that remains available until the session ends.
How do I set the variable "global" or "application"? Not sure if these terms apply to CGI ... Is it possible to have a variable shared by all sessions of the application?
Btw, I'm just using "use CGI qw (: standard)" for now.
The CGI scripts are executed and then terminated after each request. Your 200MB variable will load every time.
You should put this data into a database or other structured format that will allow you to load the data only as needed.
Look for something like MLDBM , DBD :: SQLite or DBM :: Deep
See "Copy and Show" for information on variable scope in Perl.
The Ovid CGI Course is also a good resource for learning how to write CGI scripts in Perl.
a source to share
CGI programs run in a separate process for each request. This is part of the CGI protocol .
So this is not possible if you are CGI related. Are you sure you are CGI related?
If you are using Apache and that Apache has mod_perl compiled or available as a dynamically loadable module, it can run perl scripts in the process and reuse data; there is even a compatibility mode where you can write your CGI scripts as usual (using "use CGI") and they are automatically modified automatically so that stuff inside BEGIN blocks only runs once.
a source to share
Have you looked at the Storable module and its freeze / thaw methods to freeze the structure of your object and temporarily save it? CPAN> Storable
Storable will allow you to write and retrieve your structure from anything that can be considered a database, including Berkley or even flat files. While 200MB is a big chunk of storage, you can rearrange data into smaller hashes that come back together easily.
And it's super fast.
a source to share
Try CGI :: Session .
Note, however, that, like Daniel Martin and the Daotau, it is written that this is an opacity outside the CGI area; you will need to use some kind of additional storage to save the state.
a source to share
I would look into CGI :: Fast if I were you. CGI :: Fast makes it easy to write a persistent CGI program. Another good option, depending on your data structure, is to use Cache :: Memcached (or its faster cousin Cache :: Memcached :: XS ) to move the data structure out of your program, but leave it in memory.
a source to share