Commit | Line | Data |
565f0cbb |
1 | |
2 | package Class::MOP::Method::Generated; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | |
f6ca0704 |
9 | our $VERSION = '0.94'; |
d519662a |
10 | $VERSION = eval $VERSION; |
565f0cbb |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Class::MOP::Method'; |
14 | |
315ed13b |
15 | use constant _PRINT_SOURCE => $ENV{MOP_PRINT_SOURCE} ? 1 : 0; |
16 | |
0242e3f9 |
17 | ## accessors |
e3a72dbc |
18 | |
0242e3f9 |
19 | sub new { |
20 | confess __PACKAGE__ . " is an abstract base class, you must provide a constructor."; |
e3a72dbc |
21 | } |
22 | |
d9d99689 |
23 | sub is_inline { $_[0]{is_inline} } |
24 | |
25 | sub definition_context { $_[0]{definition_context} } |
565f0cbb |
26 | |
1fd40136 |
27 | sub _initialize_body { |
565f0cbb |
28 | confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class"; |
29 | } |
30 | |
868853dc |
31 | sub body { |
32 | my $self = shift; |
33 | |
34 | $self->{'body'} ||= do { |
35 | $self->_initialize_body; |
36 | }; |
37 | |
38 | return $self->{'body'}; |
39 | } |
40 | |
7f8de9b4 |
41 | sub _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 |
71 | sub _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 | |
96 | sub _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 |
104 | 1; |
105 | |
106 | __END__ |
107 | |
108 | =pod |
109 | |
110 | =head1 NAME |
111 | |
112 | Class::MOP::Method::Generated - Abstract base class for generated methods |
113 | |
114 | =head1 DESCRIPTION |
115 | |
653556ae |
116 | This is a C<Class::MOP::Method> subclass which is subclassed by |
117 | C<Class::MOP::Method::Accessor> and |
118 | C<Class::MOP::Method::Constructor>. |
565f0cbb |
119 | |
653556ae |
120 | It is not intended to be used directly. |
565f0cbb |
121 | |
122 | =head1 AUTHORS |
123 | |
124 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
125 | |
126 | =head1 COPYRIGHT AND LICENSE |
127 | |
070bb6c9 |
128 | Copyright 2006-2009 by Infinity Interactive, Inc. |
565f0cbb |
129 | |
130 | L<http://www.iinteractive.com> |
131 | |
132 | This library is free software; you can redistribute it and/or modify |
133 | it under the same terms as Perl itself. |
134 | |
135 | =cut |
136 | |