a branch for deprecating alias method and working on new method semantics to preserve...
[gitmo/Class-MOP.git] / lib / Class / MOP / Method.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Method;
3
4use strict;
5use warnings;
6
2eb717d5 7use Carp 'confess';
5e607260 8use Scalar::Util 'weaken';
2eb717d5 9
94278c1b 10our $VERSION = '0.65';
d519662a 11$VERSION = eval $VERSION;
f0480c45 12our $AUTHORITY = 'cpan:STEVAN';
de19f115 13
b1897d4d 14use base 'Class::MOP::Object';
15
ce2ae40f 16# NOTE:
32202ce2 17# if poked in the right way,
ce2ae40f 18# they should act like CODE refs.
c23184fc 19use overload '&{}' => sub { $_[0]->body }, fallback => 1;
7855ddba 20
32202ce2 21our $UPGRADE_ERROR_TEXT = q{
22---------------------------------------------------------
23NOTE: this error is likely not an error, but a regression
24caused by the latest upgrade to Moose/Class::MOP. Consider
25upgrading any MooseX::* modules to their latest versions
26before spending too much time chasing this one down.
27---------------------------------------------------------
28};
29
de19f115 30# construction
31
32202ce2 32sub wrap {
5caf45ce 33 my ( $class, @args ) = @_;
34
35 unshift @args, 'body' if @args % 2 == 1;
36
37 my %params = @args;
38 my $code = $params{body};
32202ce2 39
9b522fc4 40 ('CODE' eq ref($code))
4d47b77f 41 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
32202ce2 42
b38f3848 43 ($params{package_name} && $params{name})
32202ce2 44 || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
45
0bfc85b8 46 my $self = (ref($class) || $class)->_new(\%params);
5e607260 47
48 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
49
50 return $self;
de19f115 51}
52
71b98d4f 53sub _new {
0bfc85b8 54 my $class = shift;
55 my $params = @_ == 1 ? $_[0] : {@_};
71b98d4f 56
57 my $self = bless {
0bfc85b8 58 'body' => $params->{body},
59 'associated_metaclass' => $params->{associated_metaclass},
60 'package_name' => $params->{package_name},
61 'name' => $params->{name},
71b98d4f 62 } => $class;
63}
64
ce2ae40f 65## accessors
66
8683db0e 67sub body { (shift)->{'body'} }
7855ddba 68
5e607260 69sub associated_metaclass { shift->{'associated_metaclass'} }
b1897d4d 70
5e607260 71sub attach_to_class {
72 my ( $self, $class ) = @_;
73 $self->{associated_metaclass} = $class;
74 weaken($self->{associated_metaclass});
75}
76
77sub detach_from_class {
78 my $self = shift;
79 delete $self->{associated_metaclass};
80}
de19f115 81
da88f307 82sub package_name { (shift)->{'package_name'} }
de19f115 83
da88f307 84sub name { (shift)->{'name'} }
de19f115 85
96ceced8 86sub fully_qualified_name {
0310d95d 87 my $code = shift;
88 $code->package_name . '::' . $code->name;
96ceced8 89}
90
4c105333 91# NOTE:
92# the Class::MOP bootstrap
93# will create this for us
94# - SL
95# sub clone { ... }
96
8b978dd5 971;
98
99__END__
100
101=pod
102
32202ce2 103=head1 NAME
8b978dd5 104
105Class::MOP::Method - Method Meta Object
106
8b978dd5 107=head1 DESCRIPTION
108
32202ce2 109The Method Protocol is very small, since methods in Perl 5 are just
110subroutines within the particular package. We provide a very basic
86482605 111introspection interface.
fe122940 112
2eb717d5 113=head1 METHODS
114
de19f115 115=head2 Introspection
2eb717d5 116
de19f115 117=over 4
fe122940 118
2eb717d5 119=item B<meta>
120
32202ce2 121This will return a B<Class::MOP::Class> instance which is related
fe122940 122to this class.
123
2eb717d5 124=back
125
de19f115 126=head2 Construction
127
128=over 4
129
4c105333 130=item B<wrap ($code, %params)>
127d39a7 131
32202ce2 132This is the basic constructor, it returns a B<Class::MOP::Method>
133instance which wraps the given C<$code> reference. You can also
4c105333 134set the C<package_name> and C<name> attributes using the C<%params>.
32202ce2 135If these are not set, then thier accessors will attempt to figure
4c105333 136it out using the C<Class::MOP::get_code_info> function.
137
138=item B<clone (%params)>
139
32202ce2 140This will make a copy of the object, allowing you to override
4c105333 141any values by stuffing them in C<%params>.
de19f115 142
de19f115 143=back
144
145=head2 Informational
146
147=over 4
148
7855ddba 149=item B<body>
150
127d39a7 151This returns the actual CODE reference of the particular instance.
152
de19f115 153=item B<name>
154
127d39a7 155This returns the name of the CODE reference.
156
5e607260 157=item B<associated_metaclass>
158
159The metaclass of the method
160
de19f115 161=item B<package_name>
162
127d39a7 163This returns the package name that the CODE reference is attached to.
164
96ceced8 165=item B<fully_qualified_name>
166
127d39a7 167This returns the fully qualified name of the CODE reference.
168
96ceced8 169=back
170
5e607260 171=head2 Metaclass
172
173=over 4
174
175=item B<attach_to_class>
176
177Sets the associated metaclass
178
179=item B<detach_from_class>
180
181Disassociates the method from the metaclass
182
183=back
184
1a09d9cc 185=head1 AUTHORS
8b978dd5 186
a2e85e6c 187Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 188
189=head1 COPYRIGHT AND LICENSE
190
69e3ab0a 191Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 192
193L<http://www.iinteractive.com>
194
195This library is free software; you can redistribute it and/or modify
32202ce2 196it under the same terms as Perl itself.
8b978dd5 197
16e960bd 198=cut
199