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