1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
package Class::MOP::Overload;
our $VERSION = '2.1405';
use strict;
use warnings;
use overload ();
use Scalar::Util qw( blessed weaken );
use Try::Tiny;
use parent 'Class::MOP::Object';
my %Operators = (
map { $_ => 1 }
grep { $_ ne 'fallback' }
map { split /\s+/ } values %overload::ops
);
sub new {
my ( $class, %params ) = @_;
unless ( defined $params{operator} ) {
$class->_throw_exception('OverloadRequiresAnOperator');
}
unless ( $Operators{ $params{operator} } ) {
$class->_throw_exception(
'InvalidOverloadOperator',
operator => $params{operator},
);
}
unless ( defined $params{method_name} || $params{coderef} ) {
$class->_throw_exception(
'OverloadRequiresAMethodNameOrCoderef',
operator => $params{operator},
);
}
if ( $params{coderef} ) {
unless ( defined $params{coderef_package}
&& defined $params{coderef_name} ) {
$class->_throw_exception('OverloadRequiresNamesForCoderef');
}
}
if ( $params{method}
&& !try { $params{method}->isa('Class::MOP::Method') } ) {
$class->_throw_exception('OverloadRequiresAMetaMethod');
}
if ( $params{associated_metaclass}
&& !try { $params{associated_metaclass}->isa('Class::MOP::Module') } )
{
$class->_throw_exception('OverloadRequiresAMetaClass');
}
my @optional_attrs
= qw( method_name coderef coderef_package coderef_name method associated_metaclass );
return bless {
operator => $params{operator},
map { defined $params{$_} ? ( $_ => $params{$_} ) : () }
@optional_attrs
},
$class;
}
sub operator { $_[0]->{operator} }
sub method_name { $_[0]->{method_name} }
sub has_method_name { exists $_[0]->{method_name} }
sub method { $_[0]->{method} }
sub has_method { exists $_[0]->{method} }
sub coderef { $_[0]->{coderef} }
sub has_coderef { exists $_[0]->{coderef} }
sub coderef_package { $_[0]->{coderef_package} }
sub has_coderef_package { exists $_[0]->{coderef_package} }
sub coderef_name { $_[0]->{coderef_name} }
sub has_coderef_name { exists $_[0]->{coderef_name} }
sub associated_metaclass { $_[0]->{associated_metaclass} }
sub is_anonymous {
my $self = shift;
return $self->has_coderef && $self->coderef_name eq '__ANON__';
}
sub attach_to_class {
my ( $self, $class ) = @_;
$self->{associated_metaclass} = $class;
weaken $self->{associated_metaclass};
}
sub clone {
my $self = shift;
my $clone = bless { %{$self}, @_ }, blessed($self);
weaken $clone->{associated_metaclass} if $clone->{associated_metaclass};
$clone->_set_original_overload($self);
return $clone;
}
sub original_overload { $_[0]->{original_overload} }
sub _set_original_overload { $_[0]->{original_overload} = $_[1] }
sub _is_equal_to {
my $self = shift;
my $other = shift;
if ( $self->has_coderef ) {
return unless $other->has_coderef;
return $self->coderef == $other->coderef;
}
else {
return $self->method_name eq $other->method_name;
}
}
1;
# ABSTRACT: Overload Meta Object
__END__
=pod
=encoding UTF-8
=head1 NAME
Class::MOP::Overload - Overload Meta Object
=head1 VERSION
version 2.1405
=head1 SYNOPSIS
my $meta = Class->meta;
my $overload = $meta->get_overloaded_operator('+');
if ( $overload->has_method_name ) {
print 'Method for + is ', $overload->method_name, "\n";
}
else {
print 'Overloading for + is implemented by ',
$overload->coderef_name, " sub\n";
}
=head1 DESCRIPTION
This class provides meta information for overloading in classes and roles.
=head1 INHERITANCE
C<Class::MOP::Overload> is a subclass of L<Class::MOP::Object>.
=head1 METHODS
This class provides the following methods:
=head2 Class::MOP::Overload->new(%options)
This method creates a new C<Class::MOP::Overload> object. It accepts a number
of options:
=over 4
=item * operator
This is a string that matches an operator known by the L<overload> module,
such as C<""> or C<+>. This is required.
=item * method_name
The name of the method which implements the overloading. Note that this does
not need to actually correspond to a real method, since it's okay to declare a
not-yet-implemented overloading.
Either this or the C<coderef> option must be passed.
=item * method
A L<Class::MOP::Method> object for the method which implements the
overloading.
This is optional.
=item * coderef
A coderef which implements the overloading.
Either this or the C<method_name> option must be passed.
=item * coderef_package
The package where the coderef was defined.
This is required if C<coderef> is passed.
=item * coderef_name
The name of the coderef. This can be "__ANON__".
This is required if C<coderef> is passed.
=item * associated_metaclass
A L<Class::MOP::Module> object for the associated class or role.
This is optional.
=back
=head2 $overload->operator
Returns the operator for this overload object.
=head2 $overload->method_name
Returns the method name that implements overloading, if it has one.
=head2 $overload->has_method_name
Returns true if the object has a method name.
=head2 $overload->method
Returns the L<Class::MOP::Method> that implements overloading, if it has one.
=head2 $overload->has_method
Returns true if the object has a method.
=head2 $overload->coderef
Returns the coderef that implements overloading, if it has one.
=head2 $overload->has_coderef
Returns true if the object has a coderef.
=head2 $overload->coderef_package
Returns the package for the coderef that implements overloading, if it has
one.
=head2 $overload->has_coderef
Returns true if the object has a coderef package.
=head2 $overload->coderef_name
Returns the sub name for the coderef that implements overloading, if it has
one.
=head2 $overload->has_coderef_name
Returns true if the object has a coderef name.
=head2 $overload->is_anonymous
Returns true if the overloading is implemented by an anonymous coderef.
=head2 $overload->associated_metaclass
Returns the L<Class::MOP::Module> (class or role) that is associated with the
overload object.
=head2 $overload->clone
Clones the overloading object, setting C<original_overload> in the process.
=head2 $overload->original_overload
For cloned objects, this returns the L<Class::MOP::Overload> object from which
they were cloned. This can be used to determine the source of an overloading
in a class that came from a role, for example.
=head1 AUTHORS
=over 4
=item *
Stevan Little <stevan.little@iinteractive.com>
=item *
Dave Rolsky <autarch@urth.org>
=item *
Jesse Luehrs <doy@tozt.net>
=item *
Shawn M Moore <code@sartak.org>
=item *
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
=item *
Karen Etheridge <ether@cpan.org>
=item *
Florian Ragwitz <rafl@debian.org>
=item *
Hans Dieter Pearcey <hdp@weftsoar.net>
=item *
Chris Prather <chris@prather.org>
=item *
Matt S Trout <mst@shadowcat.co.uk>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc..
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|