Re: Is it me or is mod_perl extremely dangerous?

André Warnier <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
Desilets, Alain wrote:
> I'm a complete newbie to mod_perl, and after reading the following documentation:
> 
> http://perl.apache.org/docs/1.0/guide/porting.html
> 
> I am scared witless by the fact that many variables don't get reinitialized between calls to the CGI scripts.
> 
> Particularly scary is the example provided on that page, where the authentication status is stored in a global variable that doesn't get reinitialized. In that example, if Joe logs into the system, and Jane then runs the script, she can get access to the system also without every logging in, because Joe's authentication status is still there. YIKES!
> 
...
Well, yes.  mod_perl does not like sloppy programming.
On the other hand, if you take care to always initialise your variables at the beginning 
of the script, you will avoid such problems.
And you do initialise your variables always, don't you ?

On the other hand, these global variables which do not get re-initialised automatically, 
are extremely useful when what you want is just that : cheap persistence between calls.
Imagine a big table that you need to load once, and then just read repeatedly within your 
script.  Instead of repeatedly reloading the table at each call to your script, you can 
just read it once (when it is still undefined), and then forever use the content across 
your cgi-bin's executions.

our $big_table;
unless (defined $big_table) {
   $big_table = go_fill_it($from_file) or die "cannot fill the table";
}

Just remember that your script can live in several separate Apache children, and each one 
has its own copy of your script (and of the table).
So it is not really an Apache-global table.  It is only persistent within this Apache 
child, as long as this child lives.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.