From: Ricardo SIGNES Date: Sun, 3 Feb 2008 16:06:51 +0000 (+0000) Subject: return existing instances more efficiently X-Git-Tag: 0.09_02~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=963b26bdbceabdfb30dac57a85d95fec1e9d925d;p=gitmo%2FMooseX-Singleton.git return existing instances more efficiently --- diff --git a/ChangeLog b/ChangeLog index e95e7dc..4a4efdd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,11 @@ Revision history for Perl extension MooseX-Singleton +0.05 2008-02-03 + - avoid re-BUILD-ing existing singleton objects + 0.04 2008-01-27 - exception when ->new called with args and instance already init'd (rjbs) - - added ->initialize method to remove any ambiguity with ->new + - added ->initialize method to remove any ambiguity with ->new (rjbs) 0.03 2007-12-16 - reimplementation as a metaclass (Sartak) diff --git a/lib/MooseX/Singleton/Object.pm b/lib/MooseX/Singleton/Object.pm index 8ad2a4d..e13457b 100644 --- a/lib/MooseX/Singleton/Object.pm +++ b/lib/MooseX/Singleton/Object.pm @@ -22,6 +22,10 @@ sub new { 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); }