Commit | Line | Data |
565f0cbb |
1 | |
2 | package Class::MOP::Method::Generated; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | |
a9f48b4b |
9 | our $VERSION = '1.11'; |
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 | |
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 { |
d2d9edc0 |
26 | my ($self, $__captures, $sub_body) = @_; |
e24b19fb |
27 | |
28 | my $code; |
29 | |
30 | my $e = do { |
31 | local $@; |
32 | local $SIG{__DIE__}; |
315ed13b |
33 | my $source = join |
e24b19fb |
34 | "\n", ( |
2507ef3a |
35 | map { |
36 | /^([\@\%\$])/ |
37 | or die "capture key should start with \@, \% or \$: $_"; |
e24b19fb |
38 | q[my ] |
39 | . $_ . q[ = ] |
40 | . $1 |
41 | . q[{$__captures->{'] |
42 | . $_ . q['}};]; |
43 | } keys %$__captures |
44 | ), |
d2d9edc0 |
45 | $sub_body; |
46 | |
47 | $self->_dump_source($source) if $ENV{MOP_PRINT_SOURCE}; |
48 | |
315ed13b |
49 | $code = eval $source; |
e24b19fb |
50 | $@; |
51 | }; |
52 | |
53 | return ( $code, $e ); |
7f8de9b4 |
54 | } |
565f0cbb |
55 | |
d2d9edc0 |
56 | sub _dump_source { |
57 | my ( $self, $source ) = @_; |
58 | |
59 | my $output; |
60 | if ( eval { require Perl::Tidy } ) { |
61 | require File::Spec; |
62 | |
63 | my $rc_file = File::Spec->catfile( |
64 | $INC{'Class/MOP/Method/Generated.pm'}, |
65 | ('..') x 5, |
66 | 'perltidyrc' |
67 | ); |
d2d9edc0 |
68 | |
69 | my %p = ( |
70 | source => \$source, |
71 | destination => \$output, |
72 | ); |
73 | $p{perltidyrc} = $rc_file |
74 | if -f $rc_file; |
75 | |
76 | Perl::Tidy::perltidy(%p); |
77 | } |
78 | else { |
79 | $output = $source; |
80 | } |
81 | |
82 | print STDERR "\n", $self->name, ":\n", $output, "\n"; |
83 | } |
84 | |
12f7b801 |
85 | sub _add_line_directive { |
86 | my ( $self, %args ) = @_; |
87 | |
88 | my ( $line, $file ); |
89 | |
90 | if ( my $ctx = ( $args{context} || $self->definition_context ) ) { |
91 | $line = $ctx->{line}; |
92 | if ( my $desc = $ctx->{description} ) { |
93 | $file = "$desc defined at $ctx->{file}"; |
94 | } else { |
95 | $file = $ctx->{file}; |
96 | } |
97 | } else { |
98 | ( $line, $file ) = ( 0, "generated method (unknown origin)" ); |
99 | } |
100 | |
101 | my $code = $args{code}; |
102 | |
103 | # if it's an array of lines, join it up |
104 | # don't use newlines so that the definition context is more meaningful |
105 | $code = join(@$code, ' ') if ref $code; |
106 | |
107 | return qq{#line $line "$file"\n} . $code; |
108 | } |
109 | |
110 | sub _compile_code { |
111 | my ( $self, %args ) = @_; |
112 | |
113 | my $code = $self->_add_line_directive(%args); |
114 | |
089535f2 |
115 | return $self->_eval_closure($args{environment}, $code); |
12f7b801 |
116 | } |
117 | |
565f0cbb |
118 | 1; |
119 | |
120 | __END__ |
121 | |
122 | =pod |
123 | |
124 | =head1 NAME |
125 | |
126 | Class::MOP::Method::Generated - Abstract base class for generated methods |
127 | |
128 | =head1 DESCRIPTION |
129 | |
653556ae |
130 | This is a C<Class::MOP::Method> subclass which is subclassed by |
131 | C<Class::MOP::Method::Accessor> and |
132 | C<Class::MOP::Method::Constructor>. |
565f0cbb |
133 | |
653556ae |
134 | It is not intended to be used directly. |
565f0cbb |
135 | |
136 | =head1 AUTHORS |
137 | |
138 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
139 | |
140 | =head1 COPYRIGHT AND LICENSE |
141 | |
3e2c8600 |
142 | Copyright 2006-2010 by Infinity Interactive, Inc. |
565f0cbb |
143 | |
144 | L<http://www.iinteractive.com> |
145 | |
146 | This library is free software; you can redistribute it and/or modify |
147 | it under the same terms as Perl itself. |
148 | |
149 | =cut |
150 | |