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