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;
53 MooseX::Singleton::Meta::Class
57 This metaclass is where the forcing of one instance occurs. The first call to
58 C<construct_instance> is run normally (and then cached). Subsequent calls will
59 return the cached version.