2 package Class::MOP::Method;
8 use Scalar::Util 'reftype', 'blessed';
11 our $VERSION = '0.02';
16 require Class::MOP::Class;
17 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
25 ('CODE' eq (reftype($code) || ''))
26 || confess "You must supply a CODE reference to bless";
27 bless $code => blessed($class) || $class;
36 || confess "Can only ask the package name of a blessed CODE";
37 my $modifier_table = {
46 my $method = $code->new(sub {
47 $_->(@_) for @{$modifier_table->{before}};
49 if (defined wantarray) {
51 @rlist = $modifier_table->{around}->{cache}->(@_);
54 $rval = $modifier_table->{around}->{cache}->(@_);
58 $modifier_table->{around}->{cache}->(@_);
60 $_->(@_) for @{$modifier_table->{after}};
61 return unless defined wantarray;
62 return wantarray ? @rlist : $rval;
64 $MODIFIERS{$method} = $modifier_table;
68 sub add_before_modifier {
71 (exists $MODIFIERS{$code})
72 || confess "You must first wrap your method before adding a modifier";
74 || confess "Can only ask the package name of a blessed CODE";
75 ('CODE' eq (reftype($code) || ''))
76 || confess "You must supply a CODE reference for a modifier";
77 unshift @{$MODIFIERS{$code}->{before}} => $modifier;
80 sub add_after_modifier {
83 (exists $MODIFIERS{$code})
84 || confess "You must first wrap your method before adding a modifier";
86 || confess "Can only ask the package name of a blessed CODE";
87 ('CODE' eq (reftype($code) || ''))
88 || confess "You must supply a CODE reference for a modifier";
89 push @{$MODIFIERS{$code}->{after}} => $modifier;
93 my $compile_around_method = sub {{
97 push @_, sub { $f2->( $f1, @_ ) };
101 sub add_around_modifier {
103 my $modifier = shift;
104 (exists $MODIFIERS{$code})
105 || confess "You must first wrap your method before adding a modifier";
107 || confess "Can only ask the package name of a blessed CODE";
108 ('CODE' eq (reftype($code) || ''))
109 || confess "You must supply a CODE reference for a modifier";
110 unshift @{$MODIFIERS{$code}->{around}->{methods}} => $modifier;
111 $MODIFIERS{$code}->{around}->{cache} = $compile_around_method->(
112 @{$MODIFIERS{$code}->{around}->{methods}},
113 $MODIFIERS{$code}->{orig}
124 || confess "Can only ask the package name of a blessed CODE";
125 svref_2object($code)->GV->STASH->NAME;
131 || confess "Can only ask the package name of a blessed CODE";
132 svref_2object($code)->GV->NAME;
143 Class::MOP::Method - Method Meta Object
147 # ... more to come later maybe
151 The Method Protocol is very small, since methods in Perl 5 are just
152 subroutines within the particular package. Basically all we do is to
153 bless the subroutine.
155 Currently this package is largely unused. Future plans are to provide
156 some very simple introspection methods for the methods themselves.
157 Suggestions for this are welcome.
167 This will return a B<Class::MOP::Class> instance which is related
178 This simply blesses the C<&code> reference passed to it.
182 This wraps an existing method so that it can handle method modifiers.
192 =item B<package_name>
200 =item B<add_before_modifier ($code)>
202 =item B<add_after_modifier ($code)>
204 =item B<add_around_modifier ($code)>
210 Stevan Little E<lt>stevan@iinteractive.comE<gt>
212 =head1 COPYRIGHT AND LICENSE
214 Copyright 2006 by Infinity Interactive, Inc.
216 L<http://www.iinteractive.com>
218 This library is free software; you can redistribute it and/or modify
219 it under the same terms as Perl itself.