2 package Class::MOP::Method::Wrapped;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.90';
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},
30 if (@$before && @$after) {
31 $modifier_table->{cache} = sub {
32 for $c (@$before) { $c->(@_) };
34 ((defined wantarray) ?
36 (@rval = $around->{cache}->(@_))
38 ($rval[0] = $around->{cache}->(@_)))
40 $around->{cache}->(@_));
41 for $c (@$after) { $c->(@_) };
42 return unless defined wantarray;
43 return wantarray ? @rval : $rval[0];
46 elsif (@$before && !@$after) {
47 $modifier_table->{cache} = sub {
48 for $c (@$before) { $c->(@_) };
49 return $around->{cache}->(@_);
52 elsif (@$after && !@$before) {
53 $modifier_table->{cache} = sub {
55 ((defined wantarray) ?
57 (@rval = $around->{cache}->(@_))
59 ($rval[0] = $around->{cache}->(@_)))
61 $around->{cache}->(@_));
62 for $c (@$after) { $c->(@_) };
63 return unless defined wantarray;
64 return wantarray ? @rval : $rval[0];
68 $modifier_table->{cache} = $around->{cache};
73 my ( $class, $code, %params ) = @_;
75 (blessed($code) && $code->isa('Class::MOP::Method'))
76 || confess "Can only wrap blessed CODE";
78 my $modifier_table = {
88 $_build_wrapped_method->($modifier_table);
89 return $class->SUPER::wrap(
90 sub { $modifier_table->{cache}->(@_) },
91 # get these from the original
92 # unless explicitly overriden
93 package_name => $params{package_name} || $code->package_name,
94 name => $params{name} || $code->name,
96 modifier_table => $modifier_table,
102 return Class::MOP::Class->initialize($class)->new_object(@_)
103 if $class ne __PACKAGE__;
105 my $params = @_ == 1 ? $_[0] : {@_};
108 # inherited from Class::MOP::Method
109 'body' => $params->{body},
110 'associated_metaclass' => $params->{associated_metaclass},
111 'package_name' => $params->{package_name},
112 'name' => $params->{name},
113 'original_method' => $params->{original_method},
115 # defined in this class
116 'modifier_table' => $params->{modifier_table}
120 sub get_original_method {
122 $code->{'modifier_table'}->{orig};
125 sub add_before_modifier {
127 my $modifier = shift;
128 unshift @{$code->{'modifier_table'}->{before}} => $modifier;
129 $_build_wrapped_method->($code->{'modifier_table'});
132 sub before_modifiers {
134 return @{$code->{'modifier_table'}->{before}};
137 sub add_after_modifier {
139 my $modifier = shift;
140 push @{$code->{'modifier_table'}->{after}} => $modifier;
141 $_build_wrapped_method->($code->{'modifier_table'});
144 sub after_modifiers {
146 return @{$code->{'modifier_table'}->{after}};
151 # this is another possible candidate for
152 # optimization as well. There is an overhead
153 # associated with the currying that, if
154 # eliminated might make around modifiers
156 my $compile_around_method = sub {{
158 return $f1 unless @_;
160 push @_, sub { $f2->( $f1, @_ ) };
164 sub add_around_modifier {
166 my $modifier = shift;
167 unshift @{$code->{'modifier_table'}->{around}->{methods}} => $modifier;
168 $code->{'modifier_table'}->{around}->{cache} = $compile_around_method->(
169 @{$code->{'modifier_table'}->{around}->{methods}},
170 $code->{'modifier_table'}->{orig}->body
172 $_build_wrapped_method->($code->{'modifier_table'});
176 sub around_modifiers {
178 return @{$code->{'modifier_table'}->{around}->{methods}};
189 Class::MOP::Method::Wrapped - Method Meta Object for methods with before/after/around modifiers
193 This is a L<Class::MOP::Method> subclass which implements before,
194 after, and around method modifiers.
202 =item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
204 This is the constructor. It accepts a L<Class::MOP::Method> object and
213 The method name (without a package name). This will be taken from the
214 provided L<Class::MOP::Method> object if it is not provided.
218 The package name for the method. This will be taken from the provided
219 L<Class::MOP::Method> object if it is not provided.
221 =item * associated_metaclass
223 An optional L<Class::MOP::Class> object. This is the metaclass for the
228 =item B<< $metamethod->get_original_method >>
230 This returns the L<Class::MOP::Method> object that was passed to the
233 =item B<< $metamethod->add_before_modifier($code) >>
235 =item B<< $metamethod->add_after_modifier($code) >>
237 =item B<< $metamethod->add_around_modifier($code) >>
239 These methods all take a subroutine reference and apply it as a
240 modifier to the original method.
242 =item B<< $metamethod->before_modifiers >>
244 =item B<< $metamethod->after_modifiers >>
246 =item B<< $metamethod->around_modifiers >>
248 These methods all return a list of subroutine references which are
249 acting as the specified type of modifier.
255 Stevan Little E<lt>stevan@iinteractive.comE<gt>
257 =head1 COPYRIGHT AND LICENSE
259 Copyright 2006-2009 by Infinity Interactive, Inc.
261 L<http://www.iinteractive.com>
263 This library is free software; you can redistribute it and/or modify
264 it under the same terms as Perl itself.