RE: Global Variables - What is in Memory?

[prev] [thread] [next] [Date index for 2004/11/05]

From: Justin Luster
Subject: RE: Global Variables - What is in Memory?
Date: 17:18 on 05 Nov 2004
Thanks.

So what you are saying is that 

http://www.mysite.com/one/MyAdmin.pl
and
http://www.mysite.com/two/MyAdmin.pl

get different package names (because they are in different directories)
and so stay separate, but because I have declared a package inside of
them with a common name that package goes into a common package name and
will be shared by both scripts?

Thanks Perrin.

-----Original Message-----
From: Perrin Harkins [mailto:perrin@xxxx.xxx] 
Sent: Friday, November 05, 2004 9:03 AM
To: Justin Luster
Cc: modperl@xxxx.xxxxxx.xxx
Subject: Re: Global Variables - What is in Memory?

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


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

Global Variables - What is in Memory?
Justin Luster 02:53 on 05 Nov 2004

Re: Global Variables - What is in Memory?
Malcolm J Harwood 15:09 on 05 Nov 2004

[mp2] Apache::DBI and Apache::Status
Scott Scecina 15:53 on 05 Nov 2004

Re: [mp2] Apache::DBI and Apache::Status
Stas Bekman 21:22 on 05 Nov 2004

RE: Global Variables - What is in Memory?
Justin Luster 16:50 on 05 Nov 2004

Re: Global Variables - What is in Memory?
Malcolm J Harwood 17:22 on 05 Nov 2004

RE: Global Variables - What is in Memory?
Justin Luster 17:37 on 05 Nov 2004

Re: Global Variables - What is in Memory?
Perrin Harkins 17:02 on 05 Nov 2004

RE: Global Variables - What is in Memory?
Justin Luster 17:18 on 05 Nov 2004

RE: Global Variables - What is in Memory?
Perrin Harkins 17:26 on 05 Nov 2004

Generated at 11:26 on 21 Dec 2004 by mariachi v0.52