2 package Class::MOP::Attribute;
8 use Scalar::Util 'blessed', 'reftype', 'weaken';
10 our $VERSION = '0.11';
11 our $AUTHORITY = 'cpan:STEVAN';
14 require Class::MOP::Class;
15 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
18 # NOTE: (meta-circularity)
19 # This method will be replaced in the
20 # boostrap section of Class::MOP, by
21 # a new version which uses the
22 # &Class::MOP::Class::construct_instance
23 # method to build an attribute meta-object
24 # which itself is described with attribute
26 # - Ain't meta-circularity grand? :)
32 (defined $name && $name)
33 || confess "You must provide a name for the attribute";
34 $options{init_arg} = $name
35 if not exists $options{init_arg};
39 accessor => $options{accessor},
40 reader => $options{reader},
41 writer => $options{writer},
42 predicate => $options{predicate},
43 clearer => $options{clearer},
44 init_arg => $options{init_arg},
45 default => $options{default},
46 # keep a weakened link to the
47 # class we are associated with
48 associated_class => undef,
53 # this is a primative (and kludgy) clone operation
54 # for now, it will be replaced in the Class::MOP
55 # bootstrap with a proper one, however we know
56 # that this one will work fine for now.
61 || confess "Can only clone an instance";
62 return bless { %{$self}, %options } => blessed($self);
65 sub initialize_instance_slot {
66 my ($self, $meta_instance, $instance, $params) = @_;
67 my $init_arg = $self->{init_arg};
68 # try to fetch the init arg from the %params ...
70 $val = $params->{$init_arg} if exists $params->{$init_arg};
71 # if nothing was in the %params, we can use the
72 # attribute's default value (if it has one)
73 if (!defined $val && defined $self->{default}) {
74 $val = $self->default($instance);
76 $meta_instance->set_slot_value($instance, $self->name, $val);
80 # the next bunch of methods will get bootstrapped
81 # away in the Class::MOP bootstrapping section
83 sub name { $_[0]->{name} }
85 sub associated_class { $_[0]->{associated_class} }
87 sub has_accessor { defined($_[0]->{accessor}) ? 1 : 0 }
88 sub has_reader { defined($_[0]->{reader}) ? 1 : 0 }
89 sub has_writer { defined($_[0]->{writer}) ? 1 : 0 }
90 sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
91 sub has_clearer { defined($_[0]->{clearer}) ? 1 : 0 }
92 sub has_init_arg { defined($_[0]->{init_arg}) ? 1 : 0 }
93 sub has_default { defined($_[0]->{default}) ? 1 : 0 }
95 sub accessor { $_[0]->{accessor} }
96 sub reader { $_[0]->{reader} }
97 sub writer { $_[0]->{writer} }
98 sub predicate { $_[0]->{predicate} }
99 sub clearer { $_[0]->{clearer} }
100 sub init_arg { $_[0]->{init_arg} }
102 # end bootstrapped away method section.
103 # (all methods below here are kept intact)
105 sub is_default_a_coderef {
106 (reftype($_[0]->{default}) && reftype($_[0]->{default}) eq 'CODE')
110 my ($self, $instance) = @_;
111 if ($instance && $self->is_default_a_coderef) {
112 # if the default is a CODE ref, then
113 # we pass in the instance and default
114 # can return a value based on that
115 # instance. Somewhat crude, but works.
116 return $self->{default}->($instance);
123 sub slots { (shift)->name }
127 sub attach_to_class {
128 my ($self, $class) = @_;
129 (blessed($class) && $class->isa('Class::MOP::Class'))
130 || confess "You must pass a Class::MOP::Class instance (or a subclass)";
131 weaken($self->{associated_class} = $class);
134 sub detach_from_class {
136 $self->{associated_class} = undef;
142 my ( $self, $instance, $value ) = @_;
144 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
146 ->set_slot_value( $instance, $self->name, $value );
150 my ( $self, $instance ) = @_;
152 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
154 ->get_slot_value( $instance, $self->name );
157 ## Method generation helpers
159 sub generate_accessor_method {
162 $attr->set_value( $_[0], $_[1] ) if scalar(@_) == 2;
163 $attr->get_value( $_[0] );
167 sub generate_accessor_method_inline {
169 my $attr_name = $self->name;
170 my $meta_instance = $self->associated_class->instance_metaclass;
172 my $code = eval 'sub {'
173 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
174 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
176 confess "Could not generate inline accessor because : $@" if $@;
181 sub generate_reader_method {
184 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
185 $attr->get_value( $_[0] );
189 sub generate_reader_method_inline {
191 my $attr_name = $self->name;
192 my $meta_instance = $self->associated_class->instance_metaclass;
194 my $code = eval 'sub {'
195 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
196 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
198 confess "Could not generate inline accessor because : $@" if $@;
203 sub generate_writer_method {
206 $attr->set_value( $_[0], $_[1] );
210 sub generate_writer_method_inline {
212 my $attr_name = $self->name;
213 my $meta_instance = $self->associated_class->instance_metaclass;
215 my $code = eval 'sub {'
216 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
218 confess "Could not generate inline accessor because : $@" if $@;
223 sub generate_predicate_method {
225 my $attr_name = $self->name;
227 defined Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
229 ->get_slot_value($_[0], $attr_name) ? 1 : 0;
233 sub generate_clearer_method {
235 my $attr_name = $self->name;
237 Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
239 ->deinitialize_slot($_[0], $attr_name);
243 sub generate_predicate_method_inline {
245 my $attr_name = $self->name;
246 my $meta_instance = $self->associated_class->instance_metaclass;
248 my $code = eval 'sub {'
249 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
251 confess "Could not generate inline predicate because : $@" if $@;
256 sub generate_clearer_method_inline {
258 my $attr_name = $self->name;
259 my $meta_instance = $self->associated_class->instance_metaclass;
261 my $code = eval 'sub {'
262 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
264 confess "Could not generate inline clearer because : $@" if $@;
269 sub process_accessors {
270 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
271 if (reftype($accessor)) {
272 (reftype($accessor) eq 'HASH')
273 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
274 my ($name, $method) = %{$accessor};
275 return ($name, Class::MOP::Attribute::Accessor->wrap($method));
278 my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
279 my $generator = $self->can('generate_' . $type . '_method' . ($inline_me ? '_inline' : ''));
281 || confess "There is no method generator for the type='$type'";
282 if (my $method = $self->$generator($self->name)) {
283 return ($accessor => Class::MOP::Attribute::Accessor->wrap($method));
285 confess "Could not create the '$type' method for " . $self->name . " because : $@";
289 sub install_accessors {
292 my $class = $self->associated_class;
295 $self->process_accessors('accessor' => $self->accessor(), $inline)
296 ) if $self->has_accessor();
299 $self->process_accessors('reader' => $self->reader(), $inline)
300 ) if $self->has_reader();
303 $self->process_accessors('writer' => $self->writer(), $inline)
304 ) if $self->has_writer();
307 $self->process_accessors('predicate' => $self->predicate(), $inline)
308 ) if $self->has_predicate();
311 $self->process_accessors('clearer' => $self->clearer(), $inline)
312 ) if $self->has_clearer();
318 my $_remove_accessor = sub {
319 my ($accessor, $class) = @_;
320 if (reftype($accessor) && reftype($accessor) eq 'HASH') {
321 ($accessor) = keys %{$accessor};
323 my $method = $class->get_method($accessor);
324 $class->remove_method($accessor)
325 if (blessed($method) && $method->isa('Class::MOP::Attribute::Accessor'));
328 sub remove_accessors {
330 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
331 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
332 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
333 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
334 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
340 package Class::MOP::Attribute::Accessor;
345 use Class::MOP::Method;
347 our $VERSION = '0.01';
349 our @ISA = ('Class::MOP::Method');
359 Class::MOP::Attribute - Attribute Meta Object
363 Class::MOP::Attribute->new('$foo' => (
364 accessor => 'foo', # dual purpose get/set accessor
365 predicate => 'has_foo' # predicate check for defined-ness
366 init_arg => '-foo', # class->new will look for a -foo key
367 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
370 Class::MOP::Attribute->new('$.bar' => (
371 reader => 'bar', # getter
372 writer => 'set_bar', # setter
373 predicate => 'has_bar' # predicate check for defined-ness
374 init_arg => ':bar', # class->new will look for a :bar key
375 # no default value means it is undef
380 The Attribute Protocol is almost entirely an invention of this module,
381 and is completely optional to this MOP. This is because Perl 5 does not
382 have consistent notion of what is an attribute of a class. There are
383 so many ways in which this is done, and very few (if any) are
384 easily discoverable by this module.
386 So, all that said, this module attempts to inject some order into this
387 chaos, by introducing a consistent API which can be used to create
396 =item B<new ($name, ?%options)>
398 An attribute must (at the very least), have a C<$name>. All other
399 C<%options> are contained added as key-value pairs. Acceptable keys
406 This should be a string value representing the expected key in
407 an initialization hash. For instance, if we have an I<init_arg>
408 value of C<-foo>, then the following code will Just Work.
410 MyClass->meta->construct_instance(-foo => "Hello There");
412 In an init_arg is not assigned, it will automatically use the
417 The value of this key is the default value which
418 C<Class::MOP::Class::construct_instance> will initialize the
422 If the value is a simple scalar (string or number), then it can
423 be just passed as is. However, if you wish to initialize it with
424 a HASH or ARRAY ref, then you need to wrap that inside a CODE
427 Class::MOP::Attribute->new('@foo' => (
428 default => sub { [] },
433 Class::MOP::Attribute->new('%foo' => (
434 default => sub { {} },
437 If you wish to initialize an attribute with a CODE reference
438 itself, then you need to wrap that in a subroutine as well, like
441 Class::MOP::Attribute->new('&foo' => (
442 default => sub { sub { print "Hello World" } },
445 And lastly, if the value of your attribute is dependent upon
446 some other aspect of the instance structure, then you can take
447 advantage of the fact that when the I<default> value is a CODE
448 reference, it is passed the raw (unblessed) instance structure
449 as it's only argument. So you can do things like this:
451 Class::MOP::Attribute->new('$object_identity' => (
452 default => sub { Scalar::Util::refaddr($_[0]) },
455 This last feature is fairly limited as there is no gurantee of
456 the order of attribute initializations, so you cannot perform
457 any kind of dependent initializations. However, if this is
458 something you need, you could subclass B<Class::MOP::Class> and
459 this class to acheive it. However, this is currently left as
460 an exercise to the reader :).
464 The I<accessor>, I<reader>, I<writer>, I<predicate> and I<clearer> keys can
465 contain either; the name of the method and an appropriate default one will be
466 generated for you, B<or> a HASH ref containing exactly one key (which will be
467 used as the name of the method) and one value, which should contain a CODE
468 reference which will be installed as the method itself.
474 The I<accessor> is a standard perl-style read/write accessor. It will
475 return the value of the attribute, and if a value is passed as an argument,
476 it will assign that value to the attribute.
479 This method will properly handle the following code, by assigning an
480 C<undef> value to the attribute.
482 $object->set_something(undef);
486 This is a basic read-only accessor, it will just return the value of
491 This is a basic write accessor, it accepts a single argument, and
492 assigns that value to the attribute. This method does not intentially
493 return a value, however perl will return the result of the last
494 expression in the subroutine, which returns in this returning the
495 same value that it was passed.
498 This method will properly handle the following code, by assigning an
499 C<undef> value to the attribute.
501 $object->set_something();
505 This is a basic test to see if the value of the attribute is not
506 C<undef>. It will return true (C<1>) if the attribute's value is
507 defined, and false (C<0>) otherwise.
511 This is the a method that will uninitialize the attr, reverting lazy values
512 back to their "unfulfilled" state.
516 =item B<clone (%options)>
518 =item B<initialize_instance_slot ($instance, $params)>
522 =head2 Value management
526 =item set_value $instance, $value
528 Set the value without going through the accessor. Note that this may be done to
529 even attributes with just read only accessors.
531 =item get_value $instance
533 Return the value without going through the accessor. Note that this may be done
534 even to attributes with just write only accessors.
540 These are all basic read-only value accessors for the values
541 passed into C<new>. I think they are pretty much self-explanitory.
559 =item B<is_default_a_coderef>
561 =item B<default (?$instance)>
563 As noted in the documentation for C<new> above, if the I<default>
564 value is a CODE reference, this accessor will pass a single additional
565 argument C<$instance> into it and return the value.
569 Returns a list of slots required by the attribute. This is usually
570 just one, which is the name of the attribute.
574 =head2 Informational predicates
576 These are all basic predicate methods for the values passed into C<new>.
580 =item B<has_accessor>
586 =item B<has_predicate>
590 =item B<has_init_arg>
596 =head2 Class association
600 =item B<associated_class>
602 =item B<attach_to_class ($class)>
604 =item B<detach_from_class>
608 =item B<allocate_slots>
610 =item B<deallocate_slots>
614 =head2 Attribute Accessor generation
618 =item B<install_accessors>
620 This allows the attribute to generate and install code for it's own
621 I<accessor/reader/writer/predicate> methods. This is called by
622 C<Class::MOP::Class::add_attribute>.
624 This method will call C<process_accessors> for each of the possible
625 method types (accessor, reader, writer & predicate).
627 =item B<process_accessors ($type, $value)>
629 This takes a C<$type> (accessor, reader, writer or predicate), and
630 a C<$value> (the value passed into the constructor for each of the
631 different types). It will then either generate the method itself
632 (using the C<generate_*_method> methods listed below) or it will
633 use the custom method passed through the constructor.
637 =item B<generate_accessor_method>
639 =item B<generate_predicate_method>
641 =item B<generate_clearer_method>
643 =item B<generate_reader_method>
645 =item B<generate_writer_method>
651 =item B<generate_accessor_method_inline>
653 =item B<generate_predicate_method_inline>
655 =item B<generate_clearer_method_inline>
657 =item B<generate_reader_method_inline>
659 =item B<generate_writer_method_inline>
663 =item B<remove_accessors>
665 This allows the attribute to remove the method for it's own
666 I<accessor/reader/writer/predicate/clearer>. This is called by
667 C<Class::MOP::Class::remove_attribute>.
677 This will return a B<Class::MOP::Class> instance which is related
680 It should also be noted that B<Class::MOP> will actually bootstrap
681 this module by installing a number of attribute meta-objects into
682 it's metaclass. This will allow this class to reap all the benifits
683 of the MOP when subclassing it.
689 Stevan Little E<lt>stevan@iinteractive.comE<gt>
691 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
693 =head1 COPYRIGHT AND LICENSE
695 Copyright 2006 by Infinity Interactive, Inc.
697 L<http://www.iinteractive.com>
699 This library is free software; you can redistribute it and/or modify
700 it under the same terms as Perl itself.