line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Accessor; |
2
|
|
|
|
|
|
|
require 5.00502; |
3
|
6
|
|
|
6
|
|
28266
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
837
|
|
4
|
|
|
|
|
|
|
$Class::Accessor::VERSION = '0.34'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
7
|
11
|
|
|
11
|
1
|
8694
|
my($proto, $fields) = @_; |
8
|
11
|
|
33
|
|
|
51
|
my($class) = ref $proto || $proto; |
9
|
|
|
|
|
|
|
|
10
|
11
|
100
|
|
|
|
28
|
$fields = {} unless defined $fields; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# make a copy of $fields. |
13
|
11
|
|
|
|
|
69
|
bless {%$fields}, $class; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub mk_accessors { |
17
|
13
|
|
|
13
|
1
|
2529
|
my($self, @fields) = @_; |
18
|
|
|
|
|
|
|
|
19
|
13
|
|
|
|
|
77
|
$self->_mk_accessors('rw', @fields); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
if (eval { require Sub::Name }) { |
23
|
|
|
|
|
|
|
Sub::Name->import; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
{ |
27
|
6
|
|
|
6
|
|
28
|
no strict 'refs'; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
7399
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub import { |
30
|
2
|
|
|
2
|
|
12
|
my ($class, @what) = @_; |
31
|
2
|
|
|
|
|
4
|
my $caller = caller; |
32
|
2
|
|
|
|
|
45
|
for (@what) { |
33
|
1
|
50
|
|
|
|
14
|
if (/^(?:antlers|moose-?like)$/i) { |
34
|
1
|
|
|
|
|
4
|
*{"${caller}::has"} = sub { |
35
|
5
|
|
|
5
|
|
939
|
my ($f, %args) = @_; |
36
|
5
|
|
100
|
|
|
29
|
$caller->_mk_accessors(($args{is}||"rw"), $f); |
37
|
1
|
|
|
|
|
5
|
}; |
38
|
1
|
|
|
|
|
4
|
*{"${caller}::extends"} = sub { |
39
|
1
|
|
|
1
|
|
2
|
@{"${caller}::ISA"} = @_; |
|
1
|
|
|
|
|
17
|
|
40
|
1
|
50
|
|
|
|
5
|
unless (grep $_->can("_mk_accessors"), @_) { |
41
|
1
|
|
|
|
|
2
|
push @{"${caller}::ISA"}, $class; |
|
1
|
|
|
|
|
1907
|
|
42
|
|
|
|
|
|
|
} |
43
|
1
|
|
|
|
|
4
|
}; |
44
|
|
|
|
|
|
|
# we'll use their @ISA as a default, in case it happens to be |
45
|
|
|
|
|
|
|
# set already |
46
|
1
|
|
|
|
|
1
|
&{"${caller}::extends"}(@{"${caller}::ISA"}); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub follow_best_practice { |
52
|
3
|
|
|
3
|
1
|
877
|
my($self) = @_; |
53
|
3
|
|
33
|
|
|
14
|
my $class = ref $self || $self; |
54
|
3
|
|
|
|
|
4
|
*{"${class}::accessor_name_for"} = \&best_practice_accessor_name_for; |
|
3
|
|
|
|
|
14
|
|
55
|
3
|
|
|
|
|
4
|
*{"${class}::mutator_name_for"} = \&best_practice_mutator_name_for; |
|
3
|
|
|
|
|
12
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _mk_accessors { |
59
|
38
|
|
|
38
|
|
69
|
my($self, $access, @fields) = @_; |
60
|
38
|
|
33
|
|
|
148
|
my $class = ref $self || $self; |
61
|
38
|
|
100
|
|
|
124
|
my $ra = $access eq 'rw' || $access eq 'ro'; |
62
|
38
|
|
100
|
|
|
108
|
my $wa = $access eq 'rw' || $access eq 'wo'; |
63
|
|
|
|
|
|
|
|
64
|
38
|
|
|
|
|
56
|
foreach my $field (@fields) { |
65
|
56
|
|
|
|
|
143
|
my $accessor_name = $self->accessor_name_for($field); |
66
|
56
|
|
|
|
|
158
|
my $mutator_name = $self->mutator_name_for($field); |
67
|
56
|
100
|
66
|
|
|
249
|
if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTROY' ) { |
68
|
4
|
|
|
|
|
37
|
$self->_carp("Having a data accessor named DESTROY in '$class' is unwise."); |
69
|
|
|
|
|
|
|
} |
70
|
56
|
100
|
|
|
|
98
|
if ($accessor_name eq $mutator_name) { |
71
|
38
|
|
|
|
|
39
|
my $accessor; |
72
|
38
|
100
|
100
|
|
|
136
|
if ($ra && $wa) { |
|
|
100
|
|
|
|
|
|
73
|
22
|
|
|
|
|
73
|
$accessor = $self->make_accessor($field); |
74
|
|
|
|
|
|
|
} elsif ($ra) { |
75
|
8
|
|
|
|
|
37
|
$accessor = $self->make_ro_accessor($field); |
76
|
|
|
|
|
|
|
} else { |
77
|
8
|
|
|
|
|
36
|
$accessor = $self->make_wo_accessor($field); |
78
|
|
|
|
|
|
|
} |
79
|
38
|
|
|
|
|
74
|
my $fullname = "${class}::$accessor_name"; |
80
|
38
|
|
|
|
|
45
|
my $subnamed = 0; |
81
|
38
|
100
|
|
|
|
38
|
unless (defined &{$fullname}) { |
|
38
|
|
|
|
|
188
|
|
82
|
32
|
50
|
|
|
|
70
|
subname($fullname, $accessor) if defined &subname; |
83
|
32
|
|
|
|
|
41
|
$subnamed = 1; |
84
|
32
|
|
|
|
|
33
|
*{$fullname} = $accessor; |
|
32
|
|
|
|
|
163
|
|
85
|
|
|
|
|
|
|
} |
86
|
38
|
50
|
|
|
|
85
|
if ($accessor_name eq $field) { |
87
|
|
|
|
|
|
|
# the old behaviour |
88
|
38
|
|
|
|
|
68
|
my $alias = "${class}::_${field}_accessor"; |
89
|
38
|
50
|
33
|
|
|
83
|
subname($alias, $accessor) if defined &subname and not $subnamed; |
90
|
38
|
50
|
|
|
|
559
|
*{$alias} = $accessor unless defined &{$alias}; |
|
38
|
|
|
|
|
241
|
|
|
38
|
|
|
|
|
266
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} else { |
93
|
18
|
|
|
|
|
30
|
my $fullaccname = "${class}::$accessor_name"; |
94
|
18
|
|
|
|
|
27
|
my $fullmutname = "${class}::$mutator_name"; |
95
|
18
|
100
|
66
|
|
|
37
|
if ($ra and not defined &{$fullaccname}) { |
|
12
|
|
|
|
|
78
|
|
96
|
12
|
|
|
|
|
44
|
my $accessor = $self->make_ro_accessor($field); |
97
|
12
|
50
|
|
|
|
26
|
subname($fullaccname, $accessor) if defined &subname; |
98
|
12
|
|
|
|
|
13
|
*{$fullaccname} = $accessor; |
|
12
|
|
|
|
|
53
|
|
99
|
|
|
|
|
|
|
} |
100
|
18
|
100
|
66
|
|
|
53
|
if ($wa and not defined &{$fullmutname}) { |
|
12
|
|
|
|
|
78
|
|
101
|
12
|
|
|
|
|
43
|
my $mutator = $self->make_wo_accessor($field); |
102
|
12
|
50
|
|
|
|
26
|
subname($fullmutname, $mutator) if defined &subname; |
103
|
12
|
|
|
|
|
14
|
*{$fullmutname} = $mutator; |
|
12
|
|
|
|
|
79
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub mk_ro_accessors { |
112
|
10
|
|
|
10
|
1
|
89
|
my($self, @fields) = @_; |
113
|
|
|
|
|
|
|
|
114
|
10
|
|
|
|
|
27
|
$self->_mk_accessors('ro', @fields); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub mk_wo_accessors { |
118
|
10
|
|
|
10
|
1
|
72
|
my($self, @fields) = @_; |
119
|
|
|
|
|
|
|
|
120
|
10
|
|
|
|
|
36
|
$self->_mk_accessors('wo', @fields); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub best_practice_accessor_name_for { |
124
|
9
|
|
|
9
|
0
|
10
|
my ($class, $field) = @_; |
125
|
9
|
|
|
|
|
16
|
return "get_$field"; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub best_practice_mutator_name_for { |
129
|
9
|
|
|
9
|
0
|
10
|
my ($class, $field) = @_; |
130
|
9
|
|
|
|
|
12
|
return "set_$field"; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub accessor_name_for { |
134
|
38
|
|
|
38
|
1
|
53
|
my ($class, $field) = @_; |
135
|
38
|
|
|
|
|
61
|
return $field; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub mutator_name_for { |
139
|
38
|
|
|
38
|
1
|
48
|
my ($class, $field) = @_; |
140
|
38
|
|
|
|
|
48
|
return $field; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub set { |
144
|
11
|
|
|
11
|
1
|
613
|
my($self, $key) = splice(@_, 0, 2); |
145
|
|
|
|
|
|
|
|
146
|
11
|
100
|
|
|
|
37
|
if(@_ == 1) { |
|
|
50
|
|
|
|
|
|
147
|
9
|
|
|
|
|
45
|
$self->{$key} = $_[0]; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
elsif(@_ > 1) { |
150
|
2
|
|
|
|
|
9
|
$self->{$key} = [@_]; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else { |
153
|
0
|
|
|
|
|
0
|
$self->_croak("Wrong number of arguments received"); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub get { |
158
|
19
|
|
|
19
|
1
|
43
|
my $self = shift; |
159
|
|
|
|
|
|
|
|
160
|
19
|
50
|
|
|
|
39
|
if(@_ == 1) { |
|
|
0
|
|
|
|
|
|
161
|
19
|
|
|
|
|
201
|
return $self->{$_[0]}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
elsif( @_ > 1 ) { |
164
|
0
|
|
|
|
|
0
|
return @{$self}{@_}; |
|
0
|
|
|
|
|
0
|
|
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
else { |
167
|
0
|
|
|
|
|
0
|
$self->_croak("Wrong number of arguments received"); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub make_accessor { |
172
|
10
|
|
|
10
|
1
|
17
|
my ($class, $field) = @_; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
return sub { |
175
|
16
|
|
|
16
|
|
1373
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
16
|
100
|
|
|
|
30
|
if(@_) { |
178
|
5
|
|
|
|
|
18
|
return $self->set($field, @_); |
179
|
|
|
|
|
|
|
} else { |
180
|
11
|
|
|
|
|
29
|
return $self->get($field); |
181
|
|
|
|
|
|
|
} |
182
|
10
|
|
|
|
|
41
|
}; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub make_ro_accessor { |
186
|
7
|
|
|
7
|
1
|
10
|
my($class, $field) = @_; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
return sub { |
189
|
8
|
|
|
8
|
|
21
|
my $self = shift; |
190
|
|
|
|
|
|
|
|
191
|
8
|
100
|
|
|
|
25
|
if (@_) { |
192
|
2
|
|
|
|
|
5
|
my $caller = caller; |
193
|
2
|
|
|
|
|
685
|
$self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'"); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
else { |
196
|
6
|
|
|
|
|
18
|
return $self->get($field); |
197
|
|
|
|
|
|
|
} |
198
|
7
|
|
|
|
|
52
|
}; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub make_wo_accessor { |
202
|
7
|
|
|
7
|
1
|
11
|
my($class, $field) = @_; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
return sub { |
205
|
7
|
|
|
7
|
|
1547
|
my $self = shift; |
206
|
|
|
|
|
|
|
|
207
|
7
|
100
|
|
|
|
22
|
unless (@_) { |
208
|
2
|
|
|
|
|
4
|
my $caller = caller; |
209
|
2
|
|
|
|
|
11
|
$self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'"); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
else { |
212
|
5
|
|
|
|
|
24
|
return $self->set($field, @_); |
213
|
|
|
|
|
|
|
} |
214
|
7
|
|
|
|
|
28
|
}; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
6
|
|
|
6
|
|
47
|
use Carp (); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
561
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub _carp { |
221
|
4
|
|
|
4
|
|
8
|
my ($self, $msg) = @_; |
222
|
4
|
|
33
|
|
|
738
|
Carp::carp($msg || $self); |
223
|
4
|
|
|
|
|
22
|
return; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub _croak { |
227
|
8
|
|
|
8
|
|
13
|
my ($self, $msg) = @_; |
228
|
8
|
|
33
|
|
|
1168
|
Carp::croak($msg || $self); |
229
|
0
|
|
|
|
|
|
return; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
__END__ |