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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]