convert the rest of the simple readers in cmop to xs
[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
a9f48b4b 9our $VERSION = '1.11';
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
1fd40136 21sub _initialize_body {
565f0cbb 22 confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
23}
24
7f8de9b4 25sub _eval_closure {
d2d9edc0 26 my ($self, $__captures, $sub_body) = @_;
e24b19fb 27
28 my $code;
29
30 my $e = do {
31 local $@;
32 local $SIG{__DIE__};
315ed13b 33 my $source = join
e24b19fb 34 "\n", (
2507ef3a 35 map {
36 /^([\@\%\$])/
37 or die "capture key should start with \@, \% or \$: $_";
e24b19fb 38 q[my ]
39 . $_ . q[ = ]
40 . $1
41 . q[{$__captures->{']
42 . $_ . q['}};];
43 } keys %$__captures
44 ),
d2d9edc0 45 $sub_body;
46
47 $self->_dump_source($source) if $ENV{MOP_PRINT_SOURCE};
48
315ed13b 49 $code = eval $source;
e24b19fb 50 $@;
51 };
52
53 return ( $code, $e );
7f8de9b4 54}
565f0cbb 55
d2d9edc0 56sub _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 );
d2d9edc0 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
12f7b801 85sub _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
110sub _compile_code {
111 my ( $self, %args ) = @_;
112
113 my $code = $self->_add_line_directive(%args);
114
089535f2 115 return $self->_eval_closure($args{environment}, $code);
12f7b801 116}
117
565f0cbb 1181;
119
120__END__
121
122=pod
123
124=head1 NAME
125
126Class::MOP::Method::Generated - Abstract base class for generated methods
127
128=head1 DESCRIPTION
129
653556ae 130This is a C<Class::MOP::Method> subclass which is subclassed by
131C<Class::MOP::Method::Accessor> and
132C<Class::MOP::Method::Constructor>.
565f0cbb 133
653556ae 134It is not intended to be used directly.
565f0cbb 135
136=head1 AUTHORS
137
138Stevan Little E<lt>stevan@iinteractive.comE<gt>
139
140=head1 COPYRIGHT AND LICENSE
141
3e2c8600 142Copyright 2006-2010 by Infinity Interactive, Inc.
565f0cbb 143
144L<http://www.iinteractive.com>
145
146This library is free software; you can redistribute it and/or modify
147it under the same terms as Perl itself.
148
149=cut
150