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