2 package MooseX::Singleton::Meta::Class;
4 use MooseX::Singleton::Meta::Instance;
6 extends 'Moose::Meta::Class';
12 $class->SUPER::initialize(
14 instance_metaclass => 'MooseX::Singleton::Meta::Instance',
19 sub existing_singleton {
21 my $pkg = $class->name;
25 # create exactly one instance
26 if (defined ${"$pkg\::singleton"}) {
27 return ${"$pkg\::singleton"};
33 override construct_instance => sub {
36 # create exactly one instance
37 my $existing = $class->existing_singleton;
38 return $existing if $existing;
40 my $pkg = $class->name;
42 return ${"$pkg\::singleton"} = super;
45 # Need to remove make_immutable before we define it below
48 use MooseX::Singleton::Meta::Method::Constructor;
52 $self->SUPER::make_immutable
54 constructor_class => 'MooseX::Singleton::Meta::Method::Constructor',
67 MooseX::Singleton::Meta::Class
71 This metaclass is where the forcing of one instance occurs. The first call to
72 C<construct_instance> is run normally (and then cached). Subsequent calls will
73 return the cached version.