okay, this is not meant to be used, but since i am not using svk or anything, I have...
[gitmo/Class-MOP.git] / lib / Class / MOP / Method.pm
1
2 package Class::MOP::Method;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'reftype', 'blessed';
9
10 our $VERSION   = '0.07';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Object';
14
15 # NOTE:
16 # if poked in the right way, 
17 # they should act like CODE refs.
18 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
19
20 # construction
21
22 sub wrap { 
23     my ( $class, $code, %params ) = @_;
24     
25     ('CODE' eq (reftype($code) || ''))
26         || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
27     
28     bless { 
29         '&!body'         => $code,
30         '$!package_name' => $params{package_name},
31         '$!name'         => $params{name}, 
32     } => blessed($class) || $class;
33 }
34
35 ## accessors
36
37 sub body { (shift)->{'&!body'} }
38
39 # TODO - add associated_class
40
41 # informational
42
43 sub package_name { 
44     my $self = shift;
45     $self->{'$!package_name'} ||= (Class::MOP::get_code_info($self->body))[0];
46 }
47
48 sub name { 
49     my $self = shift;
50     $self->{'$!name'} ||= (Class::MOP::get_code_info($self->body))[1];
51 }
52
53 sub fully_qualified_name {
54     my $code = shift;
55     $code->package_name . '::' . $code->name;
56 }
57
58 # NOTE:
59 # the Class::MOP bootstrap
60 # will create this for us
61 # - SL
62 # sub clone { ... }
63
64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME 
71
72 Class::MOP::Method - Method Meta Object
73
74 =head1 DESCRIPTION
75
76 The Method Protocol is very small, since methods in Perl 5 are just 
77 subroutines within the particular package. We provide a very basic 
78 introspection interface.
79
80 =head1 METHODS
81
82 =head2 Introspection
83
84 =over 4
85
86 =item B<meta>
87
88 This will return a B<Class::MOP::Class> instance which is related 
89 to this class.
90
91 =back
92
93 =head2 Construction
94
95 =over 4
96
97 =item B<wrap ($code, %params)>
98
99 This is the basic constructor, it returns a B<Class::MOP::Method> 
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>.
109
110 =back
111
112 =head2 Informational
113
114 =over 4
115
116 =item B<body>
117
118 This returns the actual CODE reference of the particular instance.
119
120 =item B<name>
121
122 This returns the name of the CODE reference.
123
124 =item B<package_name>
125
126 This returns the package name that the CODE reference is attached to.
127
128 =item B<fully_qualified_name>
129
130 This returns the fully qualified name of the CODE reference.
131
132 =back
133
134 =head1 AUTHORS
135
136 Stevan Little E<lt>stevan@iinteractive.comE<gt>
137
138 =head1 COPYRIGHT AND LICENSE
139
140 Copyright 2006-2008 by Infinity Interactive, Inc.
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
147 =cut
148