2 package MooseX::Singleton::Role::Meta::Class;
4 use MooseX::Singleton::Role::Meta::Instance;
5 use MooseX::Singleton::Role::Meta::Method::Constructor;
7 sub existing_singleton {
9 my $pkg = $class->name;
13 # create exactly one instance
14 if (defined ${"$pkg\::singleton"}) {
15 return ${"$pkg\::singleton"};
23 my $pkg = $class->name;
25 undef ${"$pkg\::singleton"};
28 override _construct_instance => sub {
31 # create exactly one instance
32 my $existing = $class->existing_singleton;
33 return $existing if $existing;
35 my $pkg = $class->name;
37 return ${"$pkg\::singleton"} = super;
50 MooseX::Singleton::Role::Meta::Class - Metaclass role for MooseX::Singleton
54 This metaclass role makes sure that there is only ever one instance of an
55 object for a singleton class. The first call to C<construct_instance> is run
56 normally (and then cached). Subsequent calls will return the cached version.