Use Perl::Tidy to tidy dump of generate source code if we can.
[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
2440993d 9our $VERSION = '1.08';
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 );
72 warn $rc_file;
73
74 my %p = (
75 source => \$source,
76 destination => \$output,
77 );
78 $p{perltidyrc} = $rc_file
79 if -f $rc_file;
80
81 Perl::Tidy::perltidy(%p);
82 }
83 else {
84 $output = $source;
85 }
86
87 print STDERR "\n", $self->name, ":\n", $output, "\n";
88}
89
12f7b801 90sub _add_line_directive {
91 my ( $self, %args ) = @_;
92
93 my ( $line, $file );
94
95 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
96 $line = $ctx->{line};
97 if ( my $desc = $ctx->{description} ) {
98 $file = "$desc defined at $ctx->{file}";
99 } else {
100 $file = $ctx->{file};
101 }
102 } else {
103 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
104 }
105
106 my $code = $args{code};
107
108 # if it's an array of lines, join it up
109 # don't use newlines so that the definition context is more meaningful
110 $code = join(@$code, ' ') if ref $code;
111
112 return qq{#line $line "$file"\n} . $code;
113}
114
115sub _compile_code {
116 my ( $self, %args ) = @_;
117
118 my $code = $self->_add_line_directive(%args);
119
089535f2 120 return $self->_eval_closure($args{environment}, $code);
12f7b801 121}
122
565f0cbb 1231;
124
125__END__
126
127=pod
128
129=head1 NAME
130
131Class::MOP::Method::Generated - Abstract base class for generated methods
132
133=head1 DESCRIPTION
134
653556ae 135This is a C<Class::MOP::Method> subclass which is subclassed by
136C<Class::MOP::Method::Accessor> and
137C<Class::MOP::Method::Constructor>.
565f0cbb 138
653556ae 139It is not intended to be used directly.
565f0cbb 140
141=head1 AUTHORS
142
143Stevan Little E<lt>stevan@iinteractive.comE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
3e2c8600 147Copyright 2006-2010 by Infinity Interactive, Inc.
565f0cbb 148
149L<http://www.iinteractive.com>
150
151This library is free software; you can redistribute it and/or modify
152it under the same terms as Perl itself.
153
154=cut
155