You are viewing a plain text version of this content. The canonical link for it is here.
Posted to asp@perl.apache.org by Philip Mak <pm...@aaanime.net> on 2001/09/08 08:11:28 UTC

Script_OnStart not getting executed?

I'm experiencing something strange with my installation of Apache::ASP
(v2.17).

In global.asa, I have this:

use vars qw($dbh);
...
sub Script_OnStart {
  $dbh = DBI->connect($cfg::dsn, $cfg::user, $cfg::pass, \%cfg::attr)
    or die "Could not connect to database: $DBI::errstr";
}

However, when I run my script, I get ``Can't call method "prepare" on an
undefined value'', which happens when I try to do $dbh->prepare without
$dbh = DBI->connect.

Now here's the weird part: If I "touch global.asa", everything starts
working fine! If I restart httpd, then it breaks again. I have a feeling
that it's not executing the Script_OnStart at all when it doesn't work,
because I look in the error_log, and it has this:

[Sat Sep  8 02:08:37 2001] [error] [asp] [19575] [debug] Script_OnStart
[Sat Sep  8 02:08:37 2001] [error] [asp] [19575] [debug] executing
__ASP__home_pmak_perl_web_edit_aspxINL

When it works, the error_log has this:

[Sat Sep  8 02:09:00 2001] [error] [asp] [19574] [debug] Script_OnStart
[Sat Sep  8 02:09:00 2001] [error] [asp] [19574] [debug] executing Script_OnStart

Notice that the first excerpt of the error_log does NOT have "executing
Script_OnStart". Isn't that subroutine supposed to be always executed?

Here is my httpd.conf:

PerlSetVar Global /tmp
PerlSetVar StatINC 1
PerlSetVar Debug -2
PerlSetVar NoState 1
PerlSetVar UseStrict 1
PerlSetVar PerlWarn ON
PerlSetVar MailHost localhost
PerlSetVar Global /home/pmak/perl/global
PerlSetVar XMLSubsMatch [sfh]:\w+
PerlSetVar GlobalPackage main
PerlRequire /home/pmak/perl/global/startup.pl
PerlFreshRestart On


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: Script_OnStart not getting executed?

Posted by Joshua Chamas <jo...@chamas.com>.
Philip Mak wrote:
> 
> On Sat, 8 Sep 2001, Joshua Chamas wrote:
> 
> > I think your analysis is right, that Script_OnStart is not
> > always being called, and the reason is that it is not always
> > being found in the global.asa ... I don't know how it would
> > work sometimes & not others.  A trace from the error_log
> > with Debug set to -1 would help from a fresh apache stop/start
> > through when the problem occurs.
> 
> I have attached the error log (might be pointless now since I fixed the
> problem---see below, but just in case you want it to investigate why
> this problem happened in the first place). Here's what I did:
> 
> 1. Start Apache
> 2. Load script in my browser (it gets an error)
> 3. Type "touch global.asa"
> 4. Load script in my browser again (it works this time)
> 

This was probably do to GlobalPackage not being set the same
for the Apache::ASP->Loader() so that when it was cached,
it didn't compile into the same package as your scripts
later on.

I need to figure out how to make Loader() better about
errors like this so that weird things don't happen like
this at runtime.  Other odd problems happen when XMLSubs
don't match.  I need to consider this some more, and 
see what can be done... the fix will probably be where
these compile cache keys are based on sum checksum of the 
config parameters on which things are based so that 
if the Loader() params are not the same the worst that 
would happen is you wouldn't benefit from the caching.

--Josh

_________________________________________________________________
Joshua Chamas                           Chamas Enterprises Inc.
NodeWorks Founder                       Huntington Beach, CA  USA 
http://www.nodeworks.com                1-714-625-4051

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: Script_OnStart not getting executed?

Posted by Joshua Chamas <jo...@chamas.com>.
Philip Mak wrote:
> 
> On Sun, 9 Sep 2001, Quentin Smith wrote:
> 
> > then put this in Script_OnStart: *main::dbh = *foo::dbh. It will make
> > main::dbh an alias for foo::dbh.
> 
> Err, I'm trying to make it so that the $dbh in my Global Package (foo) is
> usable in db.pm, d.pm and f.pm. There is no "main" package (that's what my
> Global Package *used* to be called but I renamed it to "foo" because of a
> naming conflict).
> 
> How should I do that? Should I put this in db.pm, d.pm and f.pm:
> *dbh = *foo::dbh;
> 

You should probably have some general application object, say $App
which is a regular OO style My::App type blessed object... if its a
type of single object only, then it might be derived off an object
like Class::Singleton, which you could then globally access from
any package by simply instantiating that object whereever you need it...

But let's say you have this general My::App object, and you always know
that its current for will be available as My::App->self() which could
restore the object cached during new() which was created during
some global init phase like Script_OnStart, THEN you could store
whatever utility objects in $App that might be helpful to your
application, like the $dbh as $App->dbh, Class::Struct style, 
or $App->{dbh}

The reason for having this $App object generally is it help centralize
all of the widgets that you might ever need into one place.
If you find you need another object besided $dbh accessible from
every other package, just store it in $App.  Without using something
like Class::Singleton, you can just alias it like Quentin suggests
into other packages upon each Script_OnStart like:

use vars qw($App);
sub Script_OnStart {

  $App = My::App->new;
  $main::App = $App;

  # free the mem later for this object explicitly,
  # since multiple refs to it will keep it from being 
  # destroyed even in you undef it 
  $Server->RegisterCleanup( sub { $App->DESTROY } );

}   

*main:: is a handy place to have other objects, as all the ASP
objects get initialized there for convenience, so you might
already be used to typing $main::Server, $main::Session, etc.
main:: is the default package that perl scripts use when running.

-- Josh
_________________________________________________________________
Joshua Chamas                           Chamas Enterprises Inc.
NodeWorks Founder                       Huntington Beach, CA  USA 
http://www.nodeworks.com                1-714-625-4051

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: Script_OnStart not getting executed?

Posted by Philip Mak <pm...@aaanime.net>.
On Sun, 9 Sep 2001, Quentin Smith wrote:

> then put this in Script_OnStart: *main::dbh = *foo::dbh. It will make
> main::dbh an alias for foo::dbh.

Err, I'm trying to make it so that the $dbh in my Global Package (foo) is
usable in db.pm, d.pm and f.pm. There is no "main" package (that's what my
Global Package *used* to be called but I renamed it to "foo" because of a
naming conflict).

How should I do that? Should I put this in db.pm, d.pm and f.pm:
*dbh = *foo::dbh;


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: Script_OnStart not getting executed?

Posted by Quentin Smith <qu...@comclub.dyndns.org>.
Philip Mak wrote:

>On Sat, 8 Sep 2001, Joshua Chamas wrote:
>
>>I think your analysis is right, that Script_OnStart is not
>>always being called, and the reason is that it is not always
>>being found in the global.asa ... I don't know how it would
>>work sometimes & not others.  A trace from the error_log
>>with Debug set to -1 would help from a fresh apache stop/start
>>through when the problem occurs.
>>
>
>I have attached the error log (might be pointless now since I fixed the
>problem---see below, but just in case you want it to investigate why
>this problem happened in the first place). Here's what I did:
>
>1. Start Apache
>2. Load script in my browser (it gets an error)
>3. Type "touch global.asa"
>4. Load script in my browser again (it works this time)
>
>>I get the feeling that something is manipulating the symbol
>>table in the *main:: package namespace where your GlobalPackage
>>is set.  It may be that if you change GlobalPackage to
>>something else, they you will be safe from that.  I can
>>imagine that certain kinds of packages like Apache::StatINC
>>or Apache::Reload might do these symbol table manipulations,
>>so try seeing if using those might be the problem too.
>>
>
>I tried running it without the "PerlSetVar StatINC 1" line in my
>configuration, but I still got the error.
>
>I just tried changing the GlobalPackage to "foo" and now it works. I guess
>it was something clobbering the "main" package, so I shouldn't use that
>package name anymore?
>
>Programming technique question: What's the right way of sharing global
>variables with my *.pm files? The reason I'm using GlobalPackage is so
>that I can refer to (e.g.) $dbh by typing $foo::dbh. Is that the right way
>of doing it? It does get a bit tedious having to type $foo::dbh a lot
>instead of just $dbh.
>
then put this in Script_OnStart: *main::dbh = *foo::dbh. It will make 
main::dbh an alias for foo::dbh.

>
>
>>Also, try to avoid having multiple global.asa's from multiple
>>ASP applications having the same global package.
>>
>
>I only have one global.asa running on this Apache server.
>
HTH,
--Quentin


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: Script_OnStart not getting executed?

Posted by Philip Mak <pm...@aaanime.net>.
On Sat, 8 Sep 2001, Joshua Chamas wrote:

> I think your analysis is right, that Script_OnStart is not
> always being called, and the reason is that it is not always
> being found in the global.asa ... I don't know how it would
> work sometimes & not others.  A trace from the error_log
> with Debug set to -1 would help from a fresh apache stop/start
> through when the problem occurs.

I have attached the error log (might be pointless now since I fixed the
problem---see below, but just in case you want it to investigate why
this problem happened in the first place). Here's what I did:

1. Start Apache
2. Load script in my browser (it gets an error)
3. Type "touch global.asa"
4. Load script in my browser again (it works this time)

> I get the feeling that something is manipulating the symbol
> table in the *main:: package namespace where your GlobalPackage
> is set.  It may be that if you change GlobalPackage to
> something else, they you will be safe from that.  I can
> imagine that certain kinds of packages like Apache::StatINC
> or Apache::Reload might do these symbol table manipulations,
> so try seeing if using those might be the problem too.

I tried running it without the "PerlSetVar StatINC 1" line in my
configuration, but I still got the error.

I just tried changing the GlobalPackage to "foo" and now it works. I guess
it was something clobbering the "main" package, so I shouldn't use that
package name anymore?

Programming technique question: What's the right way of sharing global
variables with my *.pm files? The reason I'm using GlobalPackage is so
that I can refer to (e.g.) $dbh by typing $foo::dbh. Is that the right way
of doing it? It does get a bit tedious having to type $foo::dbh a lot
instead of just $dbh.

> Also, try to avoid having multiple global.asa's from multiple
> ASP applications having the same global package.

I only have one global.asa running on this Apache server.

Re: Script_OnStart not getting executed?

Posted by Joshua Chamas <jo...@chamas.com>.
Philip Mak wrote:
> 
> I'm experiencing something strange with my installation of Apache::ASP
> (v2.17).
> 
> [Sat Sep  8 02:08:37 2001] [error] [asp] [19575] [debug] Script_OnStart
> [Sat Sep  8 02:08:37 2001] [error] [asp] [19575] [debug] executing
> __ASP__home_pmak_perl_web_edit_aspxINL
> 
> When it works, the error_log has this:
> 
> [Sat Sep  8 02:09:00 2001] [error] [asp] [19574] [debug] Script_OnStart
> [Sat Sep  8 02:09:00 2001] [error] [asp] [19574] [debug] executing Script_OnStart
> 
> Notice that the first excerpt of the error_log does NOT have "executing
> Script_OnStart". Isn't that subroutine supposed to be always executed?
> 
> Here is my httpd.conf:
> 
> PerlSetVar Global /tmp
...
> PerlSetVar GlobalPackage main
> PerlRequire /home/pmak/perl/global/startup.pl
> PerlFreshRestart On
> 

I think your analysis is right, that Script_OnStart is not
always being called, and the reason is that it is not always
being found in the global.asa ... I don't know how it would
work sometimes & not others.  A trace from the error_log 
with Debug set to -1 would help from a fresh apache stop/start
through when the problem occurs.

I get the feeling that something is manipulating the symbol
table in the *main:: package namespace where your GlobalPackage
is set.  It may be that if you change GlobalPackage to 
something else, they you will be safe from that.  I can 
imagine that certain kinds of packages like Apache::StatINC
or Apache::Reload might do these symbol table manipulations,
so try seeing if using those might be the problem too.

Also, try to avoid having multiple global.asa's from multiple
ASP applications having the same global package.  I could easily
see one global.asa's Script_OnStart overriding another 
in the *main package if you have GlobalPackage set to it in each 
case.  With GlobalPackage set, treat the global.asa as a 
perl package of the GlobalPackage name & namespace, so its
kind of like a main.pm, and if you have multiple main.pm's
only bad things will happen.

--Josh

_________________________________________________________________
Joshua Chamas                           Chamas Enterprises Inc.
NodeWorks Founder                       Huntington Beach, CA  USA 
http://www.nodeworks.com                1-714-625-4051

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org