line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::constr ; |
2
|
|
|
|
|
|
|
$VERSION = 2.30 ; |
3
|
10
|
|
|
10
|
|
298786
|
use 5.006_001 ; |
|
10
|
|
|
|
|
40
|
|
|
10
|
|
|
|
|
402
|
|
4
|
10
|
|
|
10
|
|
51
|
use strict ; |
|
10
|
|
|
|
|
33
|
|
|
10
|
|
|
|
|
373
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# This file uses the "Perlish" coding style |
7
|
|
|
|
|
|
|
# please read http://perl.4pro.net/perlish_coding_style.html |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
; use Carp |
10
|
10
|
|
|
10
|
|
56
|
; $Carp::Internal{+__PACKAGE__}++ |
|
10
|
|
|
|
|
29
|
|
|
10
|
|
|
|
|
2311
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
; sub import |
14
|
17
|
|
|
17
|
|
134
|
{ my ($pkg, @args) = @_ |
15
|
17
|
|
|
|
|
38
|
; my $callpkg = caller |
16
|
17
|
|
100
|
|
|
106
|
; $args[0] ||= {} |
17
|
17
|
|
|
|
|
33
|
; foreach my $constr ( @args ) |
18
|
19
|
|
100
|
|
|
94
|
{ my $n = $$constr{name} || 'new' |
19
|
19
|
50
|
100
|
|
|
114
|
; $$constr{init} &&= [ $$constr{init} ] |
20
|
|
|
|
|
|
|
unless ref $$constr{init} eq 'ARRAY' |
21
|
10
|
|
|
|
|
7431
|
; no strict 'refs' |
22
|
10
|
|
|
10
|
|
54
|
; *{$callpkg.'::'.$n} |
|
10
|
|
|
|
|
16
|
|
|
19
|
|
|
|
|
3149
|
|
23
|
|
|
|
|
|
|
= sub |
24
|
46
|
50
|
|
46
|
|
8463
|
{ &{$$constr{pre_process}} if defined $$constr{pre_process} |
|
0
|
|
|
|
|
0
|
|
25
|
46
|
|
|
|
|
89
|
; my $c = shift |
26
|
46
|
50
|
|
|
|
195
|
; my %props = ref $_[0] eq 'HASH' ? %{$_[0]} : @_ |
|
0
|
|
|
|
|
0
|
|
27
|
46
|
|
66
|
|
|
239
|
; my $class = ref($c) || $c |
28
|
46
|
|
|
|
|
109
|
; my $s = bless {}, $class |
29
|
46
|
|
|
|
|
72
|
; my $default_props = {} |
30
|
46
|
100
|
|
|
|
152
|
; if (my $cdef = $$constr{default}) |
31
|
6
|
|
66
|
|
|
53
|
{ $default_props = ref $cdef eq 'HASH' && $cdef |
32
|
|
|
|
|
|
|
|| ref $cdef eq 'CODE' && $s->$cdef(%props) |
33
|
|
|
|
|
|
|
|| not (ref $cdef) && $s->$cdef(%props) |
34
|
6
|
50
|
|
|
|
34
|
; ref $default_props eq 'HASH' or croak |
35
|
|
|
|
|
|
|
"Invalid default option for '$n' ($default_props), died" |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
# Values passed to the constructor (%props) |
38
|
|
|
|
|
|
|
# will override copied values, (%$c) |
39
|
|
|
|
|
|
|
# which will override default values (%$default_props) |
40
|
46
|
100
|
66
|
|
|
298
|
; %props = ( %$default_props |
41
|
|
|
|
|
|
|
, $$constr{copy} && ref $c ? %$c : () |
42
|
|
|
|
|
|
|
, %props |
43
|
|
|
|
|
|
|
) |
44
|
|
|
|
|
|
|
# Set the initial values for the new object |
45
|
46
|
|
|
|
|
165
|
; while ( my ($p, $v) = each %props ) |
46
|
41
|
100
|
|
|
|
264
|
{ if (my $a = $s->can($p)) # if accessor available, use it |
47
|
16
|
|
|
|
|
53
|
{ $s->$a( $v ) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else |
50
|
25
|
100
|
|
|
|
1875
|
{ croak "No such property '$p', died" |
51
|
|
|
|
|
|
|
unless $$constr{no_strict} |
52
|
13
|
50
|
|
|
|
20
|
; if ( $$constr{skip_autoload} ) |
53
|
0
|
|
|
|
|
0
|
{ $$s{$p} = $v |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else |
56
|
13
|
|
|
|
|
14
|
{ eval { $s->$p( $v ) } # try AUTOLOAD |
|
13
|
|
|
|
|
90
|
|
57
|
13
|
50
|
|
|
|
29
|
; $@ && do{ $$s{$p} = $v } # no strict so just set it |
|
13
|
|
|
|
|
57
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
# Execute any initializer methods |
62
|
34
|
100
|
|
|
|
94
|
; if ( $$constr{init} ) |
63
|
4
|
|
|
|
|
8
|
{ foreach my $m ( @{$$constr{init}} ) |
|
4
|
|
|
|
|
16
|
|
64
|
|
|
|
|
|
|
# any initializer can cancel construction by undefining the |
65
|
|
|
|
|
|
|
# object or returning a Class::Error object (false) |
66
|
|
|
|
|
|
|
# If this happens, no need to continue |
67
|
4
|
50
|
|
|
|
22
|
{ last unless $s |
68
|
4
|
|
|
|
|
23
|
; $s->$m(%props) |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
34
|
|
|
|
|
169
|
; $s |
72
|
|
|
|
|
|
|
}#END sub |
73
|
19
|
|
|
|
|
116
|
}#END for $constr |
74
|
|
|
|
|
|
|
}#END import |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
; 1 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |