X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FMeta%2FClass.pm;h=bc929c057edbdfa7203f9e1bf9088116f97a309c;hb=1de95613f2dfae86af1a8f548d66d1de842e7201;hp=447859d5fe8796cbcbd6af37454e34dfd6dd9352;hpb=109b110b1d5969c8ae2ec7a5e5d86066f895d828;p=gitmo%2FMooseX-Singleton.git diff --git a/lib/MooseX/Singleton/Meta/Class.pm b/lib/MooseX/Singleton/Meta/Class.pm index 447859d..bc929c0 100644 --- a/lib/MooseX/Singleton/Meta/Class.pm +++ b/lib/MooseX/Singleton/Meta/Class.pm @@ -16,5 +16,47 @@ sub initialize { ); }; +sub existing_singleton { + my ($class) = @_; + my $pkg = $class->name; + + no strict 'refs'; + + # create exactly one instance + if (defined ${"$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; +__END__ + +=pod + +=head1 NAME + +MooseX::Singleton::Meta::Class + +=head1 DESCRIPTION + +This metaclass is where the forcing of one instance occurs. The first call to +C is run normally (and then cached). Subsequent calls will +return the cached version. + +=cut +