You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Chris Ochs <ch...@paymentonline.com> on 2003/12/16 19:11:28 UTC
use vars question
I'm still a little unclear after reading the appropriate perl docs, or maybe
just want to make sure I understand it correctly.
If I declare some package globals with use vars(), will the values be
persistant in mod_perl, or will they go undefined after the code is done
executing?
Chris
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Re: use vars question
Posted by Stas Bekman <st...@stason.org>.
Chris Ochs wrote:
> Thanks I reread the last link and noticed what I didnt' see before. Now my
> next question...
>
> I use the following to access the cgi vars..
>
> %Q = $r->method eq 'POST' ? $r->content : $r->args;
>
> If I make %Q a package global, will the above line re initialize %Q every
> time the program is run, or do I need to do some kind of foreach iteration
> through %Q and undefine everything first?
As long as you make sure that you always run the above code before you use %Q,
then yes, you are all set.
As repeated several times on this list using Apache::Singleton or
Class::Singleton or writing your own, is a more robust solution than plain
globals.
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Re: use vars question
Posted by Chris Ochs <ch...@paymentonline.com>.
Thanks I reread the last link and noticed what I didnt' see before. Now my
next question...
I use the following to access the cgi vars..
%Q = $r->method eq 'POST' ? $r->content : $r->args;
If I make %Q a package global, will the above line re initialize %Q every
time the program is run, or do I need to do some kind of foreach iteration
through %Q and undefine everything first?
----- Original Message -----
From: "Stas Bekman" <st...@stason.org>
To: "Chris Ochs" <ch...@paymentonline.com>
Cc: <mo...@perl.apache.org>
Sent: Tuesday, December 16, 2003 10:32 AM
Subject: Re: use vars question
> Chris Ochs wrote:
> > I'm still a little unclear after reading the appropriate perl docs, or
maybe
> > just want to make sure I understand it correctly.
>
> It usually helps to tell what appropriate docs have you read.
>
http://perl.apache.org/docs/1.0/guide/porting.html#Global_Variables_Persistence
>
> It's also mentioned in several places at:
> http://perl.apache.org/docs/general/perl_reference/perl_reference.html
>
> Brian has patched recently this docs explaining how to use 'local our' to
have
> globals automatically getting undefined at the end of the enclosing scope.
>
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#Remedies_for_Inner_Subroutines
>
> > If I declare some package globals with use vars(), will the values be
> > persistant in mod_perl, or will they go undefined after the code is done
> > executing?
>
> persistent, yes. Undefined, no.
>
> __________________________________________________________________
> Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/ mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org http://ticketmaster.com
>
>
> --
> Reporting bugs: http://perl.apache.org/bugs/
> Mail list info: http://perl.apache.org/maillist/modperl.html
>
>
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Re: use vars question
Posted by Stas Bekman <st...@stason.org>.
Chris Ochs wrote:
> I'm still a little unclear after reading the appropriate perl docs, or maybe
> just want to make sure I understand it correctly.
It usually helps to tell what appropriate docs have you read.
http://perl.apache.org/docs/1.0/guide/porting.html#Global_Variables_Persistence
It's also mentioned in several places at:
http://perl.apache.org/docs/general/perl_reference/perl_reference.html
Brian has patched recently this docs explaining how to use 'local our' to have
globals automatically getting undefined at the end of the enclosing scope.
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#Remedies_for_Inner_Subroutines
> If I declare some package globals with use vars(), will the values be
> persistant in mod_perl, or will they go undefined after the code is done
> executing?
persistent, yes. Undefined, no.
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Re: Clearing globals
Posted by Stas Bekman <st...@stason.org>.
Chris Ochs wrote:
> Will this work to undefine everything in package Test ?
>
> sub ClearGlobals {
> my $globalspace = "Test";
> foreach (keys %{"${globalspace}::"}) {
> unless ($_ eq 'dbh') { ## Don't undef the cached database handle!
> local *symbol = "${globalspace}::${_}";
> undef $symbol;
> undef @symbol;
> undef %symbol;
> }
> }
> }
This is quite dangerous, as you may undef things you didn't create. (e.g. some
3rd party module may use the same package for its own things). A better
approach is to explicitly undef only things that you created. But again, you
won't need that when using Singleton, which can contain all the "globals" now
as private vars and you can nuke it at once.
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Clearing globals
Posted by Chris Ochs <ch...@paymentonline.com>.
Will this work to undefine everything in package Test ?
sub ClearGlobals {
my $globalspace = "Test";
foreach (keys %{"${globalspace}::"}) {
unless ($_ eq 'dbh') { ## Don't undef the cached database handle!
local *symbol = "${globalspace}::${_}";
undef $symbol;
undef @symbol;
undef %symbol;
}
}
}
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
Re: use vars question
Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hi there,
On Tue, 16 Dec 2003, Chris Ochs wrote:
> If I declare some package globals with use vars(), will the values be
> persistant in mod_perl, or will they go undefined after the code is done
> executing?
They are global, so they persist until the death of the interpreter
which executed those statements - in mod_perl version 1, that means
when a mod_perl Apache child dies.
Globals can be a pain in mod_perl.
73,
Ged.
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html