d1b50d69466aa6af5c96db8d6bd1615ee25c92ab
[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.11';
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
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
89 sub _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
114 sub _compile_code {
115     my ( $self, %args ) = @_;
116
117     my $code = $self->_add_line_directive(%args);
118
119     return $self->_eval_closure($args{environment}, $code);
120 }
121
122 1;
123
124 __END__
125
126 =pod
127
128 =head1 NAME 
129
130 Class::MOP::Method::Generated - Abstract base class for generated methods
131
132 =head1 DESCRIPTION
133
134 This is a C<Class::MOP::Method> subclass which is subclassed by
135 C<Class::MOP::Method::Accessor> and
136 C<Class::MOP::Method::Constructor>.
137
138 It is not intended to be used directly.
139
140 =head1 AUTHORS
141
142 Stevan Little E<lt>stevan@iinteractive.comE<gt>
143
144 =head1 COPYRIGHT AND LICENSE
145
146 Copyright 2006-2010 by Infinity Interactive, Inc.
147
148 L<http://www.iinteractive.com>
149
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself. 
152
153 =cut
154