in reply to Re: Class confusion when testing using ref()
in thread Class confusion when testing using ref()

This could be automated by letting objects 'de-register' themselves when they go out of scope e.g.

DESTROY { my $self = shift; delete $REGISTRY{$self}; }

The above will not actually work. DESTROY will never be called because $REGISTRY{$self} will have bumped the reference count. (Or rather it will get called, but only when the entire process ends - not when the object goes out of scope.)

Instead, you should use Scalar::Util to weaken $REGISTRY{$self} as part of the object construction. No DESTROY sub is necessary for this to work.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name