File Coverage

lib/Class/Singleton.pm
Criterion Covered Total %
statement 19 20 95.0
branch 4 6 66.6
condition 3 6 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 34 40 85.0


line stmt bran cond sub pod time code
1             #============================================================================
2             #
3             # Class::Singleton.pm
4             #
5             # Implementation of a "singleton" module which ensures that a class has
6             # only one instance and provides global access to it. For a description
7             # of the Singleton class, see "Design Patterns", Gamma et al, Addison-
8             # Wesley, 1995, ISBN 0-201-63361-2
9             #
10             # Written by Andy Wardley
11             #
12             # Copyright (C) 1998-2008 Andy Wardley. All Rights Reserved.
13             # Copyright (C) 1998 Canon Research Centre Europe Ltd.
14             #
15             #============================================================================
16              
17             package Class::Singleton;
18             require 5.004;
19 1     1   16813 use strict;
  1         3  
  1         39  
20 1     1   6 use warnings;
  1         2  
  1         281  
21              
22             our $VERSION = 1.5;
23             my %_INSTANCES = ();
24              
25              
26             #========================================================================
27             #
28             # instance()
29             #
30             # Module constructor. Creates an Class::Singleton (or derived) instance
31             # if one doesn't already exist. The instance reference is stored in the
32             # %_INSTANCES hash of the Class::Singleton package. The impact of this is
33             # that you can create any number of classes derived from Class::Singleton
34             # and create a single instance of each one. If the instance reference
35             # was stored in a scalar $_INSTANCE variable, you could only instantiate
36             # *ONE* object of *ANY* class derived from Class::Singleton. The first
37             # time the instance is created, the _new_instance() constructor is called
38             # which simply returns a reference to a blessed hash. This can be
39             # overloaded for custom constructors. Any addtional parameters passed to
40             # instance() are forwarded to _new_instance().
41             #
42             # Returns a reference to the existing, or a newly created Class::Singleton
43             # object. If the _new_instance() method returns an undefined value
44             # then the constructer is deemed to have failed.
45             #
46             #========================================================================
47              
48             sub instance {
49 11     11 1 4125 my $class = shift;
50            
51             # already got an object
52 11 50       23 return $class if ref $class;
53              
54             # we store the instance against the $class key of %_INSTANCES
55 11         12 my $instance = $_INSTANCES{$class};
56 11 100       15 unless(defined $instance) {
57 6         19 $_INSTANCES{$class} = $instance = $class->_new_instance(@_);
58             }
59 11         51 return $instance;
60             }
61              
62              
63             #=======================================================================
64             # has_instance()
65             #
66             # Public method to return the current instance if it exists.
67             #=======================================================================
68              
69             sub has_instance {
70 4     4 1 1239 my $class = shift;
71 4   33     52 $class = ref $class || $class;
72 4         15 return $_INSTANCES{$class};
73             }
74              
75              
76             #========================================================================
77             # _new_instance(...)
78             #
79             # Simple constructor which returns a hash reference blessed into the
80             # current class. May be overloaded to create non-hash objects or
81             # handle any specific initialisation required.
82             #========================================================================
83              
84             sub _new_instance {
85 4     4   4 my $class = shift;
86 4 50 66     17 my %args = @_ && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
  0         0  
87 4         15 bless { %args }, $class;
88             }
89              
90              
91             #========================================================================
92             # END()
93             #
94             # END block to explicitly destroy all Class::Singleton objects since
95             # destruction order at program exit is not predictable. See CPAN RT
96             # bugs #23568 and #68526 for examples of what can go wrong without this.
97             #========================================================================
98              
99             END {
100             # dereferences and causes orderly destruction of all instances
101 1     1   9 undef(%_INSTANCES);
102             }
103              
104              
105             1;
106              
107             __END__