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