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