Hi Justin,

> use strict;
> package mylib;
> # Globals: Set them below
> 
> $mylib::strGlobalStudyName = "";

You could also just say "our $strGlobalStudyName;" here.

> 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.

That's what they do.  Globals don't go out of scope, so they retain
their values.

> I know global variables are bad but sometimes you have to use them. 
> What are some good work-a-rounds?  

Using lexicals, or sometimes singleton objects with accessors.  It kind
of depends on why you thought you needed to use a global in the first
place.

> 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???????????????

You are guaranteed of that, because each mod_perl request is handled by
a separate process in mod_perl 1.  (Even if you use a threaded MPM in
MP2 you are still safe unless you explicitly declare the variable shared
between threads.)

> Today I was surprised when it appeared that my main script was in
> memory.

Yes, that's what Apache::Registry does.  This is described in the
Apache::Registry documentation and other places in the mod_perl docs
online.

> 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. 
> Does the package âadminâ stay in memory even though I did not
> ârequireâ it?

Yes.  Apache::Registry turns it into a module and requires it.  That's
why Registry is fast.  If you didn't declare a package name in that
file, each one would get a different auto-generated package name and
they would not overlap.  The automatic package name is based on the
URL.  Look at the Registry code if you want to see how this works.  It's
short and pure perl.

> 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.

It just checks to see if the file has been updated and reloads it if it
has.

- Perrin


-- 
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