2 package MooseX::Singleton::Meta::Class;
4 use MooseX::Singleton::Meta::Instance;
5 use MooseX::Singleton::Meta::Method::Constructor;
7 extends 'Moose::Meta::Class';
13 my $self = $class->SUPER::initialize(
15 instance_metaclass => 'MooseX::Singleton::Meta::Instance',
16 constructor_class => 'MooseX::Singleton::Meta::Method::Constructor',
23 sub existing_singleton {
25 my $pkg = $class->name;
29 # create exactly one instance
30 if (defined ${"$pkg\::singleton"}) {
31 return ${"$pkg\::singleton"};
39 my $pkg = $class->name;
41 undef ${"$pkg\::singleton"};
44 override _construct_instance => sub {
47 # create exactly one instance
48 my $existing = $class->existing_singleton;
49 return $existing if $existing;
51 my $pkg = $class->name;
53 return ${"$pkg\::singleton"} = super;
66 MooseX::Singleton::Meta::Class
70 This metaclass is where the forcing of one instance occurs. The first call to
71 C<construct_instance> is run normally (and then cached). Subsequent calls will
72 return the cached version.