Commit | Line | Data |
565f0cbb |
1 | |
2 | package Class::MOP::Method::Generated; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | |
db4c4962 |
9 | our $VERSION = '0.75'; |
d519662a |
10 | $VERSION = eval $VERSION; |
565f0cbb |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Class::MOP::Method'; |
14 | |
15 | sub new { |
16 | my $class = shift; |
17 | my %options = @_; |
18 | |
b38f3848 |
19 | ($options{package_name} && $options{name}) |
32202ce2 |
20 | || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; |
b38f3848 |
21 | |
0bfc85b8 |
22 | my $self = $class->_new(\%options); |
565f0cbb |
23 | |
24 | $self->initialize_body; |
25 | |
26 | return $self; |
27 | } |
28 | |
e3a72dbc |
29 | sub _new { |
0bfc85b8 |
30 | my $class = shift; |
31 | my $options = @_ == 1 ? $_[0] : {@_}; |
e3a72dbc |
32 | |
0bfc85b8 |
33 | $options->{is_inline} ||= 0; |
34 | $options->{body} ||= undef; |
e3a72dbc |
35 | |
0bfc85b8 |
36 | bless $options, $class; |
e3a72dbc |
37 | } |
38 | |
565f0cbb |
39 | ## accessors |
40 | |
d9d99689 |
41 | sub is_inline { $_[0]{is_inline} } |
42 | |
43 | sub definition_context { $_[0]{definition_context} } |
565f0cbb |
44 | |
45 | sub initialize_body { |
46 | confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class"; |
47 | } |
48 | |
7f8de9b4 |
49 | sub _eval_closure { |
0c6f3280 |
50 | # my ($self, $captures, $sub_body) = @_; |
51 | my $__captures = $_[1]; |
52 | eval join( |
53 | "\n", |
54 | (map { |
55 | /^([\@\%\$])/ |
56 | or die "capture key should start with \@, \% or \$: $_"; |
57 | q!my !.$_.q! = !.$1.q!{$__captures->{'!.$_.q!'}};!; |
58 | } keys %$__captures), |
59 | $_[2] |
60 | ); |
7f8de9b4 |
61 | } |
565f0cbb |
62 | |
12f7b801 |
63 | sub _add_line_directive { |
64 | my ( $self, %args ) = @_; |
65 | |
66 | my ( $line, $file ); |
67 | |
68 | if ( my $ctx = ( $args{context} || $self->definition_context ) ) { |
69 | $line = $ctx->{line}; |
70 | if ( my $desc = $ctx->{description} ) { |
71 | $file = "$desc defined at $ctx->{file}"; |
72 | } else { |
73 | $file = $ctx->{file}; |
74 | } |
75 | } else { |
76 | ( $line, $file ) = ( 0, "generated method (unknown origin)" ); |
77 | } |
78 | |
79 | my $code = $args{code}; |
80 | |
81 | # if it's an array of lines, join it up |
82 | # don't use newlines so that the definition context is more meaningful |
83 | $code = join(@$code, ' ') if ref $code; |
84 | |
85 | return qq{#line $line "$file"\n} . $code; |
86 | } |
87 | |
88 | sub _compile_code { |
89 | my ( $self, %args ) = @_; |
90 | |
91 | my $code = $self->_add_line_directive(%args); |
92 | |
93 | $self->_eval_closure($args{environment}, $code); |
94 | } |
95 | |
565f0cbb |
96 | 1; |
97 | |
98 | __END__ |
99 | |
100 | =pod |
101 | |
102 | =head1 NAME |
103 | |
104 | Class::MOP::Method::Generated - Abstract base class for generated methods |
105 | |
106 | =head1 DESCRIPTION |
107 | |
108 | This is a C<Class::MOP::Method> subclass which is used interally |
109 | by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>. |
110 | |
111 | =head1 METHODS |
112 | |
113 | =over 4 |
114 | |
115 | =item B<new (%options)> |
116 | |
117 | This creates the method based on the criteria in C<%options>, |
118 | these options are: |
119 | |
120 | =over 4 |
121 | |
122 | =item I<is_inline> |
123 | |
124 | This is a boolean to indicate if the method should be generated |
125 | as a closure, or as a more optimized inline version. |
126 | |
127 | =back |
128 | |
129 | =item B<is_inline> |
130 | |
131 | This returns the boolean which was passed into C<new>. |
132 | |
133 | =item B<initialize_body> |
134 | |
135 | This is an abstract method and will throw an exception if called. |
136 | |
137 | =back |
138 | |
139 | =head1 AUTHORS |
140 | |
141 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
142 | |
143 | =head1 COPYRIGHT AND LICENSE |
144 | |
69e3ab0a |
145 | Copyright 2006-2008 by Infinity Interactive, Inc. |
565f0cbb |
146 | |
147 | L<http://www.iinteractive.com> |
148 | |
149 | This library is free software; you can redistribute it and/or modify |
150 | it under the same terms as Perl itself. |
151 | |
152 | =cut |
153 | |