9aebcbccc63150b155a85ab0317b2ae583cdf431
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Generated.pm
1
2 package Class::MOP::Method::Generated;
3
4 use strict;
5 use warnings;
6
7 use Carp 'confess';
8
9 our $VERSION   = '1.08';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method';
14
15 ## accessors
16
17 sub new {
18     confess __PACKAGE__ . " is an abstract base class, you must provide a constructor.";
19 }
20
21 sub is_inline { $_[0]{is_inline} }
22
23 sub definition_context { $_[0]{definition_context} }
24
25 sub _initialize_body {
26     confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
27 }
28
29 sub _eval_closure {
30     my ($self, $__captures, $sub_body) = @_;
31
32     my $code;
33
34     my $e = do {
35         local $@;
36         local $SIG{__DIE__};
37         my $source = join
38             "\n", (
39             map {
40                 /^([\@\%\$])/
41                     or die "capture key should start with \@, \% or \$: $_";
42                 q[my ] 
43                     . $_ . q[ = ] 
44                     . $1
45                     . q[{$__captures->{']
46                     . $_ . q['}};];
47                 } keys %$__captures
48             ),
49             $sub_body;
50
51         $self->_dump_source($source) if $ENV{MOP_PRINT_SOURCE};
52
53         $code = eval $source;
54         $@;
55     };
56
57     return ( $code, $e );
58 }
59
60 sub _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
90 sub _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
115 sub _compile_code {
116     my ( $self, %args ) = @_;
117
118     my $code = $self->_add_line_directive(%args);
119
120     return $self->_eval_closure($args{environment}, $code);
121 }
122
123 1;
124
125 __END__
126
127 =pod
128
129 =head1 NAME 
130
131 Class::MOP::Method::Generated - Abstract base class for generated methods
132
133 =head1 DESCRIPTION
134
135 This is a C<Class::MOP::Method> subclass which is subclassed by
136 C<Class::MOP::Method::Accessor> and
137 C<Class::MOP::Method::Constructor>.
138
139 It is not intended to be used directly.
140
141 =head1 AUTHORS
142
143 Stevan Little E<lt>stevan@iinteractive.comE<gt>
144
145 =head1 COPYRIGHT AND LICENSE
146
147 Copyright 2006-2010 by Infinity Interactive, Inc.
148
149 L<http://www.iinteractive.com>
150
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself. 
153
154 =cut
155