Make method body load lazily
[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
f6ca0704 9our $VERSION = '0.94';
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
868853dc 31sub body {
32 my $self = shift;
33
34 $self->{'body'} ||= do {
35 $self->_initialize_body;
36 };
37
38 return $self->{'body'};
39}
40
7f8de9b4 41sub _eval_closure {
0c6f3280 42 # my ($self, $captures, $sub_body) = @_;
43 my $__captures = $_[1];
e24b19fb 44
45 my $code;
46
47 my $e = do {
48 local $@;
49 local $SIG{__DIE__};
315ed13b 50 my $source = join
e24b19fb 51 "\n", (
2507ef3a 52 map {
53 /^([\@\%\$])/
54 or die "capture key should start with \@, \% or \$: $_";
e24b19fb 55 q[my ]
56 . $_ . q[ = ]
57 . $1
58 . q[{$__captures->{']
59 . $_ . q['}};];
60 } keys %$__captures
61 ),
62 $_[2];
cd010527 63 print STDERR "\n", $_[0]->name, ":\n", $source, "\n" if _PRINT_SOURCE;
315ed13b 64 $code = eval $source;
e24b19fb 65 $@;
66 };
67
68 return ( $code, $e );
7f8de9b4 69}
565f0cbb 70
12f7b801 71sub _add_line_directive {
72 my ( $self, %args ) = @_;
73
74 my ( $line, $file );
75
76 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
77 $line = $ctx->{line};
78 if ( my $desc = $ctx->{description} ) {
79 $file = "$desc defined at $ctx->{file}";
80 } else {
81 $file = $ctx->{file};
82 }
83 } else {
84 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
85 }
86
87 my $code = $args{code};
88
89 # if it's an array of lines, join it up
90 # don't use newlines so that the definition context is more meaningful
91 $code = join(@$code, ' ') if ref $code;
92
93 return qq{#line $line "$file"\n} . $code;
94}
95
96sub _compile_code {
97 my ( $self, %args ) = @_;
98
99 my $code = $self->_add_line_directive(%args);
100
089535f2 101 return $self->_eval_closure($args{environment}, $code);
12f7b801 102}
103
565f0cbb 1041;
105
106__END__
107
108=pod
109
110=head1 NAME
111
112Class::MOP::Method::Generated - Abstract base class for generated methods
113
114=head1 DESCRIPTION
115
653556ae 116This is a C<Class::MOP::Method> subclass which is subclassed by
117C<Class::MOP::Method::Accessor> and
118C<Class::MOP::Method::Constructor>.
565f0cbb 119
653556ae 120It is not intended to be used directly.
565f0cbb 121
122=head1 AUTHORS
123
124Stevan Little E<lt>stevan@iinteractive.comE<gt>
125
126=head1 COPYRIGHT AND LICENSE
127
070bb6c9 128Copyright 2006-2009 by Infinity Interactive, Inc.
565f0cbb 129
130L<http://www.iinteractive.com>
131
132This library is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself.
134
135=cut
136