Class-WeakSingleton
view release on metacpan or search on metacpan
lib/Class/WeakSingleton.pm view on Meta::CPAN
YourClass->instance(...) >> are forwarded to C<<
YourClass->_new_instance(...) >>.
Returns a normal reference to the existing, or a newly created
Class::WeakSingleton object. If the C<< ->_new_instance(...) >> method
returns an undefined value then the constructer is deemed to have
failed.
=cut
use Scalar::Util 'weaken';
sub instance {
# instance()
my $class = shift;
# get a reference to the _instance variable in the $class package
my $instance = do {
## no critic
no strict 'refs';
\${ $class . "::_instance" };
};
return $$instance if defined $$instance;
my $new_instance = $$instance = $class->_new_instance(@_);
weaken $$instance;
return $new_instance;
}
=item $singleton = YourClass->_new_instance(...)
Simple constructor which returns a hash reference blessed into the
current class. May be overloaded to create non-hash objects or handle
any specific initialisation required.
( run in 0.346 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )