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

Daniel McBrearty danielmcbrearty at gmail.com
Mon Nov 20 15:17:46 GMT 2006


> >>> 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)
>
> If you do `use MySingleton` in any part of the code, it's
> possible to say `MySingleton->get_instance` in any other part.
> In Java, this is not so.

That's true. But - BIG but - it's STILL not in the global namespace.

If you do as you advoctae - a true global ... )

our $my_singleton = ....

in your main file, $my_singleton anywhere clashes with that. What's
more if you encounter it in some other file you will need to grep for
it.

MySingelton->get_instance;

(or, if you prefer $MySingleton::instance         .... )

does not suffer from that problem. While some may dissaprove of using
a class as a namespace, it is still better than a true global - in my
book, anyway. The can't accidentally clash with it, and you have some
indication as to where it originates.

(apologies for my part in dragging this thread so way OT. we could
string this out almost endlessly I suspect, but I'll leave it here ...
the discussion was interesting, to me anyhow.

and BTW I looked into COMPONENT and ACCEPT_CONTEXT and that stuff looks cool.
)


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



More information about the Catalyst mailing list