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 (reftype($code) && reftype($code) eq '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}};
48 my @rval = $modifier_table->{around}->{cache}->(@_);
49 $_->(@_) for @{$modifier_table->{after}};
50 return wantarray ? @rval : $rval[0];
52 $MODIFIERS{$method} = $modifier_table;
56 sub add_before_modifier {
59 (exists $MODIFIERS{$code})
60 || confess "You must first wrap your method before adding a modifier";
62 || confess "Can only ask the package name of a blessed CODE";
63 (reftype($modifier) && reftype($modifier) eq 'CODE')
64 || confess "You must supply a CODE reference for a modifier";
65 unshift @{$MODIFIERS{$code}->{before}} => $modifier;
68 sub add_after_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 (reftype($modifier) && reftype($modifier) eq 'CODE')
76 || confess "You must supply a CODE reference for a modifier";
77 push @{$MODIFIERS{$code}->{after}} => $modifier;
81 my $compile_around_method = sub {{
85 push @_, sub { $f2->( $f1, @_ ) };
89 sub add_around_modifier {
92 (exists $MODIFIERS{$code})
93 || confess "You must first wrap your method before adding a modifier";
95 || confess "Can only ask the package name of a blessed CODE";
96 (reftype($modifier) && reftype($modifier) eq 'CODE')
97 || confess "You must supply a CODE reference for a modifier";
98 unshift @{$MODIFIERS{$code}->{around}->{methods}} => $modifier;
99 $MODIFIERS{$code}->{around}->{cache} = $compile_around_method->(
100 @{$MODIFIERS{$code}->{around}->{methods}},
101 $MODIFIERS{$code}->{orig}
112 || confess "Can only ask the package name of a blessed CODE";
113 svref_2object($code)->GV->STASH->NAME;
119 || confess "Can only ask the package name of a blessed CODE";
120 svref_2object($code)->GV->NAME;
131 Class::MOP::Method - Method Meta Object
135 # ... more to come later maybe
139 The Method Protocol is very small, since methods in Perl 5 are just
140 subroutines within the particular package. Basically all we do is to
141 bless the subroutine.
143 Currently this package is largely unused. Future plans are to provide
144 some very simple introspection methods for the methods themselves.
145 Suggestions for this are welcome.
155 This will return a B<Class::MOP::Class> instance which is related
166 This simply blesses the C<&code> reference passed to it.
176 =item B<package_name>
182 http://dirtsimple.org/2005/01/clos-style-method-combination-for.html
184 http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html
188 Stevan Little E<lt>stevan@iinteractive.comE<gt>
190 =head1 COPYRIGHT AND LICENSE
192 Copyright 2006 by Infinity Interactive, Inc.
194 L<http://www.iinteractive.com>
196 This library is free software; you can redistribute it and/or modify
197 it under the same terms as Perl itself.