line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::LazyFactory; |
2
|
2
|
|
|
2
|
|
9857
|
use 5.006001; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
85
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
73
|
|
5
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
164
|
|
6
|
2
|
|
|
2
|
|
2390
|
use Class::Inspector; |
|
2
|
|
|
|
|
10233
|
|
|
2
|
|
|
|
|
75
|
|
7
|
2
|
|
|
2
|
|
2766
|
use UNIVERSAL; |
|
2
|
|
|
|
|
33
|
|
|
2
|
|
|
|
|
12
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $class_config = [ |
14
|
|
|
|
|
|
|
{ }, # element[0] is namespace |
15
|
|
|
|
|
|
|
{ }, # element[1] is constructor |
16
|
|
|
|
|
|
|
]; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub initialize_factory { |
19
|
1
|
|
|
1
|
1
|
20
|
my $class = shift; |
20
|
1
|
|
|
|
|
4
|
my %p = @_; |
21
|
|
|
|
|
|
|
|
22
|
1
|
50
|
|
|
|
5
|
(not ref($class)) |
23
|
|
|
|
|
|
|
or croak "new() must be called as a class method"; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# namespace |
26
|
1
|
50
|
|
|
|
4
|
if (exists $p{namespace}) { |
27
|
1
|
50
|
|
|
|
4
|
(defined $p{namespace}) |
28
|
|
|
|
|
|
|
or croak "namespace must be defined"; |
29
|
1
|
|
|
|
|
4
|
$class_config->[0]->{$class} = $p{namespace}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# constructor |
33
|
1
|
50
|
|
|
|
5
|
if (exists $p{constructor}) { |
34
|
1
|
50
|
|
|
|
4
|
(defined $p{constructor}) |
35
|
|
|
|
|
|
|
or croak "namespace must be defined"; |
36
|
1
|
|
|
|
|
4
|
$class_config->[1]->{$class} = $p{constructor}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub new { |
42
|
2
|
|
|
2
|
1
|
2164
|
my $class = shift; |
43
|
2
|
|
|
|
|
4
|
my $concrete_class = shift; |
44
|
2
|
50
|
|
|
|
11
|
(not ref($class)) |
45
|
|
|
|
|
|
|
or croak "new() must be called as a class method"; |
46
|
2
|
50
|
|
|
|
6
|
($concrete_class) |
47
|
|
|
|
|
|
|
or croak "Undefined concrete class name"; |
48
|
2
|
50
|
|
|
|
9
|
if (exists $class_config->[0]->{$class}) { |
49
|
2
|
|
|
|
|
9
|
$concrete_class = join('::', $class_config->[0]->{$class}, $concrete_class); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
# load if necessary |
52
|
2
|
|
|
|
|
15
|
my $loaded = Class::Inspector->loaded($concrete_class); |
53
|
2
|
50
|
|
|
|
82
|
(defined $loaded) |
54
|
|
|
|
|
|
|
or croak "Invalid concrete class name: $concrete_class"; |
55
|
2
|
50
|
|
|
|
6
|
if (not $loaded) { |
56
|
0
|
0
|
|
|
|
0
|
eval "require $concrete_class;" |
57
|
|
|
|
|
|
|
or croak "Unable to require concrete class: $concrete_class"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
# construct instance |
60
|
2
|
|
|
|
|
5
|
my $constructor = 'new'; |
61
|
2
|
50
|
|
|
|
8
|
if (exists $class_config->[1]->{$class}) { |
62
|
2
|
|
|
|
|
5
|
$constructor = $class_config->[1]->{$class}; |
63
|
|
|
|
|
|
|
} |
64
|
2
|
50
|
|
|
|
36
|
$concrete_class->can($constructor) |
65
|
|
|
|
|
|
|
or croak "Unable to instantiate $concrete_class with invalid constructor: $constructor"; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
10
|
my $i = $concrete_class->$constructor(@_); |
68
|
2
|
|
|
|
|
18
|
return $i; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
__END__ |