2 package Class::MOP::Attribute;
8 use Scalar::Util 'blessed', 'reftype', 'weaken';
10 our $VERSION = '0.12';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Object';
16 require Class::MOP::Class;
17 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
20 # NOTE: (meta-circularity)
21 # This method will be replaced in the
22 # boostrap section of Class::MOP, by
23 # a new version which uses the
24 # &Class::MOP::Class::construct_instance
25 # method to build an attribute meta-object
26 # which itself is described with attribute
28 # - Ain't meta-circularity grand? :)
34 (defined $name && $name)
35 || confess "You must provide a name for the attribute";
36 $options{init_arg} = $name
37 if not exists $options{init_arg};
39 (is_default_a_coderef(\%options))
40 || confess("References are not allowed as default values, you must ".
41 "wrap then in a CODE reference (ex: sub { [] } and not [])")
42 if exists $options{default} && ref $options{default};
46 accessor => $options{accessor},
47 reader => $options{reader},
48 writer => $options{writer},
49 predicate => $options{predicate},
50 clearer => $options{clearer},
51 init_arg => $options{init_arg},
52 default => $options{default},
53 # keep a weakened link to the
54 # class we are associated with
55 associated_class => undef,
60 # this is a primative (and kludgy) clone operation
61 # for now, it will be replaced in the Class::MOP
62 # bootstrap with a proper one, however we know
63 # that this one will work fine for now.
68 || confess "Can only clone an instance";
69 return bless { %{$self}, %options } => blessed($self);
72 sub initialize_instance_slot {
73 my ($self, $meta_instance, $instance, $params) = @_;
74 my $init_arg = $self->{init_arg};
75 # try to fetch the init arg from the %params ...
77 $val = $params->{$init_arg} if exists $params->{$init_arg};
78 # if nothing was in the %params, we can use the
79 # attribute's default value (if it has one)
80 if (!defined $val && defined $self->{default}) {
81 $val = $self->default($instance);
83 $meta_instance->set_slot_value($instance, $self->name, $val);
87 # the next bunch of methods will get bootstrapped
88 # away in the Class::MOP bootstrapping section
90 sub name { $_[0]->{name} }
92 sub associated_class { $_[0]->{associated_class} }
94 sub has_accessor { defined($_[0]->{accessor}) ? 1 : 0 }
95 sub has_reader { defined($_[0]->{reader}) ? 1 : 0 }
96 sub has_writer { defined($_[0]->{writer}) ? 1 : 0 }
97 sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
98 sub has_clearer { defined($_[0]->{clearer}) ? 1 : 0 }
99 sub has_init_arg { defined($_[0]->{init_arg}) ? 1 : 0 }
100 sub has_default { defined($_[0]->{default}) ? 1 : 0 }
102 sub accessor { $_[0]->{accessor} }
103 sub reader { $_[0]->{reader} }
104 sub writer { $_[0]->{writer} }
105 sub predicate { $_[0]->{predicate} }
106 sub clearer { $_[0]->{clearer} }
107 sub init_arg { $_[0]->{init_arg} }
109 # end bootstrapped away method section.
110 # (all methods below here are kept intact)
112 sub is_default_a_coderef {
113 ('CODE' eq (reftype($_[0]->{default}) || ''))
117 my ($self, $instance) = @_;
118 if ($instance && $self->is_default_a_coderef) {
119 # if the default is a CODE ref, then
120 # we pass in the instance and default
121 # can return a value based on that
122 # instance. Somewhat crude, but works.
123 return $self->{default}->($instance);
130 sub slots { (shift)->name }
134 sub attach_to_class {
135 my ($self, $class) = @_;
136 (blessed($class) && $class->isa('Class::MOP::Class'))
137 || confess "You must pass a Class::MOP::Class instance (or a subclass)";
138 weaken($self->{associated_class} = $class);
141 sub detach_from_class {
143 $self->{associated_class} = undef;
149 my ($self, $instance, $value) = @_;
151 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
153 ->set_slot_value( $instance, $self->name, $value );
157 my ($self, $instance) = @_;
159 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
161 ->get_slot_value($instance, $self->name);
164 ## Method generation helpers
166 sub generate_accessor_method {
169 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
170 $attr->get_value($_[0]);
174 sub generate_accessor_method_inline {
176 my $attr_name = $self->name;
177 my $meta_instance = $self->associated_class->instance_metaclass;
179 my $code = eval 'sub {'
180 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
181 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
183 confess "Could not generate inline accessor because : $@" if $@;
188 sub generate_reader_method {
191 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
192 $attr->get_value($_[0]);
196 sub generate_reader_method_inline {
198 my $attr_name = $self->name;
199 my $meta_instance = $self->associated_class->instance_metaclass;
201 my $code = eval 'sub {'
202 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
203 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
205 confess "Could not generate inline accessor because : $@" if $@;
210 sub generate_writer_method {
213 $attr->set_value($_[0], $_[1]);
217 sub generate_writer_method_inline {
219 my $attr_name = $self->name;
220 my $meta_instance = $self->associated_class->instance_metaclass;
222 my $code = eval 'sub {'
223 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
225 confess "Could not generate inline accessor because : $@" if $@;
230 sub generate_predicate_method {
232 my $attr_name = $self->name;
234 defined Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
236 ->get_slot_value($_[0], $attr_name) ? 1 : 0;
240 sub generate_clearer_method {
242 my $attr_name = $self->name;
244 Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
246 ->deinitialize_slot($_[0], $attr_name);
250 sub generate_predicate_method_inline {
252 my $attr_name = $self->name;
253 my $meta_instance = $self->associated_class->instance_metaclass;
255 my $code = eval 'sub {'
256 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
258 confess "Could not generate inline predicate because : $@" if $@;
263 sub generate_clearer_method_inline {
265 my $attr_name = $self->name;
266 my $meta_instance = $self->associated_class->instance_metaclass;
268 my $code = eval 'sub {'
269 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
271 confess "Could not generate inline clearer because : $@" if $@;
276 sub process_accessors {
277 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
278 if (reftype($accessor)) {
279 (reftype($accessor) eq 'HASH')
280 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
281 my ($name, $method) = %{$accessor};
282 return ($name, Class::MOP::Attribute::Accessor->wrap($method));
285 my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
286 my $generator = $self->can('generate_' . $type . '_method' . ($inline_me ? '_inline' : ''));
288 || confess "There is no method generator for the type='$type'";
289 if (my $method = $self->$generator($self->name)) {
290 return ($accessor => Class::MOP::Attribute::Accessor->wrap($method));
292 confess "Could not create the '$type' method for " . $self->name . " because : $@";
296 sub install_accessors {
299 my $class = $self->associated_class;
302 $self->process_accessors('accessor' => $self->accessor(), $inline)
303 ) if $self->has_accessor();
306 $self->process_accessors('reader' => $self->reader(), $inline)
307 ) if $self->has_reader();
310 $self->process_accessors('writer' => $self->writer(), $inline)
311 ) if $self->has_writer();
314 $self->process_accessors('predicate' => $self->predicate(), $inline)
315 ) if $self->has_predicate();
318 $self->process_accessors('clearer' => $self->clearer(), $inline)
319 ) if $self->has_clearer();
325 my $_remove_accessor = sub {
326 my ($accessor, $class) = @_;
327 if (reftype($accessor) && reftype($accessor) eq 'HASH') {
328 ($accessor) = keys %{$accessor};
330 my $method = $class->get_method($accessor);
331 $class->remove_method($accessor)
332 if (blessed($method) && $method->isa('Class::MOP::Attribute::Accessor'));
335 sub remove_accessors {
337 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
338 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
339 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
340 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
341 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
347 package Class::MOP::Attribute::Accessor;
352 use Class::MOP::Method;
354 our $VERSION = '0.02';
355 our $AUTHORITY = 'cpan:STEVAN';
357 use base 'Class::MOP::Method';
367 Class::MOP::Attribute - Attribute Meta Object
371 Class::MOP::Attribute->new('$foo' => (
372 accessor => 'foo', # dual purpose get/set accessor
373 predicate => 'has_foo' # predicate check for defined-ness
374 init_arg => '-foo', # class->new will look for a -foo key
375 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
378 Class::MOP::Attribute->new('$.bar' => (
379 reader => 'bar', # getter
380 writer => 'set_bar', # setter
381 predicate => 'has_bar' # predicate check for defined-ness
382 init_arg => ':bar', # class->new will look for a :bar key
383 # no default value means it is undef
388 The Attribute Protocol is almost entirely an invention of this module,
389 and is completely optional to this MOP. This is because Perl 5 does not
390 have consistent notion of what is an attribute of a class. There are
391 so many ways in which this is done, and very few (if any) are
392 easily discoverable by this module.
394 So, all that said, this module attempts to inject some order into this
395 chaos, by introducing a consistent API which can be used to create
404 =item B<new ($name, ?%options)>
406 An attribute must (at the very least), have a C<$name>. All other
407 C<%options> are contained added as key-value pairs. Acceptable keys
414 This should be a string value representing the expected key in
415 an initialization hash. For instance, if we have an I<init_arg>
416 value of C<-foo>, then the following code will Just Work.
418 MyClass->meta->construct_instance(-foo => "Hello There");
420 In an init_arg is not assigned, it will automatically use the
425 The value of this key is the default value which
426 C<Class::MOP::Class::construct_instance> will initialize the
430 If the value is a simple scalar (string or number), then it can
431 be just passed as is. However, if you wish to initialize it with
432 a HASH or ARRAY ref, then you need to wrap that inside a CODE
435 Class::MOP::Attribute->new('@foo' => (
436 default => sub { [] },
441 Class::MOP::Attribute->new('%foo' => (
442 default => sub { {} },
445 If you wish to initialize an attribute with a CODE reference
446 itself, then you need to wrap that in a subroutine as well, like
449 Class::MOP::Attribute->new('&foo' => (
450 default => sub { sub { print "Hello World" } },
453 And lastly, if the value of your attribute is dependent upon
454 some other aspect of the instance structure, then you can take
455 advantage of the fact that when the I<default> value is a CODE
456 reference, it is passed the raw (unblessed) instance structure
457 as it's only argument. So you can do things like this:
459 Class::MOP::Attribute->new('$object_identity' => (
460 default => sub { Scalar::Util::refaddr($_[0]) },
463 This last feature is fairly limited as there is no gurantee of
464 the order of attribute initializations, so you cannot perform
465 any kind of dependent initializations. However, if this is
466 something you need, you could subclass B<Class::MOP::Class> and
467 this class to acheive it. However, this is currently left as
468 an exercise to the reader :).
472 The I<accessor>, I<reader>, I<writer>, I<predicate> and I<clearer> keys can
473 contain either; the name of the method and an appropriate default one will be
474 generated for you, B<or> a HASH ref containing exactly one key (which will be
475 used as the name of the method) and one value, which should contain a CODE
476 reference which will be installed as the method itself.
482 The I<accessor> is a standard perl-style read/write accessor. It will
483 return the value of the attribute, and if a value is passed as an argument,
484 it will assign that value to the attribute.
487 This method will properly handle the following code, by assigning an
488 C<undef> value to the attribute.
490 $object->set_something(undef);
494 This is a basic read-only accessor, it will just return the value of
499 This is a basic write accessor, it accepts a single argument, and
500 assigns that value to the attribute. This method does not intentially
501 return a value, however perl will return the result of the last
502 expression in the subroutine, which returns in this returning the
503 same value that it was passed.
506 This method will properly handle the following code, by assigning an
507 C<undef> value to the attribute.
509 $object->set_something();
513 This is a basic test to see if the value of the attribute is not
514 C<undef>. It will return true (C<1>) if the attribute's value is
515 defined, and false (C<0>) otherwise.
519 This is the a method that will uninitialize the attr, reverting lazy values
520 back to their "unfulfilled" state.
524 =item B<clone (%options)>
526 =item B<initialize_instance_slot ($instance, $params)>
530 =head2 Value management
534 =item set_value $instance, $value
536 Set the value without going through the accessor. Note that this may be done to
537 even attributes with just read only accessors.
539 =item get_value $instance
541 Return the value without going through the accessor. Note that this may be done
542 even to attributes with just write only accessors.
548 These are all basic read-only value accessors for the values
549 passed into C<new>. I think they are pretty much self-explanitory.
567 =item B<is_default_a_coderef>
569 =item B<default (?$instance)>
571 As noted in the documentation for C<new> above, if the I<default>
572 value is a CODE reference, this accessor will pass a single additional
573 argument C<$instance> into it and return the value.
577 Returns a list of slots required by the attribute. This is usually
578 just one, which is the name of the attribute.
582 =head2 Informational predicates
584 These are all basic predicate methods for the values passed into C<new>.
588 =item B<has_accessor>
594 =item B<has_predicate>
598 =item B<has_init_arg>
604 =head2 Class association
608 =item B<associated_class>
610 =item B<attach_to_class ($class)>
612 =item B<detach_from_class>
616 =item B<allocate_slots>
618 =item B<deallocate_slots>
622 =head2 Attribute Accessor generation
626 =item B<install_accessors>
628 This allows the attribute to generate and install code for it's own
629 I<accessor/reader/writer/predicate> methods. This is called by
630 C<Class::MOP::Class::add_attribute>.
632 This method will call C<process_accessors> for each of the possible
633 method types (accessor, reader, writer & predicate).
635 =item B<process_accessors ($type, $value)>
637 This takes a C<$type> (accessor, reader, writer or predicate), and
638 a C<$value> (the value passed into the constructor for each of the
639 different types). It will then either generate the method itself
640 (using the C<generate_*_method> methods listed below) or it will
641 use the custom method passed through the constructor.
645 =item B<generate_accessor_method>
647 =item B<generate_predicate_method>
649 =item B<generate_clearer_method>
651 =item B<generate_reader_method>
653 =item B<generate_writer_method>
659 =item B<generate_accessor_method_inline>
661 =item B<generate_predicate_method_inline>
663 =item B<generate_clearer_method_inline>
665 =item B<generate_reader_method_inline>
667 =item B<generate_writer_method_inline>
671 =item B<remove_accessors>
673 This allows the attribute to remove the method for it's own
674 I<accessor/reader/writer/predicate/clearer>. This is called by
675 C<Class::MOP::Class::remove_attribute>.
685 This will return a B<Class::MOP::Class> instance which is related
688 It should also be noted that B<Class::MOP> will actually bootstrap
689 this module by installing a number of attribute meta-objects into
690 it's metaclass. This will allow this class to reap all the benifits
691 of the MOP when subclassing it.
697 Stevan Little E<lt>stevan@iinteractive.comE<gt>
699 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
701 =head1 COPYRIGHT AND LICENSE
703 Copyright 2006 by Infinity Interactive, Inc.
705 L<http://www.iinteractive.com>
707 This library is free software; you can redistribute it and/or modify
708 it under the same terms as Perl itself.