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}};
49 if (defined wantarray) {
50 @rval = $modifier_table->{around}->{cache}->(@_);
53 $modifier_table->{around}->{cache}->(@_);
55 $_->(@_) for @{$modifier_table->{after}};
56 return unless defined wantarray;
57 return wantarray ? @rval : $rval[0];
59 $MODIFIERS{$method} = $modifier_table;
63 sub add_before_modifier {
66 (exists $MODIFIERS{$code})
67 || confess "You must first wrap your method before adding a modifier";
69 || confess "Can only ask the package name of a blessed CODE";
70 (reftype($modifier) && reftype($modifier) eq 'CODE')
71 || confess "You must supply a CODE reference for a modifier";
72 unshift @{$MODIFIERS{$code}->{before}} => $modifier;
75 sub add_after_modifier {
78 (exists $MODIFIERS{$code})
79 || confess "You must first wrap your method before adding a modifier";
81 || confess "Can only ask the package name of a blessed CODE";
82 (reftype($modifier) && reftype($modifier) eq 'CODE')
83 || confess "You must supply a CODE reference for a modifier";
84 push @{$MODIFIERS{$code}->{after}} => $modifier;
88 my $compile_around_method = sub {{
92 push @_, sub { $f2->( $f1, @_ ) };
96 sub add_around_modifier {
99 (exists $MODIFIERS{$code})
100 || confess "You must first wrap your method before adding a modifier";
102 || confess "Can only ask the package name of a blessed CODE";
103 (reftype($modifier) && reftype($modifier) eq 'CODE')
104 || confess "You must supply a CODE reference for a modifier";
105 unshift @{$MODIFIERS{$code}->{around}->{methods}} => $modifier;
106 $MODIFIERS{$code}->{around}->{cache} = $compile_around_method->(
107 @{$MODIFIERS{$code}->{around}->{methods}},
108 $MODIFIERS{$code}->{orig}
119 || confess "Can only ask the package name of a blessed CODE";
120 svref_2object($code)->GV->STASH->NAME;
126 || confess "Can only ask the package name of a blessed CODE";
127 svref_2object($code)->GV->NAME;
138 Class::MOP::Method - Method Meta Object
142 # ... more to come later maybe
146 The Method Protocol is very small, since methods in Perl 5 are just
147 subroutines within the particular package. Basically all we do is to
148 bless the subroutine.
150 Currently this package is largely unused. Future plans are to provide
151 some very simple introspection methods for the methods themselves.
152 Suggestions for this are welcome.
162 This will return a B<Class::MOP::Class> instance which is related
173 This simply blesses the C<&code> reference passed to it.
183 =item B<package_name>
189 http://dirtsimple.org/2005/01/clos-style-method-combination-for.html
191 http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html
195 Stevan Little E<lt>stevan@iinteractive.comE<gt>
197 =head1 COPYRIGHT AND LICENSE
199 Copyright 2006 by Infinity Interactive, Inc.
201 L<http://www.iinteractive.com>
203 This library is free software; you can redistribute it and/or modify
204 it under the same terms as Perl itself.