19505d4b441e81dbae5293ce9edf9cfff488739b
[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   = '0.82_02';
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     my $__captures = $_[1];
32     eval join(
33         "\n",
34         (
35             map {
36                 /^([\@\%\$])/
37                     or die "capture key should start with \@, \% or \$: $_";
38                 q[my ]
39                 . $_ . q[ = ]
40                 . $1
41                 . q[{$__captures->{']
42                 . $_
43                 . q['}};];
44             } keys %$__captures
45         ),
46         $_[2]
47     );
48 }
49
50 sub _add_line_directive {
51     my ( $self, %args ) = @_;
52
53     my ( $line, $file );
54
55     if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
56         $line = $ctx->{line};
57         if ( my $desc = $ctx->{description} ) {
58             $file = "$desc defined at $ctx->{file}";
59         } else {
60             $file = $ctx->{file};
61         }
62     } else {
63         ( $line, $file ) = ( 0, "generated method (unknown origin)" );
64     }
65
66     my $code = $args{code};
67
68     # if it's an array of lines, join it up
69     # don't use newlines so that the definition context is more meaningful
70     $code = join(@$code, ' ') if ref $code;
71
72     return qq{#line $line "$file"\n} . $code;
73 }
74
75 sub _compile_code {
76     my ( $self, %args ) = @_;
77
78     my $code = $self->_add_line_directive(%args);
79
80     $self->_eval_closure($args{environment}, $code);
81 }
82
83 1;
84
85 __END__
86
87 =pod
88
89 =head1 NAME 
90
91 Class::MOP::Method::Generated - Abstract base class for generated methods
92
93 =head1 DESCRIPTION
94
95 This is a C<Class::MOP::Method> subclass which is subclassed by
96 C<Class::MOP::Method::Accessor> and
97 C<Class::MOP::Method::Constructor>.
98
99 It is not intended to be used directly.
100
101 =head1 AUTHORS
102
103 Stevan Little E<lt>stevan@iinteractive.comE<gt>
104
105 =head1 COPYRIGHT AND LICENSE
106
107 Copyright 2006-2009 by Infinity Interactive, Inc.
108
109 L<http://www.iinteractive.com>
110
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself. 
113
114 =cut
115