1 package MooseX::Singleton::Role::Meta::Class;
3 use MooseX::Singleton::Role::Meta::Instance;
4 use MooseX::Singleton::Role::Meta::Method::Constructor;
6 sub existing_singleton {
8 my $pkg = $class->name;
12 # create exactly one instance
13 if (defined ${"$pkg\::singleton"}) {
14 return ${"$pkg\::singleton"};
22 my $pkg = $class->name;
24 undef ${"$pkg\::singleton"};
27 override _construct_instance => sub {
30 # create exactly one instance
31 my $existing = $class->existing_singleton;
32 return $existing if $existing;
34 my $pkg = $class->name;
36 return ${"$pkg\::singleton"} = super;
49 MooseX::Singleton::Role::Meta::Class - Metaclass role for MooseX::Singleton
53 This metaclass role makes sure that there is only ever one instance of an
54 object for a singleton class. The first call to C<construct_instance> is run
55 normally (and then cached). Subsequent calls will return the cached version.