| 1 | package Dancer::Object::Singleton; | ||||||||||
| 2 | 249 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||
| 3 | #ABSTRACT: Singleton base class for Dancer | ||||||||||
| 4 | 249 | $Dancer::Object::Singleton::VERSION = '1.3522'; | |||||||||
| 5 | # This class is a root class for singleton objects in Dancer. | ||||||||||
| 6 | # It provides basic OO singleton tools for Perl5 without being... MooseX::Singleton ;-) | ||||||||||
| 7 | |||||||||||
| 8 | 249 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 249 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 249 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 249 | ▶ | use Dancer::Exception qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 249 | ▶ | use base qw(Dancer::Object); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # pool of instances (only one per package name) | ||||||||||
| 16 | 249 | my %instances; | |||||||||
| 17 | |||||||||||
| 18 | # constructor | ||||||||||
| 19 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 20 | 1 | my ($class) = @_; | |||||||||
| 21 | 1 | raise core => "you can't call 'new' on $class, as it's a singleton. Try to call 'instance'"; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 1 | my ($class) = @_; | |||||||||
| 26 | 1 | raise core => "you can't call 'clone' on $class, as it's a singleton. Try to call 'instance'"; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 4420 | ▶ | sub instance { | ||||||||
|
Subroutine
instance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 30 | 4420 | my ($class) = @_; | |||||||||
| 31 | 4420 | my $instance = $instances{$class}; | |||||||||
| 32 | |||||||||||
| 33 | # if exists already | ||||||||||
| 34 | 4420 | ▶ | defined $instance | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | and return $instance; | ||||||||||
| 36 | |||||||||||
| 37 | # create the instance | ||||||||||
| 38 | 264 | $instance = bless {}, $class; | |||||||||
| 39 | 264 | $class->init($instance); | |||||||||
| 40 | |||||||||||
| 41 | # save and return it | ||||||||||
| 42 | 264 | $instances{$class} = $instance; | |||||||||
| 43 | 264 | return $instance; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # accessor code for singleton objects | ||||||||||
| 47 | # (overloaded from Dancer::Object) | ||||||||||
| 48 | 957 | ▶ | sub _setter_code { | ||||||||
|
Subroutine
_setter_code: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 957 | my ($class, $attr) = @_; | |||||||||
| 50 | 12040 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 51 | 12040 | my ($class_or_instance, $value) = @_; | |||||||||
| 52 | 12040 | ▶ | my $instance = ref $class_or_instance ? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | $class_or_instance : $class_or_instance->instance; | ||||||||||
| 54 | 12040 | ▶ | if (@_ == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 11512 | return $instance->{$attr}; | |||||||||
| 56 | } | ||||||||||
| 57 | else { | ||||||||||
| 58 | 528 | return $instance->{$attr} = $value; | |||||||||
| 59 | } | ||||||||||
| 60 | 957 | }; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 249 | 1; | |||||||||
| 64 | |||||||||||
| 65 | __END__ | ||||||||||