Singleton objects were not singletons when made immutable. We need to
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Meta / Instance.pm
index e9c2522..c6a0a3c 100644 (file)
@@ -9,7 +9,14 @@ sub get_singleton_instance {
     my ($self, $instance) = @_;
 
     return $instance if blessed $instance;
-    return $instance->instance;
+
+    # optimization: it's really slow to go through new_object for every access
+    # so return the singleton if we see it already exists, which it will every
+    # single except the first.
+    no strict 'refs';
+    return ${"$instance\::singleton"} if defined ${"$instance\::singleton"};
+
+    return $instance->meta->construct_instance;
 }
 
 sub clone_instance {
@@ -47,5 +54,23 @@ sub inline_slot_access {
     sprintf "%s->meta->instance_metaclass->get_singleton_instance(%s)->{%s}", $instance, $instance, $slot_name;
 }
 
+no Moose;
+
 1;
 
+__END__
+
+=pod
+
+=head1 NAME
+
+MooseX::Singleton::Meta::Instance
+
+=head1 DESCRIPTION
+
+This instance metaclass manages attribute access and storage. When accessing an
+attribute, it will convert a bare package to its cached singleton instance
+(creating it if necessary).
+
+=cut
+