I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2006-01-17 19:03:24-08 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 2006-01-17 20:43:18-08 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.
Direct Responses: Write a response
Posted on 2006-01-21 20:48:33-08 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 2006-01-24 21:14:36-08 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.
Direct Responses: Write a response
Posted on 2006-01-25 13:43:37-08 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 2006-01-25 16:03:56-08 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 2006-01-25 17:18:57-08 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).
Direct Responses: Write a response