Subject: | Fix for objects stored in singleton instace not having destructors called. |
I patched Class::Singleton because when objects were being stored in an
instance of it, their destructors weren't being called when Perl
terminated. Also incorporated is a CVS tag patch so that a "cvs export -
kv" doesn't cause syntax errors.
Attached is the diff.
Subject: | diff.txt |
--- Singleton.pm.orig 2006-09-06 15:15:39.927839600 +0200
+++ Singleton.pm 2006-11-22 08:22:28.000000000 +0100
@@ -13,7 +13,7 @@
#
#----------------------------------------------------------------------------
#
-# $Id: Singleton.pm,v 1.3 1999/01/19 15:57:43 abw Exp $
+# $Id: Singleton.pm,v 1.3 2006/11/21 20:04:48 cmanley Exp $
#
#============================================================================
@@ -22,10 +22,13 @@
require 5.004;
use strict;
-use vars qw( $RCS_ID $VERSION );
+use vars qw( $RCS_ID $VERSION %_INSTANCES);
+$VERSION = sprintf '%d.%02d', q|$Revision: 1.3 $| =~ m/ (\d+) \. (\d+) /xg;
+$RCS_ID = q|$Id: Singleton.pm,v 1.3 2006/11/21 20:04:48 cmanley Exp $|;
-$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
-$RCS_ID = q$Id: Singleton.pm,v 1.3 1999/01/19 15:57:43 abw Exp $;
+END {
+ undef(%_INSTANCES); # dereferences and effectively causes the proper destruction of all instances.
+}
@@ -59,14 +62,14 @@
sub instance {
my $class = shift;
-
- # get a reference to the _instance variable in the $class package
- no strict 'refs';
- my $instance = \${ "$class\::_instance" };
-
- defined $$instance
- ? $$instance
- : ($$instance = $class->_new_instance(@_));
+ if (ref($class)) {
+ return $class;
+ }
+ my $instance = $_INSTANCES{$class};
+ unless(defined($instance)) {
+ $_INSTANCES{$class} = $instance = $class->_new_instance(@_);
+ }
+ return $instance;
}