2 package Class::MOP::Method::Reader;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.88';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Attribute';
16 sub associated_attribute { (shift)->{'attribute'} }
20 sub _initialize_body {
23 my $method_name = join "_" => (
26 ($self->is_inline ? 'inline' : ())
29 $self->{'body'} = $self->$method_name();
35 Carp::cluck('The generate_reader_method method has been made private.'
36 . " The public version is deprecated and will be removed in a future release.\n");
37 shift->_generate_method;
40 sub _generate_method {
41 my $attr = (shift)->associated_attribute;
43 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
44 $attr->get_value($_[0]);
50 sub generate_method_inline {
51 Carp::cluck('The generate_reader_method_inline method has been made private.'
52 . " The public version is deprecated and will be removed in a future release.\n");
53 shift->_generate_method_inline;
56 sub _generate_method_inline {
58 my $attr = $self->associated_attribute;
59 my $attr_name = $attr->name;
60 my $meta_instance = $attr->associated_class->instance_metaclass;
62 my ( $code, $e ) = $self->_eval_closure(
65 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
66 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
69 confess "Could not generate inline reader because : $e" if $e;
84 Class::MOP::Method::Reader - Method Meta Object for accessors
88 use Class::MOP::Method::Reader;
90 my $reader = Class::MOP::Method::Reader->new(
91 attribute => $attribute,
95 $reader->body->execute($instance); # call the reader method
99 This is a subclass of <Class::MOP::Method> which is used by
100 C<Class::MOP::Attribute> to generate accessor code. It handles
101 generation of readers, writers, predicates and clearers. For each type
102 of method, it can either create a subroutine reference, or actually
103 inline code by generating a string and C<eval>'ing it.
109 =item B<< Class::MOP::Method::Accessor->new(%options) >>
111 This returns a new C<Class::MOP::Method::Accessor> based on the
112 C<%options> provided.
118 This is the C<Class::MOP::Attribute> for which accessors are being
119 generated. This option is required.
121 =item * accessor_type
123 This is a string which should be one of "reader", "writer",
124 "accessor", "predicate", or "clearer". This is the type of method
125 being generated. This option is required.
129 This indicates whether or not the accessor should be inlined. This
134 The method name (without a package name). This is required.
138 The package name for the method. This is required.
142 =item B<< $metamethod->accessor_type >>
144 Returns the accessor type which was passed to C<new>.
146 =item B<< $metamethod->is_inline >>
148 Returns a boolean indicating whether or not the accessor is inlined.
150 =item B<< $metamethod->associated_attribute >>
152 This returns the L<Class::MOP::Attribute> object which was passed to
155 =item B<< $metamethod->body >>
157 The method itself is I<generated> when the accessor object is
164 Stevan Little E<lt>stevan@iinteractive.comE<gt>
166 =head1 COPYRIGHT AND LICENSE
168 Copyright 2006-2009 by Infinity Interactive, Inc.
170 L<http://www.iinteractive.com>
172 This library is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself.