On Wed, 19 Dec 2001 16:01:22 -0000
Matt Sergeant <[EMAIL PROTECTED]> wrote:

> Actually I was wondering about writing an Apache::Singleton class, that
> works the same as Class::Singleton, but clears the singleton out on each
> request (by using pnotes). Would anyone be interested in that?

Like this? (using register_cleanup instead of pnotes)


package Apache::Singleton;

use strict;
use vars qw($VERSION);
$VERSION = '0.01';

use Apache;

sub instance {
    my $class = shift;

    # get a reference to the _instance variable in the $class package
    no strict 'refs';
    my $instance = "$class\::_instance";

    unless (defined $$instance) {
        $$instance = $class->_new_instance(@_);
        Apache->request->register_cleanup(sub { undef $$instance });
    }

    return $$instance;
}

sub _new_instance {
    bless {}, shift;
}

--
Tatsuhiko Miyagawa <[EMAIL PROTECTED]>

Reply via email to