Mailing List Archive

Weird variable scope problem
Unfortunately, I don't have a readily available example which can reproduce
this problem as I'm seeing it in a large code base. I'm really just looking
for ideas that might point me in the right direction.

Basically, I have a module which has a some lexically scoped variables in
it. These variables are used by accessor methods in the package. The
problem is that, if I use this module in certain parts of my application,
these variables are mysteriously empty even though I am certain that I've
already initialized them. Is there a way for a single perl interpreter to
have multiple copies of a lexical variable?

--
John Dunlap
*CTO | Lariat *

*Direct:*
*john@lariat.co <john@lariat.co>*

*Customer Service:*
877.268.6667
support@lariat.co
Re: Weird variable scope problem [ In reply to ]
On Fri, Mar 25, 2016 at 10:11 AM, John Dunlap <john@lariat.co> wrote:
>
> Basically, I have a module which has a some lexically scoped variables in
> it. These variables are used by accessor methods in the package. The
> problem is that, if I use this module in certain parts of my application,
> these variables are mysteriously empty even though I am certain that I've
> already initialized them.
>

How do you initialize them? In code that runs at compile-time in the same
module? In calls from httpd.conf or startup.pl? Is this an OO module with a
declared package name?


> Is there a way for a single perl interpreter to have multiple copies of a
> lexical variable?
>

No, but keep in mind that mod_perl is running inside multiple httpd
processes and each one has a separate interpreter with unique copies of all
lexicals.

- Perrin
Re: Weird variable scope problem [ In reply to ]
They are initialized in my handler method, at the start of the request so
they should be initialized at runtime. Basically, I have a singleton object
which extends Class::Singleton which functions as an IOC container.
However, because I have multiple virtual hosts and because the database
connection is inside the IOC container, I have to have a separate object
graph for each virtual host to prevent them from inadvertently talking to
each other's databases. When I get an instance of the IOC container, I pass
it the name of the site. Internally, it then maintains 2 lexically scoped
variables; A hash, keyed by the name of the virtual host, and a scalar for
storing the name of the current virtual host so that subsequent calls to
the IOC container to get references to objects don't require a site name.
This initialization happens at the start of every request to ensure that
the right object graph is referenced when executing the request. However,
in some older sections of the code base, when I attempt to references the
IOC container these 2 lexically scoped variables are somehow empty, even
though they were initialized at the start of the request, and then I get an
error for attempting to call a function on an undefined value.

I know that the code for some older areas of the application are loaded
dynamically through evals and requires. Is it possible for that kind of
dynamic loading to result in multiple lexical scopes for the same package?

On Fri, Mar 25, 2016 at 3:58 PM, Perrin Harkins <pharkins@gmail.com> wrote:

> On Fri, Mar 25, 2016 at 10:11 AM, John Dunlap <john@lariat.co> wrote:
>>
>> Basically, I have a module which has a some lexically scoped variables in
>> it. These variables are used by accessor methods in the package. The
>> problem is that, if I use this module in certain parts of my application,
>> these variables are mysteriously empty even though I am certain that I've
>> already initialized them.
>>
>
> How do you initialize them? In code that runs at compile-time in the same
> module? In calls from httpd.conf or startup.pl? Is this an OO module with
> a declared package name?
>
>
>> Is there a way for a single perl interpreter to have multiple copies of a
>> lexical variable?
>>
>
> No, but keep in mind that mod_perl is running inside multiple httpd
> processes and each one has a separate interpreter with unique copies of all
> lexicals.
>
> - Perrin
>
>


--
John Dunlap
*CTO | Lariat *

*Direct:*
*john@lariat.co <john@lariat.co>*

*Customer Service:*
877.268.6667
support@lariat.co
Re: Weird variable scope problem [ In reply to ]
Hi John,

Pretty hard to know what's going on without looking at actual code, but it
could be some variation of this type of problem:
http://perl.apache.org/docs/1.0/guide/porting.html#Name_collisions_with_Modules_and_libs

You can easily run into trouble with code that either doesn't use package
names or exports things from use() calls.

> Is it possible for that kind of dynamic loading to result in multiple
lexical scopes for the same package?

No, Perl doesn't support that.

- Perrin


On Fri, Mar 25, 2016 at 4:23 PM, John Dunlap <john@lariat.co> wrote:

> They are initialized in my handler method, at the start of the request so
> they should be initialized at runtime. Basically, I have a singleton object
> which extends Class::Singleton which functions as an IOC container.
> However, because I have multiple virtual hosts and because the database
> connection is inside the IOC container, I have to have a separate object
> graph for each virtual host to prevent them from inadvertently talking to
> each other's databases. When I get an instance of the IOC container, I pass
> it the name of the site. Internally, it then maintains 2 lexically scoped
> variables; A hash, keyed by the name of the virtual host, and a scalar for
> storing the name of the current virtual host so that subsequent calls to
> the IOC container to get references to objects don't require a site name.
> This initialization happens at the start of every request to ensure that
> the right object graph is referenced when executing the request. However,
> in some older sections of the code base, when I attempt to references the
> IOC container these 2 lexically scoped variables are somehow empty, even
> though they were initialized at the start of the request, and then I get an
> error for attempting to call a function on an undefined value.
>
> I know that the code for some older areas of the application are loaded
> dynamically through evals and requires. Is it possible for that kind of
> dynamic loading to result in multiple lexical scopes for the same package?
>
> On Fri, Mar 25, 2016 at 3:58 PM, Perrin Harkins <pharkins@gmail.com>
> wrote:
>
>> On Fri, Mar 25, 2016 at 10:11 AM, John Dunlap <john@lariat.co> wrote:
>>>
>>> Basically, I have a module which has a some lexically scoped variables
>>> in it. These variables are used by accessor methods in the package. The
>>> problem is that, if I use this module in certain parts of my application,
>>> these variables are mysteriously empty even though I am certain that I've
>>> already initialized them.
>>>
>>
>> How do you initialize them? In code that runs at compile-time in the same
>> module? In calls from httpd.conf or startup.pl? Is this an OO module
>> with a declared package name?
>>
>>
>>> Is there a way for a single perl interpreter to have multiple copies of
>>> a lexical variable?
>>>
>>
>> No, but keep in mind that mod_perl is running inside multiple httpd
>> processes and each one has a separate interpreter with unique copies of all
>> lexicals.
>>
>> - Perrin
>>
>>
>
>
> --
> John Dunlap
> *CTO | Lariat *
>
> *Direct:*
> *john@lariat.co <john@lariat.co>*
>
> *Customer Service:*
> 877.268.6667
> support@lariat.co
>