| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Devel::OverloadInfo; |
|
2
|
|
|
|
|
|
|
$Devel::OverloadInfo::VERSION = '0.008'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: introspect overloaded operators |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
|
6
|
|
|
|
|
|
|
#pod |
|
7
|
|
|
|
|
|
|
#pod Devel::OverloadInfo returns information about L<overloaded|overload> |
|
8
|
|
|
|
|
|
|
#pod operators for a given class (or object), including where in the |
|
9
|
|
|
|
|
|
|
#pod inheritance hierarchy the overloads are declared and where the code |
|
10
|
|
|
|
|
|
|
#pod implementing them is. |
|
11
|
|
|
|
|
|
|
#pod |
|
12
|
|
|
|
|
|
|
#pod =cut |
|
13
|
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
213045
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
86
|
|
|
15
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
198
|
|
|
16
|
2
|
|
|
2
|
|
1484
|
use overload (); |
|
|
2
|
|
|
|
|
4331
|
|
|
|
2
|
|
|
|
|
105
|
|
|
17
|
2
|
|
|
2
|
|
19
|
use Scalar::Util qw(blessed); |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
193
|
|
|
18
|
2
|
|
|
2
|
|
1318
|
use Package::Stash 0.14; |
|
|
2
|
|
|
|
|
18760
|
|
|
|
2
|
|
|
|
|
121
|
|
|
19
|
2
|
|
|
2
|
|
1103
|
use MRO::Compat; |
|
|
2
|
|
|
|
|
4538
|
|
|
|
2
|
|
|
|
|
524
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
BEGIN { |
|
22
|
2
|
50
|
33
|
2
|
|
8
|
if (eval { require Sub::Util } && defined &Sub::Util::subname) { |
|
|
2
|
|
|
|
|
33
|
|
|
23
|
2
|
|
|
|
|
91
|
*subname = \&Sub::Util::subname; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
else { |
|
26
|
0
|
|
|
|
|
0
|
require B; |
|
27
|
|
|
|
|
|
|
*subname = sub { |
|
28
|
0
|
|
|
|
|
0
|
my ($coderef) = @_; |
|
29
|
0
|
0
|
|
|
|
0
|
die 'Not a subroutine reference' |
|
30
|
|
|
|
|
|
|
unless ref $coderef; |
|
31
|
0
|
|
|
|
|
0
|
my $cv = B::svref_2object($coderef); |
|
32
|
0
|
0
|
|
|
|
0
|
die 'Not a subroutine reference' |
|
33
|
|
|
|
|
|
|
unless $cv->isa('B::CV'); |
|
34
|
0
|
|
|
|
|
0
|
my $gv = $cv->GV; |
|
35
|
|
|
|
|
|
|
return undef |
|
36
|
0
|
0
|
|
|
|
0
|
if $gv->isa('B::SPECIAL'); |
|
37
|
0
|
|
|
|
|
0
|
my $stash = $gv->STASH; |
|
38
|
0
|
0
|
|
|
|
0
|
my $package = $stash->isa('B::SPECIAL') ? '__ANON__' : $stash->NAME; |
|
39
|
0
|
|
|
|
|
0
|
return $package . '::' . $gv->NAME; |
|
40
|
0
|
|
|
|
|
0
|
}; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
2
|
|
|
2
|
|
11
|
use Exporter 5.57 qw(import); |
|
|
2
|
|
|
|
|
55
|
|
|
|
2
|
|
|
|
|
1797
|
|
|
46
|
|
|
|
|
|
|
our @EXPORT_OK = qw(overload_info overload_op_info is_overloaded); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub stash_with_symbol { |
|
49
|
467
|
|
|
467
|
0
|
1027
|
my ($class, $symbol) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
467
|
|
|
|
|
813
|
for my $package (@{mro::get_linear_isa($class)}) { |
|
|
467
|
|
|
|
|
1292
|
|
|
52
|
697
|
|
|
|
|
4019
|
my $stash = Package::Stash->new($package); |
|
53
|
697
|
|
|
|
|
3831
|
my $value_ref = $stash->get_symbol($symbol); |
|
54
|
697
|
100
|
|
|
|
2680
|
return ($stash, $value_ref) if $value_ref; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
439
|
|
|
|
|
2180
|
return; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#pod =func is_overloaded |
|
60
|
|
|
|
|
|
|
#pod |
|
61
|
|
|
|
|
|
|
#pod if (is_overloaded($class_or_object)) { ... } |
|
62
|
|
|
|
|
|
|
#pod |
|
63
|
|
|
|
|
|
|
#pod Returns a boolean indicating whether the given class or object has any |
|
64
|
|
|
|
|
|
|
#pod overloading declared. Note that a bare C<use overload;> with no |
|
65
|
|
|
|
|
|
|
#pod actual operators counts as being overloaded. |
|
66
|
|
|
|
|
|
|
#pod |
|
67
|
|
|
|
|
|
|
#pod Equivalent to |
|
68
|
|
|
|
|
|
|
#pod L<overload::Overloaded()|overload/overload::Overloaded(arg)>, but |
|
69
|
|
|
|
|
|
|
#pod doesn't trigger various bugs associated with it in versions of perl |
|
70
|
|
|
|
|
|
|
#pod before 5.16. |
|
71
|
|
|
|
|
|
|
#pod |
|
72
|
|
|
|
|
|
|
#pod =cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub is_overloaded { |
|
75
|
470
|
|
33
|
470
|
1
|
217985
|
my $class = blessed($_[0]) || $_[0]; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Perl before 5.16 seems to corrupt inherited overload info if |
|
78
|
|
|
|
|
|
|
# there's a lone dereference overload and overload::Overloaded() |
|
79
|
|
|
|
|
|
|
# is called before any object has been blessed into the class. |
|
80
|
470
|
50
|
|
|
|
2499
|
return !!("$]" >= 5.016 |
|
81
|
|
|
|
|
|
|
? overload::Overloaded($class) |
|
82
|
|
|
|
|
|
|
: stash_with_symbol($class, '&()') |
|
83
|
|
|
|
|
|
|
); |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
#pod =func overload_op_info |
|
87
|
|
|
|
|
|
|
#pod |
|
88
|
|
|
|
|
|
|
#pod my $info = overload_op_info($class_or_object, $op); |
|
89
|
|
|
|
|
|
|
#pod |
|
90
|
|
|
|
|
|
|
#pod Returns a hash reference with information about the specified |
|
91
|
|
|
|
|
|
|
#pod overloaded operator of the named class or blessed object. |
|
92
|
|
|
|
|
|
|
#pod |
|
93
|
|
|
|
|
|
|
#pod Returns C<undef> if the operator is not overloaded. |
|
94
|
|
|
|
|
|
|
#pod |
|
95
|
|
|
|
|
|
|
#pod See L<overload/Overloadable Operations> for the available operators. |
|
96
|
|
|
|
|
|
|
#pod |
|
97
|
|
|
|
|
|
|
#pod The keys in the returned hash are as follows: |
|
98
|
|
|
|
|
|
|
#pod |
|
99
|
|
|
|
|
|
|
#pod =over |
|
100
|
|
|
|
|
|
|
#pod |
|
101
|
|
|
|
|
|
|
#pod =item class |
|
102
|
|
|
|
|
|
|
#pod |
|
103
|
|
|
|
|
|
|
#pod The name of the class in which the operator overloading was declared. |
|
104
|
|
|
|
|
|
|
#pod |
|
105
|
|
|
|
|
|
|
#pod =item code |
|
106
|
|
|
|
|
|
|
#pod |
|
107
|
|
|
|
|
|
|
#pod A reference to the function implementing the overloaded operator. |
|
108
|
|
|
|
|
|
|
#pod |
|
109
|
|
|
|
|
|
|
#pod =item code_name |
|
110
|
|
|
|
|
|
|
#pod |
|
111
|
|
|
|
|
|
|
#pod The fully qualified name of the function implementing the overloaded operator. |
|
112
|
|
|
|
|
|
|
#pod |
|
113
|
|
|
|
|
|
|
#pod =item method_name (optional) |
|
114
|
|
|
|
|
|
|
#pod |
|
115
|
|
|
|
|
|
|
#pod The name of the method implementing the overloaded operator, if the |
|
116
|
|
|
|
|
|
|
#pod overloading was specified as a named method, e.g. C<< use overload $op |
|
117
|
|
|
|
|
|
|
#pod => 'method'; >>. |
|
118
|
|
|
|
|
|
|
#pod |
|
119
|
|
|
|
|
|
|
#pod =item code_class (optional) |
|
120
|
|
|
|
|
|
|
#pod |
|
121
|
|
|
|
|
|
|
#pod The name of the class in which the method specified by C<method_name> |
|
122
|
|
|
|
|
|
|
#pod was found. |
|
123
|
|
|
|
|
|
|
#pod |
|
124
|
|
|
|
|
|
|
#pod =item value (optional) |
|
125
|
|
|
|
|
|
|
#pod |
|
126
|
|
|
|
|
|
|
#pod For the special C<fallback> key, the value it was given in C<class>. |
|
127
|
|
|
|
|
|
|
#pod |
|
128
|
|
|
|
|
|
|
#pod =back |
|
129
|
|
|
|
|
|
|
#pod |
|
130
|
|
|
|
|
|
|
#pod =cut |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub overload_op_info { |
|
133
|
458
|
|
|
458
|
1
|
7791
|
my ($class, $op) = @_; |
|
134
|
458
|
|
33
|
|
|
1608
|
$class = blessed($class) || $class; |
|
135
|
|
|
|
|
|
|
|
|
136
|
458
|
100
|
|
|
|
911
|
return undef unless is_overloaded($class); |
|
137
|
457
|
100
|
|
|
|
19387
|
my $op_method = $op eq 'fallback' ? "()" : "($op"; |
|
138
|
457
|
100
|
|
|
|
1221
|
my ($stash, $func) = stash_with_symbol($class, "&$op_method") |
|
139
|
|
|
|
|
|
|
or return undef; |
|
140
|
21
|
|
|
|
|
87
|
my $info = { |
|
141
|
|
|
|
|
|
|
class => $stash->name, |
|
142
|
|
|
|
|
|
|
}; |
|
143
|
21
|
100
|
|
|
|
97
|
if ($func == \&overload::nil) { |
|
144
|
|
|
|
|
|
|
# Named method or fallback, stored in the scalar slot |
|
145
|
13
|
50
|
|
|
|
124
|
if (my $value_ref = $stash->get_symbol("\$$op_method")) { |
|
146
|
13
|
|
|
|
|
25
|
my $value = $$value_ref; |
|
147
|
13
|
100
|
|
|
|
34
|
if ($op eq 'fallback') { |
|
148
|
3
|
|
|
|
|
11
|
$info->{value} = $value; |
|
149
|
|
|
|
|
|
|
} else { |
|
150
|
10
|
|
|
|
|
23
|
$info->{method_name} = $value; |
|
151
|
10
|
100
|
|
|
|
27
|
if (my ($impl_stash, $impl_func) = stash_with_symbol($class, "&$value")) { |
|
152
|
7
|
|
|
|
|
50
|
$info->{code_class} = $impl_stash->name; |
|
153
|
7
|
|
|
|
|
19
|
$info->{code} = $impl_func; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
} else { |
|
158
|
8
|
|
|
|
|
19
|
$info->{code} = $func; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
$info->{code_name} = subname($info->{code}) |
|
161
|
21
|
100
|
|
|
|
150
|
if exists $info->{code}; |
|
162
|
|
|
|
|
|
|
|
|
163
|
21
|
|
|
|
|
130
|
return $info; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
#pod =func overload_info |
|
167
|
|
|
|
|
|
|
#pod |
|
168
|
|
|
|
|
|
|
#pod my $info = overload_info($class_or_object); |
|
169
|
|
|
|
|
|
|
#pod |
|
170
|
|
|
|
|
|
|
#pod Returns a hash reference with information about all the overloaded |
|
171
|
|
|
|
|
|
|
#pod operators of specified class name or blessed object. The keys are the |
|
172
|
|
|
|
|
|
|
#pod overloaded operators, as specified in C<%overload::ops> (see |
|
173
|
|
|
|
|
|
|
#pod L<overload/Overloadable Operations>), and the values are the hashes |
|
174
|
|
|
|
|
|
|
#pod returned by L</overload_op_info>. |
|
175
|
|
|
|
|
|
|
#pod |
|
176
|
|
|
|
|
|
|
#pod =cut |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub overload_info { |
|
179
|
7
|
|
33
|
7
|
1
|
236134
|
my $class = blessed($_[0]) || $_[0]; |
|
180
|
|
|
|
|
|
|
|
|
181
|
7
|
100
|
|
|
|
21
|
return {} unless is_overloaded($class); |
|
182
|
|
|
|
|
|
|
|
|
183
|
6
|
|
|
|
|
357
|
my (%overloaded); |
|
184
|
6
|
|
|
|
|
178
|
for my $op (map split(/\s+/), values %overload::ops) { |
|
185
|
450
|
100
|
|
|
|
1370
|
my $info = overload_op_info($class, $op) |
|
186
|
|
|
|
|
|
|
or next; |
|
187
|
16
|
|
|
|
|
50
|
$overloaded{$op} = $info |
|
188
|
|
|
|
|
|
|
} |
|
189
|
6
|
|
|
|
|
73
|
return \%overloaded; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
#pod =head1 CAVEATS |
|
193
|
|
|
|
|
|
|
#pod |
|
194
|
|
|
|
|
|
|
#pod Whether the C<fallback> key exists when it has its default value of |
|
195
|
|
|
|
|
|
|
#pod C<undef> varies between perl versions: Before 5.18 it's there, in |
|
196
|
|
|
|
|
|
|
#pod later versions it's not. |
|
197
|
|
|
|
|
|
|
#pod |
|
198
|
|
|
|
|
|
|
#pod =cut |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__END__ |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=pod |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=encoding UTF-8 |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 NAME |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Devel::OverloadInfo - introspect overloaded operators |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 VERSION |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
version 0.008 |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Devel::OverloadInfo returns information about L<overloaded|overload> |
|
219
|
|
|
|
|
|
|
operators for a given class (or object), including where in the |
|
220
|
|
|
|
|
|
|
inheritance hierarchy the overloads are declared and where the code |
|
221
|
|
|
|
|
|
|
implementing them is. |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 is_overloaded |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
if (is_overloaded($class_or_object)) { ... } |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Returns a boolean indicating whether the given class or object has any |
|
230
|
|
|
|
|
|
|
overloading declared. Note that a bare C<use overload;> with no |
|
231
|
|
|
|
|
|
|
actual operators counts as being overloaded. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Equivalent to |
|
234
|
|
|
|
|
|
|
L<overload::Overloaded()|overload/overload::Overloaded(arg)>, but |
|
235
|
|
|
|
|
|
|
doesn't trigger various bugs associated with it in versions of perl |
|
236
|
|
|
|
|
|
|
before 5.16. |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 overload_op_info |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
my $info = overload_op_info($class_or_object, $op); |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Returns a hash reference with information about the specified |
|
243
|
|
|
|
|
|
|
overloaded operator of the named class or blessed object. |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Returns C<undef> if the operator is not overloaded. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
See L<overload/Overloadable Operations> for the available operators. |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
The keys in the returned hash are as follows: |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=over |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item class |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
The name of the class in which the operator overloading was declared. |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=item code |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
A reference to the function implementing the overloaded operator. |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item code_name |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
The fully qualified name of the function implementing the overloaded operator. |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item method_name (optional) |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
The name of the method implementing the overloaded operator, if the |
|
268
|
|
|
|
|
|
|
overloading was specified as a named method, e.g. C<< use overload $op |
|
269
|
|
|
|
|
|
|
=> 'method'; >>. |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item code_class (optional) |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
The name of the class in which the method specified by C<method_name> |
|
274
|
|
|
|
|
|
|
was found. |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=item value (optional) |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
For the special C<fallback> key, the value it was given in C<class>. |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=back |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 overload_info |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
my $info = overload_info($class_or_object); |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Returns a hash reference with information about all the overloaded |
|
287
|
|
|
|
|
|
|
operators of specified class name or blessed object. The keys are the |
|
288
|
|
|
|
|
|
|
overloaded operators, as specified in C<%overload::ops> (see |
|
289
|
|
|
|
|
|
|
L<overload/Overloadable Operations>), and the values are the hashes |
|
290
|
|
|
|
|
|
|
returned by L</overload_op_info>. |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head1 CAVEATS |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Whether the C<fallback> key exists when it has its default value of |
|
295
|
|
|
|
|
|
|
C<undef> varies between perl versions: Before 5.18 it's there, in |
|
296
|
|
|
|
|
|
|
later versions it's not. |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head1 AUTHOR |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Dagfinn Ilmari Mannsåker. |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
307
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=cut |