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