2 package Class::MOP::Method::Wrapped;
8 use Scalar::Util 'blessed';
10 our $VERSION = '1.12';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method';
17 # this ugly beast is the result of trying
18 # to micro optimize this as much as possible
19 # while not completely loosing maintainability.
20 # At this point it's "fast enough", after all
21 # you can't get something for nothing :)
22 my $_build_wrapped_method = sub {
23 my $modifier_table = shift;
24 my ($before, $after, $around) = (
25 $modifier_table->{before},
26 $modifier_table->{after},
27 $modifier_table->{around},
29 if (@$before && @$after) {
30 $modifier_table->{cache} = sub {
31 for my $c (@$before) { $c->(@_) };
33 ((defined wantarray) ?
35 (@rval = $around->{cache}->(@_))
37 ($rval[0] = $around->{cache}->(@_)))
39 $around->{cache}->(@_));
40 for my $c (@$after) { $c->(@_) };
41 return unless defined wantarray;
42 return wantarray ? @rval : $rval[0];
45 elsif (@$before && !@$after) {
46 $modifier_table->{cache} = sub {
47 for my $c (@$before) { $c->(@_) };
48 return $around->{cache}->(@_);
51 elsif (@$after && !@$before) {
52 $modifier_table->{cache} = sub {
54 ((defined wantarray) ?
56 (@rval = $around->{cache}->(@_))
58 ($rval[0] = $around->{cache}->(@_)))
60 $around->{cache}->(@_));
61 for my $c (@$after) { $c->(@_) };
62 return unless defined wantarray;
63 return wantarray ? @rval : $rval[0];
67 $modifier_table->{cache} = $around->{cache};
72 my ( $class, $code, %params ) = @_;
74 (blessed($code) && $code->isa('Class::MOP::Method'))
75 || confess "Can only wrap blessed CODE";
77 my $modifier_table = {
87 $_build_wrapped_method->($modifier_table);
88 return $class->SUPER::wrap(
89 sub { $modifier_table->{cache}->(@_) },
90 # get these from the original
91 # unless explicitly overriden
92 package_name => $params{package_name} || $code->package_name,
93 name => $params{name} || $code->name,
95 modifier_table => $modifier_table,
101 return Class::MOP::Class->initialize($class)->new_object(@_)
102 if $class ne __PACKAGE__;
104 my $params = @_ == 1 ? $_[0] : {@_};
107 # inherited from Class::MOP::Method
108 'body' => $params->{body},
109 'associated_metaclass' => $params->{associated_metaclass},
110 'package_name' => $params->{package_name},
111 'name' => $params->{name},
112 'original_method' => $params->{original_method},
114 # defined in this class
115 'modifier_table' => $params->{modifier_table}
119 sub get_original_method {
121 $code->{'modifier_table'}->{orig};
124 sub add_before_modifier {
126 my $modifier = shift;
127 unshift @{$code->{'modifier_table'}->{before}} => $modifier;
128 $_build_wrapped_method->($code->{'modifier_table'});
131 sub before_modifiers {
133 return @{$code->{'modifier_table'}->{before}};
136 sub add_after_modifier {
138 my $modifier = shift;
139 push @{$code->{'modifier_table'}->{after}} => $modifier;
140 $_build_wrapped_method->($code->{'modifier_table'});
143 sub after_modifiers {
145 return @{$code->{'modifier_table'}->{after}};
150 # this is another possible candidate for
151 # optimization as well. There is an overhead
152 # associated with the currying that, if
153 # eliminated might make around modifiers
155 my $compile_around_method = sub {{
157 return $f1 unless @_;
159 push @_, sub { $f2->( $f1, @_ ) };
163 sub add_around_modifier {
165 my $modifier = shift;
166 unshift @{$code->{'modifier_table'}->{around}->{methods}} => $modifier;
167 $code->{'modifier_table'}->{around}->{cache} = $compile_around_method->(
168 @{$code->{'modifier_table'}->{around}->{methods}},
169 $code->{'modifier_table'}->{orig}->body
171 $_build_wrapped_method->($code->{'modifier_table'});
175 sub around_modifiers {
177 return @{$code->{'modifier_table'}->{around}->{methods}};
180 sub _make_compatible_with {
184 # XXX: this is pretty gross. the issue here is that CMOP::Method::Wrapped
185 # objects are subclasses of CMOP::Method, but when we get to moose, they'll
186 # need to be compatible with Moose::Meta::Method, which isn't possible. the
187 # right solution here is to make ::Wrapped into a role that gets applied to
188 # whatever the method_metaclass happens to be and get rid of
189 # wrapped_method_metaclass entirely, but that's not going to happen until
190 # we ditch cmop and get roles into the bootstrapping, so. i'm not
191 # maintaining the previous behavior of turning them into instances of the
192 # new method_metaclass because that's equally broken, and at least this way
193 # any issues will at least be detectable and potentially fixable. -doy
194 return $self unless $other->_is_compatible_with($self->_real_ref_name);
196 return $self->SUPER::_make_compatible_with(@_);
207 Class::MOP::Method::Wrapped - Method Meta Object for methods with before/after/around modifiers
211 This is a L<Class::MOP::Method> subclass which implements before,
212 after, and around method modifiers.
220 =item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
222 This is the constructor. It accepts a L<Class::MOP::Method> object and
231 The method name (without a package name). This will be taken from the
232 provided L<Class::MOP::Method> object if it is not provided.
236 The package name for the method. This will be taken from the provided
237 L<Class::MOP::Method> object if it is not provided.
239 =item * associated_metaclass
241 An optional L<Class::MOP::Class> object. This is the metaclass for the
246 =item B<< $metamethod->get_original_method >>
248 This returns the L<Class::MOP::Method> object that was passed to the
251 =item B<< $metamethod->add_before_modifier($code) >>
253 =item B<< $metamethod->add_after_modifier($code) >>
255 =item B<< $metamethod->add_around_modifier($code) >>
257 These methods all take a subroutine reference and apply it as a
258 modifier to the original method.
260 =item B<< $metamethod->before_modifiers >>
262 =item B<< $metamethod->after_modifiers >>
264 =item B<< $metamethod->around_modifiers >>
266 These methods all return a list of subroutine references which are
267 acting as the specified type of modifier.
273 Stevan Little E<lt>stevan@iinteractive.comE<gt>
275 =head1 COPYRIGHT AND LICENSE
277 Copyright 2006-2010 by Infinity Interactive, Inc.
279 L<http://www.iinteractive.com>
281 This library is free software; you can redistribute it and/or modify
282 it under the same terms as Perl itself.