[Catalyst] Re: using Plugin::Singleton and testing

Daniel McBrearty danielmcbrearty at gmail.com
Fri Nov 17 21:24:25 GMT 2006


On 11/17/06, A. Pagaltzis <pagaltzis at gmx.de> wrote:
> * Daniel McBrearty <danielmcbrearty at gmail.com> [2006-11-17 12:00]:
> > A global is something that is in the global namespace.
> > A singleton isn't, any more than other class is. You have to
> > import the class to which it belongs to use it.
>
> That might be true in Java. It's patently false in Perl.
>

(file MySingleton.pm)
package MySingleton;

...

sub get_instance {
# the standard stuff ...
}

1;


(EOF)

please explain how it is possible to get hold of this object without doing

use MySingleton;

my $s = MySinglton->get_instance;

(assuming that you don't deliberately export into the global
namespace, which can be the case with any class)

> > Until someone actually presents a solid reason why you should
> > *never* have a class which can only have a single instance, the
> > argument is going nowhere, as far as I can see.
>
> No, I think the onus is the other way around. I'd like to hear
> about a case that is much more sensibly be solved using
> a singleton class rather than by other means of ensuring
> uniqueness in a higher level of the infrastructure.
>
> I don't want to claim that there is no circumstance in which
> singletons are useful; I argue with people who claim that every
> use of GOTO is evil, too. But I doubt that singletons are even as
> useful as GOTO, in practice.
>

So my original example : say we have an application that streams
audio, and only one source may be streamed at a time. Having more than
one player instance in existence could violate this on many OS's. Even
if the OS would cause an error  (due to more the resource being busy
when it starts up), you just don't want it to happen. How would you
design this?

Or are we arguing about definitions? By my thinking, any class which
can have only one instance is, by definition, a singleton. Are you
arguing against this ever being done, or against certain
implementations of this?

-- 
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131



More information about the Catalyst mailing list