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";
37 $options{init_arg} = $name
38 if not exists $options{init_arg};
40 (is_default_a_coderef(\%options))
41 || confess("References are not allowed as default values, you must ".
42 "wrap then in a CODE reference (ex: sub { [] } and not [])")
43 if exists $options{default} && ref $options{default};
47 accessor => $options{accessor},
48 reader => $options{reader},
49 writer => $options{writer},
50 predicate => $options{predicate},
51 clearer => $options{clearer},
52 init_arg => $options{init_arg},
53 default => $options{default},
54 # keep a weakened link to the
55 # class we are associated with
56 associated_class => undef,
61 # this is a primative (and kludgy) clone operation
62 # for now, it will be replaced in the Class::MOP
63 # bootstrap with a proper one, however we know
64 # that this one will work fine for now.
69 || confess "Can only clone an instance";
70 return bless { %{$self}, %options } => blessed($self);
73 sub initialize_instance_slot {
74 my ($self, $meta_instance, $instance, $params) = @_;
75 my $init_arg = $self->{init_arg};
76 # try to fetch the init arg from the %params ...
78 $val = $params->{$init_arg} if exists $params->{$init_arg};
79 # if nothing was in the %params, we can use the
80 # attribute's default value (if it has one)
81 if (!defined $val && defined $self->{default}) {
82 $val = $self->default($instance);
84 $meta_instance->set_slot_value($instance, $self->name, $val);
88 # the next bunch of methods will get bootstrapped
89 # away in the Class::MOP bootstrapping section
91 sub name { $_[0]->{name} }
93 sub associated_class { $_[0]->{associated_class} }
95 sub has_accessor { defined($_[0]->{accessor}) ? 1 : 0 }
96 sub has_reader { defined($_[0]->{reader}) ? 1 : 0 }
97 sub has_writer { defined($_[0]->{writer}) ? 1 : 0 }
98 sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
99 sub has_clearer { defined($_[0]->{clearer}) ? 1 : 0 }
100 sub has_init_arg { defined($_[0]->{init_arg}) ? 1 : 0 }
101 sub has_default { defined($_[0]->{default}) ? 1 : 0 }
103 sub accessor { $_[0]->{accessor} }
104 sub reader { $_[0]->{reader} }
105 sub writer { $_[0]->{writer} }
106 sub predicate { $_[0]->{predicate} }
107 sub clearer { $_[0]->{clearer} }
108 sub init_arg { $_[0]->{init_arg} }
110 # end bootstrapped away method section.
111 # (all methods below here are kept intact)
113 sub is_default_a_coderef {
114 ('CODE' eq (reftype($_[0]->{default}) || ''))
118 my ($self, $instance) = @_;
119 if ($instance && $self->is_default_a_coderef) {
120 # if the default is a CODE ref, then
121 # we pass in the instance and default
122 # can return a value based on that
123 # instance. Somewhat crude, but works.
124 return $self->{default}->($instance);
131 sub slots { (shift)->name }
135 sub attach_to_class {
136 my ($self, $class) = @_;
137 (blessed($class) && $class->isa('Class::MOP::Class'))
138 || confess "You must pass a Class::MOP::Class instance (or a subclass)";
139 weaken($self->{associated_class} = $class);
142 sub detach_from_class {
144 $self->{associated_class} = undef;
150 my ($self, $instance, $value) = @_;
152 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
154 ->set_slot_value( $instance, $self->name, $value );
158 my ($self, $instance) = @_;
160 Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
162 ->get_slot_value($instance, $self->name);
165 ## Method generation helpers
167 sub generate_accessor_method {
170 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
171 $attr->get_value($_[0]);
175 sub generate_accessor_method_inline {
177 my $attr_name = $self->name;
178 my $meta_instance = $self->associated_class->instance_metaclass;
180 my $code = eval 'sub {'
181 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
182 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
184 confess "Could not generate inline accessor because : $@" if $@;
189 sub generate_reader_method {
192 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
193 $attr->get_value($_[0]);
197 sub generate_reader_method_inline {
199 my $attr_name = $self->name;
200 my $meta_instance = $self->associated_class->instance_metaclass;
202 my $code = eval 'sub {'
203 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
204 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
206 confess "Could not generate inline accessor because : $@" if $@;
211 sub generate_writer_method {
214 $attr->set_value($_[0], $_[1]);
218 sub generate_writer_method_inline {
220 my $attr_name = $self->name;
221 my $meta_instance = $self->associated_class->instance_metaclass;
223 my $code = eval 'sub {'
224 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
226 confess "Could not generate inline accessor because : $@" if $@;
231 sub generate_predicate_method {
233 my $attr_name = $self->name;
235 defined Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
237 ->get_slot_value($_[0], $attr_name) ? 1 : 0;
241 sub generate_clearer_method {
243 my $attr_name = $self->name;
245 Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
247 ->deinitialize_slot($_[0], $attr_name);
251 sub generate_predicate_method_inline {
253 my $attr_name = $self->name;
254 my $meta_instance = $self->associated_class->instance_metaclass;
256 my $code = eval 'sub {'
257 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
259 confess "Could not generate inline predicate because : $@" if $@;
264 sub generate_clearer_method_inline {
266 my $attr_name = $self->name;
267 my $meta_instance = $self->associated_class->instance_metaclass;
269 my $code = eval 'sub {'
270 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
272 confess "Could not generate inline clearer because : $@" if $@;
277 sub process_accessors {
278 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
279 if (reftype($accessor)) {
280 (reftype($accessor) eq 'HASH')
281 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
282 my ($name, $method) = %{$accessor};
283 return ($name, Class::MOP::Attribute::Accessor->wrap($method));
286 my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
287 my $generator = $self->can('generate_' . $type . '_method' . ($inline_me ? '_inline' : ''));
289 || confess "There is no method generator for the type='$type'";
290 if (my $method = $self->$generator($self->name)) {
291 return ($accessor => Class::MOP::Attribute::Accessor->wrap($method));
293 confess "Could not create the '$type' method for " . $self->name . " because : $@";
297 sub install_accessors {
300 my $class = $self->associated_class;
303 $self->process_accessors('accessor' => $self->accessor(), $inline)
304 ) if $self->has_accessor();
307 $self->process_accessors('reader' => $self->reader(), $inline)
308 ) if $self->has_reader();
311 $self->process_accessors('writer' => $self->writer(), $inline)
312 ) if $self->has_writer();
315 $self->process_accessors('predicate' => $self->predicate(), $inline)
316 ) if $self->has_predicate();
319 $self->process_accessors('clearer' => $self->clearer(), $inline)
320 ) if $self->has_clearer();
326 my $_remove_accessor = sub {
327 my ($accessor, $class) = @_;
328 if (reftype($accessor) && reftype($accessor) eq 'HASH') {
329 ($accessor) = keys %{$accessor};
331 my $method = $class->get_method($accessor);
332 $class->remove_method($accessor)
333 if (blessed($method) && $method->isa('Class::MOP::Attribute::Accessor'));
336 sub remove_accessors {
338 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
339 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
340 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
341 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
342 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
348 package Class::MOP::Attribute::Accessor;
353 use Class::MOP::Method;
355 our $VERSION = '0.02';
356 our $AUTHORITY = 'cpan:STEVAN';
358 use base 'Class::MOP::Method';
368 Class::MOP::Attribute - Attribute Meta Object
372 Class::MOP::Attribute->new('$foo' => (
373 accessor => 'foo', # dual purpose get/set accessor
374 predicate => 'has_foo' # predicate check for defined-ness
375 init_arg => '-foo', # class->new will look for a -foo key
376 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
379 Class::MOP::Attribute->new('$.bar' => (
380 reader => 'bar', # getter
381 writer => 'set_bar', # setter
382 predicate => 'has_bar' # predicate check for defined-ness
383 init_arg => ':bar', # class->new will look for a :bar key
384 # no default value means it is undef
389 The Attribute Protocol is almost entirely an invention of this module,
390 and is completely optional to this MOP. This is because Perl 5 does not
391 have consistent notion of what is an attribute of a class. There are
392 so many ways in which this is done, and very few (if any) are
393 easily discoverable by this module.
395 So, all that said, this module attempts to inject some order into this
396 chaos, by introducing a consistent API which can be used to create
405 =item B<new ($name, ?%options)>
407 An attribute must (at the very least), have a C<$name>. All other
408 C<%options> are contained added as key-value pairs. Acceptable keys
415 This should be a string value representing the expected key in
416 an initialization hash. For instance, if we have an I<init_arg>
417 value of C<-foo>, then the following code will Just Work.
419 MyClass->meta->construct_instance(-foo => "Hello There");
421 In an init_arg is not assigned, it will automatically use the
426 The value of this key is the default value which
427 C<Class::MOP::Class::construct_instance> will initialize the
431 If the value is a simple scalar (string or number), then it can
432 be just passed as is. However, if you wish to initialize it with
433 a HASH or ARRAY ref, then you need to wrap that inside a CODE
436 Class::MOP::Attribute->new('@foo' => (
437 default => sub { [] },
442 Class::MOP::Attribute->new('%foo' => (
443 default => sub { {} },
446 If you wish to initialize an attribute with a CODE reference
447 itself, then you need to wrap that in a subroutine as well, like
450 Class::MOP::Attribute->new('&foo' => (
451 default => sub { sub { print "Hello World" } },
454 And lastly, if the value of your attribute is dependent upon
455 some other aspect of the instance structure, then you can take
456 advantage of the fact that when the I<default> value is a CODE
457 reference, it is passed the raw (unblessed) instance structure
458 as it's only argument. So you can do things like this:
460 Class::MOP::Attribute->new('$object_identity' => (
461 default => sub { Scalar::Util::refaddr($_[0]) },
464 This last feature is fairly limited as there is no gurantee of
465 the order of attribute initializations, so you cannot perform
466 any kind of dependent initializations. However, if this is
467 something you need, you could subclass B<Class::MOP::Class> and
468 this class to acheive it. However, this is currently left as
469 an exercise to the reader :).
473 The I<accessor>, I<reader>, I<writer>, I<predicate> and I<clearer> keys can
474 contain either; the name of the method and an appropriate default one will be
475 generated for you, B<or> a HASH ref containing exactly one key (which will be
476 used as the name of the method) and one value, which should contain a CODE
477 reference which will be installed as the method itself.
483 The I<accessor> is a standard perl-style read/write accessor. It will
484 return the value of the attribute, and if a value is passed as an argument,
485 it will assign that value to the attribute.
488 This method will properly handle the following code, by assigning an
489 C<undef> value to the attribute.
491 $object->set_something(undef);
495 This is a basic read-only accessor, it will just return the value of
500 This is a basic write accessor, it accepts a single argument, and
501 assigns that value to the attribute. This method does not intentially
502 return a value, however perl will return the result of the last
503 expression in the subroutine, which returns in this returning the
504 same value that it was passed.
507 This method will properly handle the following code, by assigning an
508 C<undef> value to the attribute.
510 $object->set_something();
514 This is a basic test to see if the value of the attribute is not
515 C<undef>. It will return true (C<1>) if the attribute's value is
516 defined, and false (C<0>) otherwise.
520 This is the a method that will uninitialize the attr, reverting lazy values
521 back to their "unfulfilled" state.
525 =item B<clone (%options)>
527 =item B<initialize_instance_slot ($instance, $params)>
531 =head2 Value management
535 =item set_value $instance, $value
537 Set the value without going through the accessor. Note that this may be done to
538 even attributes with just read only accessors.
540 =item get_value $instance
542 Return the value without going through the accessor. Note that this may be done
543 even to attributes with just write only accessors.
549 These are all basic read-only value accessors for the values
550 passed into C<new>. I think they are pretty much self-explanitory.
568 =item B<is_default_a_coderef>
570 =item B<default (?$instance)>
572 As noted in the documentation for C<new> above, if the I<default>
573 value is a CODE reference, this accessor will pass a single additional
574 argument C<$instance> into it and return the value.
578 Returns a list of slots required by the attribute. This is usually
579 just one, which is the name of the attribute.
583 =head2 Informational predicates
585 These are all basic predicate methods for the values passed into C<new>.
589 =item B<has_accessor>
595 =item B<has_predicate>
599 =item B<has_init_arg>
605 =head2 Class association
609 =item B<associated_class>
611 =item B<attach_to_class ($class)>
613 =item B<detach_from_class>
617 =item B<allocate_slots>
619 =item B<deallocate_slots>
623 =head2 Attribute Accessor generation
627 =item B<install_accessors>
629 This allows the attribute to generate and install code for it's own
630 I<accessor/reader/writer/predicate> methods. This is called by
631 C<Class::MOP::Class::add_attribute>.
633 This method will call C<process_accessors> for each of the possible
634 method types (accessor, reader, writer & predicate).
636 =item B<process_accessors ($type, $value)>
638 This takes a C<$type> (accessor, reader, writer or predicate), and
639 a C<$value> (the value passed into the constructor for each of the
640 different types). It will then either generate the method itself
641 (using the C<generate_*_method> methods listed below) or it will
642 use the custom method passed through the constructor.
646 =item B<generate_accessor_method>
648 =item B<generate_predicate_method>
650 =item B<generate_clearer_method>
652 =item B<generate_reader_method>
654 =item B<generate_writer_method>
660 =item B<generate_accessor_method_inline>
662 =item B<generate_predicate_method_inline>
664 =item B<generate_clearer_method_inline>
666 =item B<generate_reader_method_inline>
668 =item B<generate_writer_method_inline>
672 =item B<remove_accessors>
674 This allows the attribute to remove the method for it's own
675 I<accessor/reader/writer/predicate/clearer>. This is called by
676 C<Class::MOP::Class::remove_attribute>.
686 This will return a B<Class::MOP::Class> instance which is related
689 It should also be noted that B<Class::MOP> will actually bootstrap
690 this module by installing a number of attribute meta-objects into
691 it's metaclass. This will allow this class to reap all the benifits
692 of the MOP when subclassing it.
698 Stevan Little E<lt>stevan@iinteractive.comE<gt>
700 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
702 =head1 COPYRIGHT AND LICENSE
704 Copyright 2006 by Infinity Interactive, Inc.
706 L<http://www.iinteractive.com>
708 This library is free software; you can redistribute it and/or modify
709 it under the same terms as Perl itself.