use _new for inline constructors of meta objects
[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
5e607260 45 my $self = bless {
46 'body' => $code,
47 'associated_metaclass' => $params{associated_metaclass},
48 'package_name' => $params{package_name},
49 'name' => $params{name},
50 } => ref($class) || $class;
51
52 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
53
54 return $self;
de19f115 55}
56
ce2ae40f 57## accessors
58
8683db0e 59sub body { (shift)->{'body'} }
7855ddba 60
5e607260 61sub associated_metaclass { shift->{'associated_metaclass'} }
b1897d4d 62
5e607260 63sub attach_to_class {
64 my ( $self, $class ) = @_;
65 $self->{associated_metaclass} = $class;
66 weaken($self->{associated_metaclass});
67}
68
69sub detach_from_class {
70 my $self = shift;
71 delete $self->{associated_metaclass};
72}
de19f115 73
32202ce2 74sub package_name {
4c105333 75 my $self = shift;
8683db0e 76 $self->{'package_name'} ||= (Class::MOP::get_code_info($self->body))[0];
de19f115 77}
78
32202ce2 79sub name {
4c105333 80 my $self = shift;
8683db0e 81 $self->{'name'} ||= (Class::MOP::get_code_info($self->body))[1];
2eb717d5 82}
de19f115 83
96ceced8 84sub fully_qualified_name {
4c105333 85 my $code = shift;
86 $code->package_name . '::' . $code->name;
96ceced8 87}
88
4c105333 89# NOTE:
90# the Class::MOP bootstrap
91# will create this for us
92# - SL
93# sub clone { ... }
94
8b978dd5 951;
96
97__END__
98
99=pod
100
32202ce2 101=head1 NAME
8b978dd5 102
103Class::MOP::Method - Method Meta Object
104
8b978dd5 105=head1 DESCRIPTION
106
32202ce2 107The Method Protocol is very small, since methods in Perl 5 are just
108subroutines within the particular package. We provide a very basic
86482605 109introspection interface.
fe122940 110
2eb717d5 111=head1 METHODS
112
de19f115 113=head2 Introspection
2eb717d5 114
de19f115 115=over 4
fe122940 116
2eb717d5 117=item B<meta>
118
32202ce2 119This will return a B<Class::MOP::Class> instance which is related
fe122940 120to this class.
121
2eb717d5 122=back
123
de19f115 124=head2 Construction
125
126=over 4
127
4c105333 128=item B<wrap ($code, %params)>
127d39a7 129
32202ce2 130This is the basic constructor, it returns a B<Class::MOP::Method>
131instance which wraps the given C<$code> reference. You can also
4c105333 132set the C<package_name> and C<name> attributes using the C<%params>.
32202ce2 133If these are not set, then thier accessors will attempt to figure
4c105333 134it out using the C<Class::MOP::get_code_info> function.
135
136=item B<clone (%params)>
137
32202ce2 138This will make a copy of the object, allowing you to override
4c105333 139any values by stuffing them in C<%params>.
de19f115 140
de19f115 141=back
142
143=head2 Informational
144
145=over 4
146
7855ddba 147=item B<body>
148
127d39a7 149This returns the actual CODE reference of the particular instance.
150
de19f115 151=item B<name>
152
127d39a7 153This returns the name of the CODE reference.
154
5e607260 155=item B<associated_metaclass>
156
157The metaclass of the method
158
de19f115 159=item B<package_name>
160
127d39a7 161This returns the package name that the CODE reference is attached to.
162
96ceced8 163=item B<fully_qualified_name>
164
127d39a7 165This returns the fully qualified name of the CODE reference.
166
96ceced8 167=back
168
5e607260 169=head2 Metaclass
170
171=over 4
172
173=item B<attach_to_class>
174
175Sets the associated metaclass
176
177=item B<detach_from_class>
178
179Disassociates the method from the metaclass
180
181=back
182
1a09d9cc 183=head1 AUTHORS
8b978dd5 184
a2e85e6c 185Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 186
187=head1 COPYRIGHT AND LICENSE
188
69e3ab0a 189Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 190
191L<http://www.iinteractive.com>
192
193This library is free software; you can redistribute it and/or modify
32202ce2 194it under the same terms as Perl itself.
8b978dd5 195
16e960bd 196=cut
197