| 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 <abw@wardley.org> | ||||||||||||||||||||||||||||
| 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<LICENCE> file. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | #============================================================================ | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package Class::Singleton; | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our $VERSION = 1.6; | |||||||||||||||||||||||||||
| 28 | 1 | 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 | 11 | ▶ | sub instance { | ||||||||||||||||||||||||||
|
Subroutine
instance: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # already got an object | ||||||||||||||||||||||||||||
| 57 | 11 | ▶ | return $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # we store the instance against the $class key of %_INSTANCES | ||||||||||||||||||||||||||||
| 60 | 11 | my $instance = $_INSTANCES{$class}; | |||||||||||||||||||||||||||
| 61 | 11 | ▶ | unless(defined $instance) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 6 | $_INSTANCES{$class} = $instance = $class->_new_instance(@_); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 11 | return $instance; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #======================================================================= | ||||||||||||||||||||||||||||
| 69 | # has_instance() | ||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||
| 71 | # Public method to return the current instance if it exists. | ||||||||||||||||||||||||||||
| 72 | #======================================================================= | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | ▶ | sub has_instance { | ||||||||||||||||||||||||||
|
Subroutine
has_instance: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 77 | 4 | 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 | 4 | ▶ | sub _new_instance { | ||||||||||||||||||||||||||
|
Subroutine
_new_instance: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 90 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | my %args = @_ && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 92 | 4 | 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 | 1 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | # dereferences and causes orderly destruction of all instances | ||||||||||||||||||||||||||||
| 106 | 1 | undef(%_INSTANCES); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||