X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FObject.pm;h=e13457bef50c0a1057afe33bc42a45c61ea3ec6b;hb=963b26bdbceabdfb30dac57a85d95fec1e9d925d;hp=08a492643baf8a63e61c8dde13feb51fc9e8f1cf;hpb=3822ace201bf25804b3b760238b59df07890c634;p=gitmo%2FMooseX-Singleton.git diff --git a/lib/MooseX/Singleton/Object.pm b/lib/MooseX/Singleton/Object.pm index 08a4926..e13457b 100644 --- a/lib/MooseX/Singleton/Object.pm +++ b/lib/MooseX/Singleton/Object.pm @@ -5,9 +5,43 @@ use metaclass 'MooseX::Singleton::Meta::Class'; extends 'Moose::Object'; -no strict 'refs'; - sub instance { shift->new } +sub initialize { + my ($class, @args) = @_; + + my $existing = $class->meta->existing_singleton; + confess "Singleton is already initialized" if $existing; + + return $class->SUPER::new(@args); +} + +sub new { + my ($class, @args) = @_; + + my $existing = $class->meta->existing_singleton; + confess "Singleton is already initialized" if $existing and @args; + + # Otherwise BUILD will be called repeatedly on the existing instance. + # -- rjbs, 2008-02-03 + return $existing if $existing and ! @args; + + return $class->SUPER::new(@args); +} + 1; +__END__ + +=pod + +=head1 NAME + +MooseX::Singleton::Object - base class for MooseX::Singleton + +=head1 DESCRIPTION + +This just adds C as a shortcut for C. + +=cut +