Hey, here's a crazy idea, let's die we try to generate some code that
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Generated.pm
CommitLineData
565f0cbb 1
2package Class::MOP::Method::Generated;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8
b4bd10ec 9our $VERSION = '0.77';
d519662a 10$VERSION = eval $VERSION;
565f0cbb 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method';
14
15sub 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 29sub _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 41sub is_inline { $_[0]{is_inline} }
42
43sub definition_context { $_[0]{definition_context} }
565f0cbb 44
45sub initialize_body {
46 confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
47}
48
7f8de9b4 49sub _eval_closure {
0c6f3280 50 # my ($self, $captures, $sub_body) = @_;
51 my $__captures = $_[1];
e7a64dad 52
53 local $@;
0c6f3280 54 eval join(
55 "\n",
2507ef3a 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 ),
0c6f3280 68 $_[2]
69 );
e7a64dad 70 die $@ if $@;
7f8de9b4 71}
565f0cbb 72
12f7b801 73sub _add_line_directive {
74 my ( $self, %args ) = @_;
75
76 my ( $line, $file );
77
78 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
79 $line = $ctx->{line};
80 if ( my $desc = $ctx->{description} ) {
81 $file = "$desc defined at $ctx->{file}";
82 } else {
83 $file = $ctx->{file};
84 }
85 } else {
86 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
87 }
88
89 my $code = $args{code};
90
91 # if it's an array of lines, join it up
92 # don't use newlines so that the definition context is more meaningful
93 $code = join(@$code, ' ') if ref $code;
94
95 return qq{#line $line "$file"\n} . $code;
96}
97
98sub _compile_code {
99 my ( $self, %args ) = @_;
100
101 my $code = $self->_add_line_directive(%args);
102
103 $self->_eval_closure($args{environment}, $code);
104}
105
565f0cbb 1061;
107
108__END__
109
110=pod
111
112=head1 NAME
113
114Class::MOP::Method::Generated - Abstract base class for generated methods
115
116=head1 DESCRIPTION
117
118This is a C<Class::MOP::Method> subclass which is used interally
119by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>.
120
121=head1 METHODS
122
123=over 4
124
125=item B<new (%options)>
126
127This creates the method based on the criteria in C<%options>,
128these options are:
129
130=over 4
131
132=item I<is_inline>
133
134This is a boolean to indicate if the method should be generated
135as a closure, or as a more optimized inline version.
136
137=back
138
139=item B<is_inline>
140
141This returns the boolean which was passed into C<new>.
142
143=item B<initialize_body>
144
145This is an abstract method and will throw an exception if called.
146
147=back
148
149=head1 AUTHORS
150
151Stevan Little E<lt>stevan@iinteractive.comE<gt>
152
153=head1 COPYRIGHT AND LICENSE
154
69e3ab0a 155Copyright 2006-2008 by Infinity Interactive, Inc.
565f0cbb 156
157L<http://www.iinteractive.com>
158
159This library is free software; you can redistribute it and/or modify
160it under the same terms as Perl itself.
161
162=cut
163