Update Changes, bump version to 0.97_01, make copyright 2006-2010
[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
3e2c8600 9our $VERSION = '0.97_01';
d519662a 10$VERSION = eval $VERSION;
565f0cbb 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method';
14
315ed13b 15use constant _PRINT_SOURCE => $ENV{MOP_PRINT_SOURCE} ? 1 : 0;
16
0242e3f9 17## accessors
e3a72dbc 18
0242e3f9 19sub new {
20 confess __PACKAGE__ . " is an abstract base class, you must provide a constructor.";
e3a72dbc 21}
22
d9d99689 23sub is_inline { $_[0]{is_inline} }
24
25sub definition_context { $_[0]{definition_context} }
565f0cbb 26
1fd40136 27sub _initialize_body {
565f0cbb 28 confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
29}
30
7f8de9b4 31sub _eval_closure {
0c6f3280 32 # my ($self, $captures, $sub_body) = @_;
33 my $__captures = $_[1];
e24b19fb 34
35 my $code;
36
37 my $e = do {
38 local $@;
39 local $SIG{__DIE__};
315ed13b 40 my $source = join
e24b19fb 41 "\n", (
2507ef3a 42 map {
43 /^([\@\%\$])/
44 or die "capture key should start with \@, \% or \$: $_";
e24b19fb 45 q[my ]
46 . $_ . q[ = ]
47 . $1
48 . q[{$__captures->{']
49 . $_ . q['}};];
50 } keys %$__captures
51 ),
52 $_[2];
cd010527 53 print STDERR "\n", $_[0]->name, ":\n", $source, "\n" if _PRINT_SOURCE;
315ed13b 54 $code = eval $source;
e24b19fb 55 $@;
56 };
57
58 return ( $code, $e );
7f8de9b4 59}
565f0cbb 60
12f7b801 61sub _add_line_directive {
62 my ( $self, %args ) = @_;
63
64 my ( $line, $file );
65
66 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
67 $line = $ctx->{line};
68 if ( my $desc = $ctx->{description} ) {
69 $file = "$desc defined at $ctx->{file}";
70 } else {
71 $file = $ctx->{file};
72 }
73 } else {
74 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
75 }
76
77 my $code = $args{code};
78
79 # if it's an array of lines, join it up
80 # don't use newlines so that the definition context is more meaningful
81 $code = join(@$code, ' ') if ref $code;
82
83 return qq{#line $line "$file"\n} . $code;
84}
85
86sub _compile_code {
87 my ( $self, %args ) = @_;
88
89 my $code = $self->_add_line_directive(%args);
90
089535f2 91 return $self->_eval_closure($args{environment}, $code);
12f7b801 92}
93
565f0cbb 941;
95
96__END__
97
98=pod
99
100=head1 NAME
101
102Class::MOP::Method::Generated - Abstract base class for generated methods
103
104=head1 DESCRIPTION
105
653556ae 106This is a C<Class::MOP::Method> subclass which is subclassed by
107C<Class::MOP::Method::Accessor> and
108C<Class::MOP::Method::Constructor>.
565f0cbb 109
653556ae 110It is not intended to be used directly.
565f0cbb 111
112=head1 AUTHORS
113
114Stevan Little E<lt>stevan@iinteractive.comE<gt>
115
116=head1 COPYRIGHT AND LICENSE
117
3e2c8600 118Copyright 2006-2010 by Infinity Interactive, Inc.
565f0cbb 119
120L<http://www.iinteractive.com>
121
122This library is free software; you can redistribute it and/or modify
123it under the same terms as Perl itself.
124
125=cut
126