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