Thread

Posted on Tue Jan 17 21:03:24 2006 by je44ery
InsideOut Singleton?
Is there any chance to get built-in support for singletons with Object::InsideOut?
Direct Responses: 1631 | 1674 | Write a response
Posted on Tue Jan 17 22:43:18 2006 by jdhedden in response to 1629
Re: InsideOut Singleton?
Good idea. I'll work on it, and get out a new version hopefully this week.
Write a response
Posted on Sat Jan 21 22:48:33 2006 by jdhedden in response to 1629
Re: InsideOut Singleton?
Well, I worked on this a bit, and have come to the conclusion that there is no way to deal with this on a general basis. For example, what happens if you instantiate a singleton class, and then try to instantiate a child class that inherits from it. There is no way for the same object to represent (i.e., be blessed into) two classes at the same time.

Generic singletons are easy. When you try to deal with inheritance, the problem becomes intractable. My conclusion is that these sort of nuances need to be handled by the developer.

In any case, the simplest singleton skeleton using Object::InsideOut would look something like this:
package My::Class; { use Object::InsideOut; my $singleton; if ($threads::shared::threads_shared) { share($singleton); } sub new { my $thing = shift; if (! $singleton) { $singleton = $thing->Object::InsideOut::new(@_); } return ($singleton); } }
Direct Responses: 1697 | 1703 | Write a response
Posted on Tue Jan 24 23:14:36 2006 by je44ery in response to 1674
Re: InsideOut Singleton?
Thank you very much for taking a look at this. If it is intractible, then so be it. I appreciate you giving me code examples for what I'll need. I'll try this out, soon enough. Right now, I'm still cheating.
Write a response
Posted on Wed Jan 25 15:43:37 2006 by jdhedden in response to 1674
Re: InsideOut Singleton?
Correction. You don't need the sharing stuff. Just use:
package My::Class; { use Object::InsideOut; my $singleton; sub new { my $thing = shift; if (! $singleton) { $singleton = $thing->Object::InsideOut::new(@_); } return ($singleton); } }
Direct Responses: 1705 | Write a response
Posted on Wed Jan 25 18:03:56 2006 by sbelton in response to 1703
Re: InsideOut Singleton?

What happens if the constructor takes a while to create the singleton (big database query for example), and a second process calls the new() method while the first one is still being run? Will I end up with 2 instances of my singleton?

I am using Class::Singleton together with Object::InsideOut at the moment:

package MyClass; { use strict; use Object::InsideOut; use base qw(Class::Singleton); sub _new_instance(){ my $class = shift @_; return $class->new(@_); } } ... # and then to get my singleton my $mc = MyClass->instance();
Direct Responses: 1706 | Write a response
Posted on Wed Jan 25 19:18:57 2006 by jdhedden in response to 1705
Re: InsideOut Singleton?
What happens if the constructor takes a while to create the singleton (big database query for example), and a second process calls the new() method while the first one is still being run?

That can only happen if you're using threads. In that case, you need to use the proper controls supplied by the threads module.

As to Class::Singleton, it offers no advantages over the sample code I suggested (and using it requires Perl to load and parse two additional modules, namely 'base' and 'Class::Singleton'). It has a minor downside in that it stores the singleton object in a global variable ($MyClass::_instance).
Write a response