convert the rest of the simple readers in cmop to xs
[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 _initialize_body {
22     confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
23 }
24
25 sub _eval_closure {
26     my ($self, $__captures, $sub_body) = @_;
27
28     my $code;
29
30     my $e = do {
31         local $@;
32         local $SIG{__DIE__};
33         my $source = join
34             "\n", (
35             map {
36                 /^([\@\%\$])/
37                     or die "capture key should start with \@, \% or \$: $_";
38                 q[my ] 
39                     . $_ . q[ = ] 
40                     . $1
41                     . q[{$__captures->{']
42                     . $_ . q['}};];
43                 } keys %$__captures
44             ),
45             $sub_body;
46
47         $self->_dump_source($source) if $ENV{MOP_PRINT_SOURCE};
48
49         $code = eval $source;
50         $@;
51     };
52
53     return ( $code, $e );
54 }
55
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         );
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
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
115     return $self->_eval_closure($args{environment}, $code);
116 }
117
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
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>.
133
134 It is not intended to be used directly.
135
136 =head1 AUTHORS
137
138 Stevan Little E<lt>stevan@iinteractive.comE<gt>
139
140 =head1 COPYRIGHT AND LICENSE
141
142 Copyright 2006-2010 by Infinity Interactive, Inc.
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