bump copyright year to 2009
[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
eca95e04 9our $VERSION = '0.78';
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];
a6eef5a3 52 eval join(
0c6f3280 53 "\n",
2507ef3a 54 (
55 map {
56 /^([\@\%\$])/
57 or die "capture key should start with \@, \% or \$: $_";
58 q[my ]
59 . $_ . q[ = ]
60 . $1
61 . q[{$__captures->{']
62 . $_
63 . q['}};];
64 } keys %$__captures
65 ),
0c6f3280 66 $_[2]
67 );
7f8de9b4 68}
565f0cbb 69
12f7b801 70sub _add_line_directive {
71 my ( $self, %args ) = @_;
72
73 my ( $line, $file );
74
75 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
76 $line = $ctx->{line};
77 if ( my $desc = $ctx->{description} ) {
78 $file = "$desc defined at $ctx->{file}";
79 } else {
80 $file = $ctx->{file};
81 }
82 } else {
83 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
84 }
85
86 my $code = $args{code};
87
88 # if it's an array of lines, join it up
89 # don't use newlines so that the definition context is more meaningful
90 $code = join(@$code, ' ') if ref $code;
91
92 return qq{#line $line "$file"\n} . $code;
93}
94
95sub _compile_code {
96 my ( $self, %args ) = @_;
97
98 my $code = $self->_add_line_directive(%args);
99
100 $self->_eval_closure($args{environment}, $code);
101}
102
565f0cbb 1031;
104
105__END__
106
107=pod
108
109=head1 NAME
110
111Class::MOP::Method::Generated - Abstract base class for generated methods
112
113=head1 DESCRIPTION
114
115This is a C<Class::MOP::Method> subclass which is used interally
116by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>.
117
118=head1 METHODS
119
120=over 4
121
122=item B<new (%options)>
123
124This creates the method based on the criteria in C<%options>,
125these options are:
126
127=over 4
128
129=item I<is_inline>
130
131This is a boolean to indicate if the method should be generated
132as a closure, or as a more optimized inline version.
133
134=back
135
136=item B<is_inline>
137
138This returns the boolean which was passed into C<new>.
139
140=item B<initialize_body>
141
142This is an abstract method and will throw an exception if called.
143
144=back
145
146=head1 AUTHORS
147
148Stevan Little E<lt>stevan@iinteractive.comE<gt>
149
150=head1 COPYRIGHT AND LICENSE
151
070bb6c9 152Copyright 2006-2009 by Infinity Interactive, Inc.
565f0cbb 153
154L<http://www.iinteractive.com>
155
156This library is free software; you can redistribute it and/or modify
157it under the same terms as Perl itself.
158
159=cut
160