One last tweak to make sure our Sub::Name-using tests _do_ run when we
[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';
9b522fc4 8use Scalar::Util 'blessed';
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 {
4c105333 32 my ( $class, $code, %params ) = @_;
32202ce2 33
9b522fc4 34 ('CODE' eq ref($code))
4d47b77f 35 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
32202ce2 36
b38f3848 37 ($params{package_name} && $params{name})
32202ce2 38 || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
39
40 bless {
4c105333 41 '&!body' => $code,
42 '$!package_name' => $params{package_name},
32202ce2 43 '$!name' => $params{name},
7855ddba 44 } => blessed($class) || $class;
de19f115 45}
46
ce2ae40f 47## accessors
48
c23184fc 49sub body { (shift)->{'&!body'} }
7855ddba 50
b1897d4d 51# TODO - add associated_class
52
de19f115 53# informational
54
32202ce2 55sub package_name {
4c105333 56 my $self = shift;
57 $self->{'$!package_name'} ||= (Class::MOP::get_code_info($self->body))[0];
de19f115 58}
59
32202ce2 60sub name {
4c105333 61 my $self = shift;
62 $self->{'$!name'} ||= (Class::MOP::get_code_info($self->body))[1];
2eb717d5 63}
de19f115 64
96ceced8 65sub fully_qualified_name {
4c105333 66 my $code = shift;
67 $code->package_name . '::' . $code->name;
96ceced8 68}
69
4c105333 70# NOTE:
71# the Class::MOP bootstrap
72# will create this for us
73# - SL
74# sub clone { ... }
75
8b978dd5 761;
77
78__END__
79
80=pod
81
32202ce2 82=head1 NAME
8b978dd5 83
84Class::MOP::Method - Method Meta Object
85
8b978dd5 86=head1 DESCRIPTION
87
32202ce2 88The Method Protocol is very small, since methods in Perl 5 are just
89subroutines within the particular package. We provide a very basic
86482605 90introspection interface.
fe122940 91
2eb717d5 92=head1 METHODS
93
de19f115 94=head2 Introspection
2eb717d5 95
de19f115 96=over 4
fe122940 97
2eb717d5 98=item B<meta>
99
32202ce2 100This will return a B<Class::MOP::Class> instance which is related
fe122940 101to this class.
102
2eb717d5 103=back
104
de19f115 105=head2 Construction
106
107=over 4
108
4c105333 109=item B<wrap ($code, %params)>
127d39a7 110
32202ce2 111This is the basic constructor, it returns a B<Class::MOP::Method>
112instance which wraps the given C<$code> reference. You can also
4c105333 113set the C<package_name> and C<name> attributes using the C<%params>.
32202ce2 114If these are not set, then thier accessors will attempt to figure
4c105333 115it out using the C<Class::MOP::get_code_info> function.
116
117=item B<clone (%params)>
118
32202ce2 119This will make a copy of the object, allowing you to override
4c105333 120any values by stuffing them in C<%params>.
de19f115 121
de19f115 122=back
123
124=head2 Informational
125
126=over 4
127
7855ddba 128=item B<body>
129
127d39a7 130This returns the actual CODE reference of the particular instance.
131
de19f115 132=item B<name>
133
127d39a7 134This returns the name of the CODE reference.
135
de19f115 136=item B<package_name>
137
127d39a7 138This returns the package name that the CODE reference is attached to.
139
96ceced8 140=item B<fully_qualified_name>
141
127d39a7 142This returns the fully qualified name of the CODE reference.
143
96ceced8 144=back
145
1a09d9cc 146=head1 AUTHORS
8b978dd5 147
a2e85e6c 148Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 149
150=head1 COPYRIGHT AND LICENSE
151
69e3ab0a 152Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 153
154L<http://www.iinteractive.com>
155
156This library is free software; you can redistribute it and/or modify
32202ce2 157it under the same terms as Perl itself.
8b978dd5 158
16e960bd 159=cut
160