You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Greg Cope <gj...@rubberplant.freeserve.co.uk> on 2000/09/17 20:17:30 UTC
Clean way of transfering Objects between childinit and request handlers
?
Dear All
I want to create an Object in a ChildInit handler and then pass it to
request handlers.
Its a DB Object / module and it does some initialiastion, it prepares a
few statment handles and then stores them in an array for later
execution.
One solution is to create a the object in a child init handler and to
store it in a package global - and then to use a type glob to referance
it from other handlers by using that package global (detailed below).
However this appears as a bit of a kludge and does not have any repect
for encapsulation ;-).
I feel there must be a cleaner way of doing this ? A read of the guide
etc ... has not given me any clues.
Any ideas appreciated.
Greg Cope
### type glob example (off the top of my head - could be wrong)
package child_init;
use vars qw($foo);
$foo = My::db_handler->new();
#######
package request_handler;
*db = \$child_init::foo;
$db->foo();
Re: Clean way of transfering Objects between childinit and
requesthandlers ?
Posted by Greg Cope <gj...@rubberplant.freeserve.co.uk>.
Matt Sergeant wrote:
>
> On Sun, 17 Sep 2000, Chris Winters wrote:
>
> > Hi Greg,
> >
> > Check out Class::Singleton for this purpose. Works great for me.
> >
.....
<snippage>
> One thing C::Singleton misses is a clear_instance method though, which is
> pretty much necessary for mod_perl work (I'm surprised Andy hasn't fixed
> this, since he does a lot of mod_perl stuff)...
>
> However copying the code is pretty trivial and adding that method is about
> another 3 lines.
Thanks gents - I'd not seen this one before.
Oh, Chris I think your ratio is a bit low - the actual module code is
extremely short.
Greg
>
> --
> <Matt/>
>
> Fastnet Software Ltd. High Performance Web Specialists
> Providing mod_perl, XML, Sybase and Oracle solutions
> Email for training and consultancy availability.
> http://sergeant.org | AxKit: http://axkit.org
Re: Clean way of transfering Objects between childinit and request
handlers ?
Posted by Matt Sergeant <ma...@sergeant.org>.
On Sun, 17 Sep 2000, Chris Winters wrote:
> Hi Greg,
>
> Check out Class::Singleton for this purpose. Works great for me.
>
> Basically, the first time you create the object, do:
>
> my $obj = $object_class->instance( ... );
>
> Every successive time you want the object, just call:
>
> my $obj = $object_class->instance;
>
> And you'll get the same object back. Initialization stuff (such as
> database connects, whatever) is put in the _new_instance() method,
> which is called when the object is created but not when the object is
> returned.
>
> The object is stored in the class itself, and Class::Singleton is
> actually a really simple module (probably a 10-to-1 ratio of
> documentation-to-code), but it's nice to have a consistent, standard
> way of doing things.
One thing C::Singleton misses is a clear_instance method though, which is
pretty much necessary for mod_perl work (I'm surprised Andy hasn't fixed
this, since he does a lot of mod_perl stuff)...
However copying the code is pretty trivial and adding that method is about
another 3 lines.
--
<Matt/>
Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org | AxKit: http://axkit.org
Re: Clean way of transfering Objects between childinit and request handlers ?
Posted by Chris Winters <cw...@intes.net>.
* Greg Cope (gjjc@rubberplant.freeserve.co.uk) [000917 14:31]:
> Dear All
>
> I want to create an Object in a ChildInit handler and then pass it to
> request handlers.
>
> Its a DB Object / module and it does some initialiastion, it prepares a
> few statment handles and then stores them in an array for later
> execution.
>
> One solution is to create a the object in a child init handler and to
> store it in a package global - and then to use a type glob to referance
> it from other handlers by using that package global (detailed below).
>
> However this appears as a bit of a kludge and does not have any repect
> for encapsulation ;-).
>
> I feel there must be a cleaner way of doing this ? A read of the guide
> etc ... has not given me any clues.
>
> Any ideas appreciated.
>
> Greg Cope
Hi Greg,
Check out Class::Singleton for this purpose. Works great for me.
Basically, the first time you create the object, do:
my $obj = $object_class->instance( ... );
Every successive time you want the object, just call:
my $obj = $object_class->instance;
And you'll get the same object back. Initialization stuff (such as
database connects, whatever) is put in the _new_instance() method,
which is called when the object is created but not when the object is
returned.
The object is stored in the class itself, and Class::Singleton is
actually a really simple module (probably a 10-to-1 ratio of
documentation-to-code), but it's nice to have a consistent, standard
way of doing things.
Chris
--
Chris Winters
Senior Internet Developer intes.net
cwinters@intes.net http://www.intes.net/
Integrated hardware/software solutions to make the Internet work for you.