1 package MooseX::Singleton::Role::Meta::Class;
3 use MooseX::Singleton::Role::Meta::Instance;
4 use MooseX::Singleton::Role::Meta::Method::Constructor;
7 $VERSION = eval $VERSION;
9 sub existing_singleton {
11 my $pkg = $class->name;
15 # create exactly one instance
16 if ( defined ${"$pkg\::singleton"} ) {
17 return ${"$pkg\::singleton"};
25 my $pkg = $class->name;
27 undef ${"$pkg\::singleton"};
30 override _construct_instance => sub {
33 # create exactly one instance
34 my $existing = $class->existing_singleton;
35 return $existing if $existing;
37 my $pkg = $class->name;
39 return ${"$pkg\::singleton"} = super;
52 MooseX::Singleton::Role::Meta::Class - Metaclass role for MooseX::Singleton
56 This metaclass role makes sure that there is only ever one instance of an
57 object for a singleton class. The first call to C<construct_instance> is run
58 normally (and then cached). Subsequent calls will return the cached version.