Bugzilla->instance and friends

Bradley Baetz bbaetz at acm.org
Sun Feb 2 06:21:25 UTC 2003


On Sun, Feb 02, 2003 at 01:14:12AM -0500, David Miller wrote:
> On 1/29/03 9:31 PM +1100, Bradley Baetz wrote:

> Maybe I need some education here as to what a "singleton class" is.  Does
> that just mean that the object is the class itself and it doesn't really
> instantiate anything?  Or that once instantiated, any other attempt to
> instantiate just gives you the first one originally instantiated, so
> everyone's talking to the same object no matter if they used "new" or not?

The latter. You have a way of getting to the class, in such a way that
its only created once, and then returned eah time its 'got'.

> 
> > My original idea for the fix was to do what I'd been planning for a
> > while - make the Bugzilla object creation routine take an argument
> > indicating what type of program it was, and then only initalise whats
> > required. (With defaults so that the majority of cgi files wouldn't
> > have to specify anything, and would get Bugzilla::TYPE_CGI
> > automaticaly)
> 
> This sounds like a cool thing to do.  Might help tinderbox from trying to
> open a database connection when running tests, too. :-)

Yep.

> > So, the alternative is to use Class::Singleton either directly (or just
> > use the standard ||= construct), possibly with Apache::Singleton too
> > (despite the name, this sort of works without mod_perl, although the
> > install tests fail).
> 
> So what is Class::Singleton, and what advantage do we get from that outside
> of what we'd get from hanging onto the object ID of the created instance
> somewhere and just handing that same object ID back to any subsequent
> requests for a new Bugzilla object?  This could be accomplished with a "my"
> variable within the package definition that isn't stored in the object
> hash, couldn't it?

Thats how Class::Singleton works - its just a semi-standard interface
for it. Reusable code, etc, etc

> 
> > To get a template, you then:
> >
> > Bugzilla::Template->instance
> 
> Why not Bugzilla->template ?

Because then the Bugzilla class is the central point of access. Thats
what we have now, but once the template accessor sub becomes:

my $_template;
sub template {
  $_template ||= new Bugzilla::Template();
  return $_template;
}

theres not much advantage to having it in the central package

> > Code which wants a cgi object then explictly asks for it, and its
> > created as needed. Code which doesn't never asks, and so never inits
> > stuff it doesn't want.
> 
> This would be a good thing anyway.

Right. Currently its created at startup, and I definatley want to move
away from that.

> On the other hand, turning processmail into a module is very close to
> landing, and that would solve that problem anyway, wouldn't it?  (since the
> CGI stream won't be getting re-initialized after it's already been read off
> of standard input during a POST)

Sure, but it doesn't solve problems like collectstats.pl trying to
create one. And that CGI ENV var thing is a bit icky.

Anyway, I think I'll leave it in Bugzilla.pm for now, and see what comes
up later on, when I get time to do the login stuff.

Bradley



More information about the developers mailing list