* Greg Cope ([EMAIL PROTECTED]) [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
[EMAIL PROTECTED]           http://www.intes.net/
Integrated hardware/software solutions to make the Internet work for you.

Reply via email to