File Coverage

blib/lib/Class/Singleton.pm
Criterion Covered Total %
statement 21 22 95.4
branch 4 6 66.6
condition 3 6 50.0
subroutine 7 7 100.0
pod 2 2 100.0
total 37 43 86.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 Canon Research Centre Europe Ltd.
13             # Copyright (C) 1998-2008 Andy Wardley. All rights reserved.
14             # Copyright (C) 2014 Steve Hay. All rights reserved.
15             #
16             # This module is free software; you can redistribute it and/or modify it under
17             # the same terms as Perl itself, i.e. under the terms of either the GNU General
18             # Public License or the Artistic License, as specified in the F file.
19             #
20             #============================================================================
21              
22             package Class::Singleton;
23 1     1   69935 use 5.008001;
  1         11  
24 1     1   6 use strict;
  1         1  
  1         43  
25 1     1   6 use warnings;
  1         2  
  1         249  
26              
27             our $VERSION = 1.6;
28             my %_INSTANCES = ();
29              
30              
31             #========================================================================
32             #
33             # instance()
34             #
35             # Module constructor. Creates an Class::Singleton (or derived) instance
36             # if one doesn't already exist. The instance reference is stored in the
37             # %_INSTANCES hash of the Class::Singleton package. The impact of this is
38             # that you can create any number of classes derived from Class::Singleton
39             # and create a single instance of each one. If the instance reference
40             # was stored in a scalar $_INSTANCE variable, you could only instantiate
41             # *ONE* object of *ANY* class derived from Class::Singleton. The first
42             # time the instance is created, the _new_instance() constructor is called
43             # which simply returns a reference to a blessed hash. This can be
44             # overloaded for custom constructors. Any additional parameters passed to
45             # instance() are forwarded to _new_instance().
46             #
47             # Returns a reference to the existing, or a newly created Class::Singleton
48             # object. If the _new_instance() method returns an undefined value
49             # then the constructer is deemed to have failed.
50             #
51             #========================================================================
52              
53             sub instance {
54 11     11 1 7727 my $class = shift;
55            
56             # already got an object
57 11 50       31 return $class if ref $class;
58              
59             # we store the instance against the $class key of %_INSTANCES
60 11         20 my $instance = $_INSTANCES{$class};
61 11 100       24 unless(defined $instance) {
62 6         21 $_INSTANCES{$class} = $instance = $class->_new_instance(@_);
63             }
64 11         173 return $instance;
65             }
66              
67              
68             #=======================================================================
69             # has_instance()
70             #
71             # Public method to return the current instance if it exists.
72             #=======================================================================
73              
74             sub has_instance {
75 4     4 1 2319 my $class = shift;
76 4   33     20 $class = ref $class || $class;
77 4         16 return $_INSTANCES{$class};
78             }
79              
80              
81             #========================================================================
82             # _new_instance(...)
83             #
84             # Simple constructor which returns a hash reference blessed into the
85             # current class. May be overloaded to create non-hash objects or
86             # handle any specific initialisation required.
87             #========================================================================
88              
89             sub _new_instance {
90 4     4   5 my $class = shift;
91 4 50 66     19 my %args = @_ && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
  0         0  
92 4         15 bless { %args }, $class;
93             }
94              
95              
96             #========================================================================
97             # END()
98             #
99             # END block to explicitly destroy all Class::Singleton objects since
100             # destruction order at program exit is not predictable. See CPAN RT
101             # bugs #23568 and #68526 for examples of what can go wrong without this.
102             #========================================================================
103              
104             END {
105             # dereferences and causes orderly destruction of all instances
106 1     1   34 undef(%_INSTANCES);
107             }
108              
109              
110             1;
111              
112             __END__