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