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