mason-globals_variable_hassles_etc

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: mason-users@lists.sourceforge.net
From: David Dyer-Bennet <dd-b@dd-b.net>
Subject: [Mason] Puzzled about global variables
Date: 07 Aug 2002 15:37:53 -0500

I'm trying to set up a site-wide global variable and a site-wide
per-request variable in my site-wide autohandler.  

I have code like this in the autohandler:

    <%init>
    # Globals initialized for each request
    $session = $m->comp('/comp/util/session');
    </%init>

    <%once>
    # Globals loaded once at startup
    use vars qw ($dcm $session);
    $dcm = $m->comp('/comp/util/dcm');
    </%once>

I'm getting a compilation error (line 61 in autohandler is the
assignment to $dcm in the %once section):

    error: 
    Error during compilation of /www/dcmdev/html/autohandler:
    Modification of non-creatable array value attempted, subscript -1 at
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm line 753. Stack:
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:753]
    [/www/dcmdev/html/autohandler:61]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:264]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:427]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Component.pm:298]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:284] 

    context: 
    ... 


    code stack: 
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:596
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:271
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:427
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Component.pm:298
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:284

Shutting down apache/mod_perl/Mason, clearing the cache, and
restarting apache/mod_perl/Mason, doesn't help at all.

I thought I had it working when I had only the per-request global
$session (at that point the use vars was in the %init section
instead). 

So what am I misunderstanding?

Another question -- I thought I could access $session in anything
running after the autohandler in the request, but then I got an error
on one of those.  Should I be putting a "use vars" in the components
that wish to *access* these global variables too?
-- 
David Dyer-Bennet, dd-b@dd-b.net  /  New TMDA anti-spam in test
 John Dyer-Bennet 1915-2002 Memorial Site http://john.dyer-bennet.net
        Book log: http://www.dd-b.net/dd-b/Ouroboros/booknotes/
         New Dragaera mailing lists, see http://dragaera.info


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

Subject: Re: [Mason] Puzzled about global variables
Cc: mason-users@lists.sourceforge.net
To: David Dyer-Bennet <dd-b@dd-b.net>
From: Benjamin Turner <bjturner@whowhere.com>
Date: Wed, 7 Aug 2002 14:03:57 -0700


On Wednesday, August 7, 2002, at 01:37  PM, David Dyer-Bennet wrote:

> I'm trying to set up a site-wide global variable and a site-wide
> per-request variable in my site-wide autohandler.
>
> I have code like this in the autohandler:
>
>     <%init>
>     # Globals initialized for each request
>     $session = $m->comp('/comp/util/session');
>     </%init>
>
>     <%once>
>     # Globals loaded once at startup
>     use vars qw ($dcm $session);
>     $dcm = $m->comp('/comp/util/dcm');
>     </%once>

	You can't reference $m in a %once section, since $m is only 
available in the context of a request, and %once sections 
technically happen outside of a request context. You may want to 
put code like the following in your %init section:

$dcm ||= $m->comp('/comp/util/dcm');

	<thinking_aloud>I wonder if it would be possible to define 
$m to be a mock object while running %once sections. This object 
could have just an AUTOLOAD method that spits out a meaningful 
error message about not being able to use $m inside %once 
sections....</thinking_aloud>

> Another question -- I thought I could access $session in anything
> running after the autohandler in the request, but then I got an error
> on one of those.  Should I be putting a "use vars" in the components
> that wish to *access* these global variables too?

	You shouldn't need to do that -- they all run in the same 
package namespace, so one "use vars" should do it. Please 
provide a small example that demonstrates this and we can 
probably help you figure out the problem.
		Ben
--
Benjamin John Turner          |  bjturner@lycos-inc.com
http://www.usfca.edu/turner/  |  bjturner@bigfoot.com



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

To: mason-users@lists.sourceforge.net
Subject: Re: [Mason] Puzzled about global variables
From: David Dyer-Bennet <dd-b@dd-b.net>
Date: Wed, 7 Aug 2002 16:20:14 -0500 (CDT)

Benjamin Turner <bjturner@whowhere.com> writes on 7 August 2002 at 14:03:57 -0700
 > 
 > On Wednesday, August 7, 2002, at 01:37  PM, David Dyer-Bennet wrote:
 > 
 > > I'm trying to set up a site-wide global variable and a site-wide
 > > per-request variable in my site-wide autohandler.
 > >
 > > I have code like this in the autohandler:
 > >
 > >     <%init>
 > >     # Globals initialized for each request
 > >     $session = $m->comp('/comp/util/session');
 > >     </%init>
 > >
 > >     <%once>
 > >     # Globals loaded once at startup
 > >     use vars qw ($dcm $session);
 > >     $dcm = $m->comp('/comp/util/dcm');
 > >     </%once>
 > 
 > 	You can't reference $m in a %once section, since $m is only 
 > available in the context of a request, and %once sections 
 > technically happen outside of a request context. You may want to 
 > put code like the following in your %init section:
 > 
 > $dcm ||= $m->comp('/comp/util/dcm');

Thanks.  I know I've seen that go by somewhere before, but it didn't
pop up on the pattern-recognizer when I *needed* it.

 > 	<thinking_aloud>I wonder if it would be possible to define 
 > $m to be a mock object while running %once sections. This object 
 > could have just an AUTOLOAD method that spits out a meaningful 
 > error message about not being able to use $m inside %once 
 > sections....</thinking_aloud>

Handy for this situation, certainly.  I suppose this is one of those
things that will cost a little bit on each and every single invocation
of a %once section in the universe, though?

I must say the message I *did* get is something of a marvel of
obscurity. 

 > > Another question -- I thought I could access $session in anything
 > > running after the autohandler in the request, but then I got an error
 > > on one of those.  Should I be putting a "use vars" in the components
 > > that wish to *access* these global variables too?
 > 
 > 	You shouldn't need to do that -- they all run in the same 
 > package namespace, so one "use vars" should do it. Please 
 > provide a small example that demonstrates this and we can 
 > probably help you figure out the problem.

Well, if the problem is still around when I get the *rest* of this
stuff straightened out, I'll be back with it.  I'd be inclined to bet
on its being a side-effect of thrashing around while trying to resolve
the other issues, though.  We'll see. 
-- 
David Dyer-Bennet, dd-b@dd-b.net  /  New TMDA anti-spam in test
 John Dyer-Bennet 1915-2002 Memorial Site http://john.dyer-bennet.net
        Book log: http://www.dd-b.net/dd-b/Ouroboros/booknotes/
         New Dragaera mailing lists, see http://dragaera.info


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

To: mason-users@lists.sourceforge.net
Subject: Re: [Mason] Puzzled about global variables
From: David Dyer-Bennet <dd-b@dd-b.net>
Date: Wed, 7 Aug 2002 16:32:31 -0500 (CDT)

Benjamin Turner <bjturner@whowhere.com> writes on 7 August 2002 at 14:03:57 -0700
 > 
 > On Wednesday, August 7, 2002, at 01:37  PM, David Dyer-Bennet wrote:
 > 
 > > I'm trying to set up a site-wide global variable and a site-wide
 > > per-request variable in my site-wide autohandler.
 > >
 > > I have code like this in the autohandler:
 > >
 > >     <%init>
 > >     # Globals initialized for each request
 > >     $session = $m->comp('/comp/util/session');
 > >     </%init>
 > >
 > >     <%once>
 > >     # Globals loaded once at startup
 > >     use vars qw ($dcm $session);
 > >     $dcm = $m->comp('/comp/util/dcm');
 > >     </%once>
 > 
 > 	You can't reference $m in a %once section, since $m is only 
 > available in the context of a request, and %once sections 
 > technically happen outside of a request context. You may want to 
 > put code like the following in your %init section:
 > 
 > $dcm ||= $m->comp('/comp/util/dcm');

Doing that has eliminated the error messages from the autohandler,
anyway. 

 > > Another question -- I thought I could access $session in anything
 > > running after the autohandler in the request, but then I got an error
 > > on one of those.  Should I be putting a "use vars" in the components
 > > that wish to *access* these global variables too?
 > 
 > 	You shouldn't need to do that -- they all run in the same 
 > package namespace, so one "use vars" should do it. Please 
 > provide a small example that demonstrates this and we can 
 > probably help you figure out the problem.

test.comp:

    Hello.
    <%perl>
    die "no dcm" unless $dcm;
    </%perl>

Very trimmed-down autohandler:

    <%init>
    # Globals initialized for each request
    $session = $m->comp('/comp/util/session');
    $dcm ||= $m->comp('/comp/util/dcm');
    </%init>

    <%once>
    # Globals loaded once at startup
    use vars qw ($session $dcm);
    </%once>

Error message:

    System error

    error: 
    Error during compilation of /www/dcmdev/html/test.comp:
    Global symbol "$dcm" requires explicit package name at
    /www/dcmdev/html/test.comp line 3. Stack:
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:505]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:264]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:174]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:138]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:59]
    [/usr/lib/perl5/site_perl/5.6.1/Class/Container.pm:194]
    [/usr/lib/perl5/site_perl/5.6.1/Class/Container.pm:257]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:170]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:777]
    [/usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:713]
    [/etc/httpd//conf/handler.pl:35] [/dev/null:0] 

    context: 
    1: 
    Hello.
    2: 
    <%perl>
    3: 
    die "no dcm" unless $dcm;
    4: 
    </%perl>
    5: 


    code stack: 
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:596
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:271
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:174
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Request.pm:138
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:59
    /usr/lib/perl5/site_perl/5.6.1/Class/Container.pm:194
    /usr/lib/perl5/site_perl/5.6.1/Class/Container.pm:257
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/Interp.pm:170
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:777
    /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/ApacheHandler.pm:713
    /etc/httpd//conf/handler.pl:35
    /dev/null:0

A quick test (changing the autohandler to assign a constant value to
$dcm in the %init section) shows that the problem isn't my
/comp/util/dcm component failing, either (which was the obvious next
thing to check, I thought).  (The dcm component just assigns a bunch
of constant values into a hash and returns a ref; it's my way of
centralizing config information an accessing it from within
components.)

Taking out the $dcm reference from test.comp produces the expected
result of dipslaying the "Hello." in the content position of the page
layout produced by the autohandler; no reason I can see to be
suspicious of other parts of the autohandler since they seem to work.
-- 
David Dyer-Bennet, dd-b@dd-b.net  /  New TMDA anti-spam in test
 John Dyer-Bennet 1915-2002 Memorial Site http://john.dyer-bennet.net
        Book log: http://www.dd-b.net/dd-b/Ouroboros/booknotes/
         New Dragaera mailing lists, see http://dragaera.info


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

From: "Jonathan Swartz" <swartz@pobox.com>
To: "David Dyer-Bennet" <dd-b@dd-b.net>, <mason-users@lists.sourceforge.net>
Subject: RE: [Mason] Puzzled about global variables
Date: Wed, 7 Aug 2002 14:38:33 -0700

>  > > Another question -- I thought I could access $session in anything
>  > > running after the autohandler in the request, but then I got an error
>  > > on one of those.  Should I be putting a "use vars" in the components
>  > > that wish to *access* these global variables too?
>  >
>  > 	You shouldn't need to do that -- they all run in the same
>  > package namespace, so one "use vars" should do it. Please
>  > provide a small example that demonstrates this and we can
>  > probably help you figure out the problem.

Actually, the page component will get loaded *before* its autohandler, so
putting the "use vars" in the autohandler isn't enough. You would need to
put the "use vars" in every component that accesses the variable. But it's
easier to use the allow_globals / MasonAllowGlobals parameter. See

  http://www.masonhq.com/docs/faq/#can_i_use_globals_in_components

Jon




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

To: <mason-users@lists.sourceforge.net>
Subject: RE: [Mason] Puzzled about global variables
From: David Dyer-Bennet <dd-b@dd-b.net>
Date: Wed, 7 Aug 2002 16:50:19 -0500 (CDT)

Jonathan Swartz <swartz@pobox.com> writes on 7 August 2002 at 14:38:33 -0700
 > >  > > Another question -- I thought I could access $session in anything
 > >  > > running after the autohandler in the request, but then I got an error
 > >  > > on one of those.  Should I be putting a "use vars" in the components
 > >  > > that wish to *access* these global variables too?
 > >  >
 > >  > 	You shouldn't need to do that -- they all run in the same
 > >  > package namespace, so one "use vars" should do it. Please
 > >  > provide a small example that demonstrates this and we can
 > >  > probably help you figure out the problem.
 > 
 > Actually, the page component will get loaded *before* its autohandler, so
 > putting the "use vars" in the autohandler isn't enough. You would need to
 > put the "use vars" in every component that accesses the variable. But it's
 > easier to use the allow_globals / MasonAllowGlobals parameter. See
 > 
 >   http://www.masonhq.com/docs/faq/#can_i_use_globals_in_components

So this is actually a one-time startup problem?  If I had a way to
successfully get the autohandler invoked once per Apache/mod_perl
startup, that would let things work just the way I have them?  (Not
that I plan to actually solve it that way, that's too fragile, but I'm
trying to test my expanding understanding here).  Certainly a quick
test seems to verify this.

The problem with the config file statements is that never in my
personal or professional life as a webmaster have I had a site that
always ran on a dedicated server.  At the very least, the
*development* server is always shared.  That's true even of the
secondary server in my basement (never mind the primary, that's
handling several *dozen* virtual domains).  

I know that for bigger installations people often work at the scale of
multiple servers per site, rather than vice versa, but even then I'd
think you'd need multiple copies of the site on the development
servers, so you can't really architect the site in a way that doesn't
work except on a dedicated server.  

So, if I put that global variable stuff in the right place in my
handler.pl, can I do it in such a way that the right variables appear
for the right sites?

I picked the way I'm doing it, in the autohandler, based on what the
adminstrator's guide says about initializing global variables.  If
that's not a good choice, the docs should probably be updated to not
make it look so attractive.
-- 
David Dyer-Bennet, dd-b@dd-b.net  /  New TMDA anti-spam in test
 John Dyer-Bennet 1915-2002 Memorial Site http://john.dyer-bennet.net
        Book log: http://www.dd-b.net/dd-b/Ouroboros/booknotes/
         New Dragaera mailing lists, see http://dragaera.info


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

From: Jonathan Swartz <swartz@pobox.com>
To: David Dyer-Bennet <dd-b@dd-b.net>, mason-users@lists.sourceforge.net
Subject: RE: [Mason] Puzzled about global variables
Date: Wed, 7 Aug 2002 15:09:04 -0700

> Actually, the page component will get loaded *before* its autohandler, so
> putting the "use vars" in the autohandler isn't enough. You would need to
> put the "use vars" in every component that accesses the variable. But it's
> easier to use the allow_globals / MasonAllowGlobals parameter. See
>
>   http://www.masonhq.com/docs/faq/#can_i_use_globals_in_components
>

I see now that the Admin manual incorrectly recommends that the "use vars"
be placed in the autohandler. I will fix this.

Jon


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

From: Jonathan Swartz <swartz@pobox.com>
To: David Dyer-Bennet <dd-b@dd-b.net>, mason-users@lists.sourceforge.net
Subject: RE: [Mason] Puzzled about global variables
Date: Wed, 7 Aug 2002 15:13:13 -0700

> So this is actually a one-time startup problem?  If I had a way to
> successfully get the autohandler invoked once per Apache/mod_perl
> startup, that would let things work just the way I have them?

Yes. You could preload the autohandler, for example
(http://www.masonhq.com/docs/manual/Admin.html#preloading). But as you say,
it'd be unwise to base the design on this.

> So, if I put that global variable stuff in the right place in my
> handler.pl, can I do it in such a way that the right variables appear
> for the right sites?

Global variables will be global for everything running under that Mason
environment. Putting the "use vars" in the autohandler, even if you got past
the preloading problem, wouldn't make the variables any less global.

>
> I picked the way I'm doing it, in the autohandler, based on what the
> adminstrator's guide says about initializing global variables.  If
> that's not a good choice, the docs should probably be updated to not
> make it look so attractive.

Yes, that was ill-advised -- I've fixed it for the next release.

Jon


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


===

To: mason-users@lists.sourceforge.net
Subject: Re: [Mason] Puzzled about global variables
From: David Dyer-Bennet <dd-b@dd-b.net>
Date: 07 Aug 2002 17:23:55 -0500

Jonathan Swartz <swartz@pobox.com> writes:

> Global variables will be global for everything running under that Mason
> environment. Putting the "use vars" in the autohandler, even if you got past
> the preloading problem, wouldn't make the variables any less global.

Ah.  Well, that rules that out, for everything I do.  I guess the
component root is the only thing available to me that is actually
specific to the virtual site, so I'll have to continue basing
everything off of that.  And really unfortunate.  As I say, I've never
in my life dealt with a site that could be architected to run *only*
on dedicated servers; it always had to be virtual at some points in
its life. 

> > I picked the way I'm doing it, in the autohandler, based on what the
> > adminstrator's guide says about initializing global variables.  If
> > that's not a good choice, the docs should probably be updated to not
> > make it look so attractive.
> 
> Yes, that was ill-advised -- I've fixed it for the next release.

Thanks; and I feel a bit less dumb, since I actually got my strange
ideas from a reasonable source. 
-- 
David Dyer-Bennet, dd-b@dd-b.net  /  New TMDA anti-spam in test
 John Dyer-Bennet 1915-2002 Memorial Site http://john.dyer-bennet.net
        Book log: http://www.dd-b.net/dd-b/Ouroboros/booknotes/
         New Dragaera mailing lists, see http://dragaera.info


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu