line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::constr ; |
2
|
|
|
|
|
|
|
$VERSION = 2.41 ; |
3
|
10
|
|
|
10
|
|
656781
|
use 5.006_001 ; |
|
10
|
|
|
|
|
99
|
|
4
|
10
|
|
|
10
|
|
54
|
use strict ; |
|
10
|
|
|
|
|
13
|
|
|
10
|
|
|
|
|
229
|
|
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
|
|
45
|
; $Carp::Internal{+__PACKAGE__}++ |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
1187
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
; sub import |
14
|
17
|
|
|
17
|
|
131
|
{ my ($pkg, @args) = @_ |
15
|
17
|
|
|
|
|
29
|
; my $callpkg = caller |
16
|
17
|
|
100
|
|
|
78
|
; $args[0] ||= {} |
17
|
17
|
|
|
|
|
54
|
; foreach my $constr ( @args ) |
18
|
19
|
|
100
|
|
|
75
|
{ my $n = $$constr{name} || 'new' |
19
|
|
|
|
|
|
|
; $$constr{init} &&= [ $$constr{init} ] |
20
|
19
|
50
|
100
|
|
|
66
|
unless ref $$constr{init} eq 'ARRAY' |
21
|
10
|
|
|
|
|
3953
|
; no strict 'refs' |
22
|
10
|
|
|
10
|
|
64
|
; *{$callpkg.'::'.$n} |
|
10
|
|
|
|
|
17
|
|
|
19
|
|
|
|
|
8318
|
|
23
|
|
|
|
|
|
|
= sub |
24
|
0
|
|
|
|
|
0
|
{ &{$$constr{pre_process}} if defined $$constr{pre_process} |
25
|
46
|
50
|
|
46
|
|
6343
|
; my $c = shift |
|
46
|
|
|
|
|
76
|
|
26
|
46
|
50
|
|
|
|
167
|
; my %props = ref $_[0] eq 'HASH' ? %{$_[0]} : @_ |
|
0
|
|
|
|
|
0
|
|
27
|
46
|
|
66
|
|
|
186
|
; my $class = ref($c) || $c |
28
|
46
|
|
|
|
|
85
|
; my $s = bless {}, $class |
29
|
46
|
|
|
|
|
71
|
; my $default_props = {} |
30
|
46
|
100
|
|
|
|
103
|
; if (my $cdef = $$constr{default}) |
31
|
6
|
|
66
|
|
|
31
|
{ $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
|
|
|
|
27
|
; 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
|
|
|
|
|
|
|
; %props = ( %$default_props |
41
|
46
|
100
|
66
|
|
|
198
|
, $$constr{copy} && ref $c ? %$c : () |
42
|
|
|
|
|
|
|
, %props |
43
|
|
|
|
|
|
|
) |
44
|
|
|
|
|
|
|
# Set the initial values for the new object |
45
|
46
|
|
|
|
|
145
|
; while ( my ($p, $v) = each %props ) |
46
|
41
|
100
|
|
|
|
223
|
{ if (my $a = $s->can($p)) # if accessor available, use it |
47
|
16
|
|
|
|
|
46
|
{ $s->$a( $v ) |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else |
50
|
|
|
|
|
|
|
{ croak "No such property '$p', died" |
51
|
|
|
|
|
|
|
unless $$constr{no_strict} |
52
|
25
|
100
|
|
|
|
1283
|
; if ( $$constr{skip_autoload} ) |
|
13
|
50
|
|
|
|
20
|
|
53
|
0
|
|
|
|
|
0
|
{ $$s{$p} = $v |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else |
56
|
13
|
|
|
|
|
25
|
{ eval { $s->$p( $v ) } # try AUTOLOAD |
|
13
|
|
|
|
|
62
|
|
57
|
13
|
50
|
|
|
|
26
|
; $@ && do{ $$s{$p} = $v } # no strict so just set it |
|
13
|
|
|
|
|
42
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
# Execute any initializer methods |
62
|
34
|
100
|
|
|
|
83
|
; if ( $$constr{init} ) |
63
|
4
|
|
|
|
|
6
|
{ foreach my $m ( @{$$constr{init}} ) |
|
4
|
|
|
|
|
10
|
|
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
|
|
|
|
14
|
{ last unless $s |
68
|
4
|
|
|
|
|
17
|
; $s->$m(%props) |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
34
|
|
|
|
|
128
|
; $s |
72
|
|
|
|
|
|
|
}#END sub |
73
|
19
|
|
|
|
|
83
|
}#END for $constr |
74
|
|
|
|
|
|
|
}#END import |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
; 1 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |