line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Object::Singleton; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: Singleton base class for Dancer |
4
|
|
|
|
|
|
|
$Dancer::Object::Singleton::VERSION = '1.3514_04'; # TRIAL |
5
|
|
|
|
|
|
|
$Dancer::Object::Singleton::VERSION = '1.351404'; |
6
|
|
|
|
|
|
|
# This class is a root class for singleton objects in Dancer. |
7
|
|
|
|
|
|
|
# It provides basic OO singleton tools for Perl5 without being... MooseX::Singleton ;-) |
8
|
|
|
|
|
|
|
|
9
|
196
|
|
|
196
|
|
56428
|
use strict; |
|
196
|
|
|
|
|
356
|
|
|
196
|
|
|
|
|
4788
|
|
10
|
196
|
|
|
196
|
|
896
|
use warnings; |
|
196
|
|
|
|
|
331
|
|
|
196
|
|
|
|
|
3846
|
|
11
|
196
|
|
|
196
|
|
801
|
use Carp; |
|
196
|
|
|
|
|
327
|
|
|
196
|
|
|
|
|
10104
|
|
12
|
196
|
|
|
196
|
|
3002
|
use Dancer::Exception qw(:all); |
|
196
|
|
|
|
|
564
|
|
|
196
|
|
|
|
|
24078
|
|
13
|
|
|
|
|
|
|
|
14
|
196
|
|
|
196
|
|
1245
|
use base qw(Dancer::Object); |
|
196
|
|
|
|
|
403
|
|
|
196
|
|
|
|
|
67801
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# pool of instances (only one per package name) |
17
|
|
|
|
|
|
|
my %instances; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# constructor |
20
|
|
|
|
|
|
|
sub new { |
21
|
1
|
|
|
1
|
1
|
7
|
my ($class) = @_; |
22
|
1
|
|
|
|
|
4
|
raise core => "you can't call 'new' on $class, as it's a singleton. Try to call 'instance'"; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub clone { |
26
|
1
|
|
|
1
|
1
|
456
|
my ($class) = @_; |
27
|
1
|
|
|
|
|
8
|
raise core => "you can't call 'clone' on $class, as it's a singleton. Try to call 'instance'"; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub instance { |
31
|
4236
|
|
|
4236
|
1
|
6552417
|
my ($class) = @_; |
32
|
4236
|
|
|
|
|
6829
|
my $instance = $instances{$class}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# if exists already |
35
|
4236
|
100
|
|
|
|
16595
|
defined $instance |
36
|
|
|
|
|
|
|
and return $instance; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# create the instance |
39
|
214
|
|
|
|
|
557
|
$instance = bless {}, $class; |
40
|
214
|
|
|
|
|
972
|
$class->init($instance); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# save and return it |
43
|
214
|
|
|
|
|
476
|
$instances{$class} = $instance; |
44
|
214
|
|
|
|
|
734
|
return $instance; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# accessor code for singleton objects |
48
|
|
|
|
|
|
|
# (overloaded from Dancer::Object) |
49
|
|
|
|
|
|
|
sub _setter_code { |
50
|
771
|
|
|
771
|
|
1637
|
my ($class, $attr) = @_; |
51
|
|
|
|
|
|
|
sub { |
52
|
10951
|
|
|
10951
|
|
17781
|
my ($class_or_instance, $value) = @_; |
53
|
10951
|
100
|
|
|
|
19293
|
my $instance = ref $class_or_instance ? |
54
|
|
|
|
|
|
|
$class_or_instance : $class_or_instance->instance; |
55
|
10951
|
100
|
|
|
|
16654
|
if (@_ == 1) { |
56
|
10523
|
|
|
|
|
34746
|
return $instance->{$attr}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
428
|
|
|
|
|
2098
|
return $instance->{$attr} = $value; |
60
|
|
|
|
|
|
|
} |
61
|
771
|
|
|
|
|
3283
|
}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
__END__ |