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