On Thursday 4 November 2004 09:53 pm, Justin Luster wrote:

> Problem #1:

> I forgot to initialize a global variable!  Bummer.  In my code I declare
> global variables like this:

> use strict;
> package mylib;
> # Globals: Set them below
> $mylib::strGlobalStudyName = "";
> I then initialize them with this:

> sub InitializeMylibGlobals
> {
> $mylib::strGlobalStudyName = "";
> }

Why initialise your data in a seperate function? Is there a reason for not 
doing: 

use strict;
package mylib;
$strGlobalStudyName = "value"?

and then just referencing it as "$mylib::strGlobalStudyName" when you need it?

> I call this initialize function every time the script runs.  Anyway I
> forgot and so one of my global variables was not initialized.  I was
> shocked when I discovered the value from the global variable from
> another user stick around.  

What else would you expect? It's a global, therefor unless you change the 
value it holds the last thing it was set to. You are running Apache::Registry 
and thus the code is not recompiled every time, so the values of globals from 
previous requests are retained (on a per process basis).

> What are some good work-a-rounds?

- If you want to go that far, you could write your own handler that always 
initialises your data. 
- If the values are constants, define them as such.
- Use a globals object, have the new function do your initialisation, your 
script can't access it without explictly creating the object (so you know 
your scripts can't forget to call it) and it goes out of scope at the end of 
your script so the values are discarded.

> But my big question is, is it safe to use global variables at all?  Am I
> guaranteed that if I initialize them at the start of my script that they
> won't be set by another process before I use them later in my
> script???????????????

They can't be set in another process, each process has it's own copy. It can 
be set by another script being run by the same apache process however. 
Scripts are atomic so you can't have a situation where one script affects 
another that is still running (unless you deliberately set up shared memory).

> Problem #2:

> I realize that when a Perl library file is "required" in goes into
> memory.  And then the next time that that library is needed it will just
> be used from memory and not have to be read in again.  I thought that
> only files that were "required" went into memory.  Today I was surprised
> when it appeared that my main script was in memory.  

This is what mod_perl does. 

> The main file that the web users hit in their URL is called "MyAdmin.pl". 
> This file looks like this:

> use strict;
> admin::main();

> package admin;
> sub main
> {
> }
> 1;

> This file is not required by any other script.  On my machine I had two
> different versions of "MyAdmin.pl".  They were in different locations
> so:
> http://www.mysite.com/one/MyAdmin.pl
> and
> http://www.mysite.com/two/MyAdmin.pl
> <http://www.mysite.com/one/MyAdmin.pl>

> Well, the script in directory "one" started to run the version in
> directory "two".  I would understand this behavior if their was a
> "required" library file, but in this case there are two main files.

Apache::Registry should mangle scripts to seperate names so that they don't 
conflict. However if you are using the same package name in each case, the 
last definition to be loaded will be used. Ie. if both 
one/MyAdmin.pl and two/MyAdmin.pl define admin:main(), whichever file loaded 
last will have it's definition used. (Check your error logs for 
"redefinition" warnings).

> Does the package "admin" stay in memory even though I did not "require"
> it?

It appears to be defined within your script, packages are not independent of 
the files they are in, so you've defined and used it, the script is not 
discarded (because you're using Apache::Registry) so the package stays in 
memory.

> I have been lead to believe that it does not stay in memory because 
> when I update MyAdmin.pl with new code I see the new behavior without
> having to reset Apache.

Apache::Registry caches scripts and reloads them when they change. So when you 
edit one of your scripts it will be reloaded, becoming the "latest defintion" 
of admin::main() and overwriting the previous version.

see also: http://perl.apache.org/docs/1.0/guide/intro.html#Apache__Registry

-- 
"The Universe doesn't give you any points for doing things that are easy."
- Sheridan to Garibaldi in Babylon 5:"The Geometry of Shadows"

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html

Reply via email to