X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Singleton.git;a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FMeta%2FClass.pm;h=bc929c057edbdfa7203f9e1bf9088116f97a309c;hp=28b1437f94642d9b7497642ef2b9034b52938b19;hb=1de95613f2dfae86af1a8f548d66d1de842e7201;hpb=b375b147ae0615bfe819dad1b2233ebf3fd90fc3 diff --git a/lib/MooseX/Singleton/Meta/Class.pm b/lib/MooseX/Singleton/Meta/Class.pm index 28b1437..bc929c0 100644 --- a/lib/MooseX/Singleton/Meta/Class.pm +++ b/lib/MooseX/Singleton/Meta/Class.pm @@ -16,18 +16,30 @@ sub initialize { ); }; -override construct_instance => sub { +sub existing_singleton { my ($class) = @_; my $pkg = $class->name; no strict 'refs'; # create exactly one instance - if (!defined ${"$pkg\::singleton"}) { - ${"$pkg\::singleton"} = super; + if (defined ${"$pkg\::singleton"}) { + return ${"$pkg\::singleton"}; } - return ${"$pkg\::singleton"}; + return; +} + +override construct_instance => sub { + my ($class) = @_; + + # create exactly one instance + my $existing = $class->existing_singleton; + return $existing if $existing; + + my $pkg = $class->name; + no strict 'refs'; + return ${"$pkg\::singleton"} = super; }; 1;