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;
42 if ( $Moose::VERSION >= 1.9900 ) {
43 override _inline_params => sub {
47 'my $existing = do {',
49 'no warnings "once";',
50 '\${"$class\::singleton"};',
52 'return ${$existing} if ${$existing};',
56 override _inline_extra_init => sub {
59 return '${$existing} = $instance;';
73 MooseX::Singleton::Role::Meta::Class - Metaclass role for MooseX::Singleton
77 This metaclass role makes sure that there is only ever one instance of an
78 object for a singleton class. The first call to C<construct_instance> is run
79 normally (and then cached). Subsequent calls will return the cached version.