Global Variables - What is in Memory?

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

From: Justin Luster
Subject: Global Variables - What is in Memory?
Date: 02:53 on 05 Nov 2004
------=_NextPart_000_0010_01C4C29F.9D51AA20
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

I have been working with Mod_Perl for a few years now and have been
fairly successful.  I thought that I had a fair understanding of how
things worked until today.  I'm using Mod_Perl version 1.0 and I'm just
using the Apache::Registry to speed up regular Perl scripts.

 

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 = "";

}

 

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.  I know global variables are bad but
sometimes you have to use them.  What are some good work-a-rounds?  

 

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

 

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

 

I would appreciate any help.

 

Thanks.

 

 

 


------=_NextPart_000_0010_01C4C29F.9D51AA20
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html>

<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">


<meta name=3DGenerator content=3D"Microsoft Word 10 (filtered)">

<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:"MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
	{font-family:"\@MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{font-family:Arial;
	color:windowtext;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
	{page:Section1;}
-->
</style>

</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I have been working with Mod_Perl for a few years now and have =
been
fairly successful.&nbsp; I thought that I had a fair understanding of =
how
things worked until today.&nbsp; I&#8217;m using Mod_Perl version 1.0 =
and I&#8217;m
just using the Apache::Registry to speed up regular Perl =
scripts.</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>Problem #1:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I forgot to initialize a global variable!&nbsp; Bummer.&nbsp; In =
my
code I declare global variables like this:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>use strict;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>package mylib;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'># Globals: Set them below</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>$mylib::strGlobalStudyName =3D &quot;&quot;;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I then initialize them with this:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>sub InitializeMylibGlobals</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>{</span></font></p>

<p class=3DMsoNormal style=3D'text-indent:.5in'><font size=3D3 =
face=3D"Times New Roman"><span
style=3D'font-size:12.0pt'>$mylib::strGlobalStudyName =3D =
&quot;&quot;;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>}</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I call this initialize function every time the script =
runs.&nbsp;
Anyway I forgot and so one of my global variables was not =
initialized.&nbsp; I
was shocked when I discovered the value from the global variable from =
another
user stick around.&nbsp; I know global variables are bad but sometimes =
you have
to use them.&nbsp; What are some good work-a-rounds?&nbsp; =
</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>But my big question is, is it safe to use global variables at =
all?&nbsp;
Am I guaranteed that if I initialize them at the start of my script that =
they
won&#8217;t be set by another process before I use them later in my =
script???????????????&nbsp;
</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>Problem #2:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I realize that when a Perl library file is =
&#8220;required&#8221; in
goes into memory.&nbsp; 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.&nbsp; I =
thought
that only files that were &#8220;required&#8221; went into memory.&nbsp; =
Today
I was surprised when it appeared that my main script was in =
memory.&nbsp; The
main file that the web users hit in their URL is called =
&#8220;MyAdmin.pl&#8221;.&nbsp;
This file looks like this:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>use strict;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>admin::main();</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>package admin;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>sub main</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>{</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>}</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>1;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>This file is not required by any other script.&nbsp; On my =
machine I
had two different versions of &#8220;MyAdmin.pl&#8221;.&nbsp; They were =
in
different locations so:</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'><a =
href=3D"http://www.mysite.com/one/MyAdmin.pl">http://www.mysite.com/one/M=
yAdmin.pl</a></span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>and </span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'><a =
href=3D"http://www.mysite.com/one/MyAdmin.pl">http://www.mysite.com/two/M=
yAdmin.pl</a></span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>Well, the script in directory &#8220;one&#8221; started to run =
the
version in directory &#8220;two&#8221;.&nbsp; I would understand this =
behavior
if their was a &#8220;required&#8221; library file, but in this case =
there are
two main files.&nbsp; Does the package &#8220;admin&#8221; stay in =
memory even
though I did not &#8220;require&#8221; it?&nbsp; 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.</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>I would appreciate any help.</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>Thanks.</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D3 face=3D"Times New Roman"><span =
style=3D'font-size:
12.0pt'>&nbsp;</span></font></p>

</div>

</body>

</html>

------=_NextPart_000_0010_01C4C29F.9D51AA20--

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