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 = { before => [], after => [] };
38 my $method = $code->new(sub {
39 $_->(@_) for @{$modifier_table->{before}};
41 # we actually need to be sure to preserve
42 # the calling context and call this method
43 # with the same context too. This just
44 # requires some bookkeeping code, thats all.
45 my @rval = $code->(@_);
46 $_->(@_) for @{$modifier_table->{after}};
47 return wantarray ? @rval : $rval[0];
49 $MODIFIERS{$method} = $modifier_table;
53 sub add_before_modifier {
56 (exists $MODIFIERS{$code})
57 || confess "You must first wrap your method before adding a modifier";
59 || confess "Can only ask the package name of a blessed CODE";
60 (reftype($modifier) && reftype($modifier) eq 'CODE')
61 || confess "You must supply a CODE reference for a modifier";
62 unshift @{$MODIFIERS{$code}->{before}} => $modifier;
65 sub add_after_modifier {
68 (exists $MODIFIERS{$code})
69 || confess "You must first wrap your method before adding a modifier";
71 || confess "Can only ask the package name of a blessed CODE";
72 (reftype($modifier) && reftype($modifier) eq 'CODE')
73 || confess "You must supply a CODE reference for a modifier";
74 push @{$MODIFIERS{$code}->{after}} => $modifier;
83 || confess "Can only ask the package name of a blessed CODE";
84 svref_2object($code)->GV->STASH->NAME;
90 || confess "Can only ask the package name of a blessed CODE";
91 svref_2object($code)->GV->NAME;
102 Class::MOP::Method - Method Meta Object
106 # ... more to come later maybe
110 The Method Protocol is very small, since methods in Perl 5 are just
111 subroutines within the particular package. Basically all we do is to
112 bless the subroutine.
114 Currently this package is largely unused. Future plans are to provide
115 some very simple introspection methods for the methods themselves.
116 Suggestions for this are welcome.
126 This will return a B<Class::MOP::Class> instance which is related
137 This simply blesses the C<&code> reference passed to it.
147 =item B<package_name>
153 http://dirtsimple.org/2005/01/clos-style-method-combination-for.html
155 http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html
159 Stevan Little E<lt>stevan@iinteractive.comE<gt>
161 =head1 COPYRIGHT AND LICENSE
163 Copyright 2006 by Infinity Interactive, Inc.
165 L<http://www.iinteractive.com>
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself.