2 package Moose::Meta::Attribute;
7 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.62_02';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use Moose::Meta::Method::Accessor;
14 use Moose::Meta::Method::Delegation;
16 use Moose::Util::TypeConstraints ();
18 use base 'Class::MOP::Attribute';
20 # options which are not directly used
21 # but we store them for metadata purposes
22 __PACKAGE__->meta->add_attribute('isa' => (reader => '_isa_metadata'));
23 __PACKAGE__->meta->add_attribute('does' => (reader => '_does_metadata'));
24 __PACKAGE__->meta->add_attribute('is' => (reader => '_is_metadata'));
26 # these are actual options for the attrs
27 __PACKAGE__->meta->add_attribute('required' => (reader => 'is_required' ));
28 __PACKAGE__->meta->add_attribute('lazy' => (reader => 'is_lazy' ));
29 __PACKAGE__->meta->add_attribute('lazy_build' => (reader => 'is_lazy_build' ));
30 __PACKAGE__->meta->add_attribute('coerce' => (reader => 'should_coerce' ));
31 __PACKAGE__->meta->add_attribute('weak_ref' => (reader => 'is_weak_ref' ));
32 __PACKAGE__->meta->add_attribute('auto_deref' => (reader => 'should_auto_deref'));
33 __PACKAGE__->meta->add_attribute('type_constraint' => (
34 reader => 'type_constraint',
35 predicate => 'has_type_constraint',
37 __PACKAGE__->meta->add_attribute('trigger' => (
39 predicate => 'has_trigger',
41 __PACKAGE__->meta->add_attribute('handles' => (
43 predicate => 'has_handles',
45 __PACKAGE__->meta->add_attribute('documentation' => (
46 reader => 'documentation',
47 predicate => 'has_documentation',
49 __PACKAGE__->meta->add_attribute('traits' => (
50 reader => 'applied_traits',
51 predicate => 'has_applied_traits',
54 # we need to have a ->does method in here to
55 # more easily support traits, and the introspection
56 # of those traits. We extend the does check to look
57 # for metatrait aliases.
59 my ($self, $role_name) = @_;
61 Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
63 return 0 if !defined($name); # failed to load class
64 return $self->Moose::Object::does($name);
69 my $class = ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
70 unshift @_, "message" if @_ % 2 == 1;
71 unshift @_, attr => $self if ref $self;
73 my $handler = $class->can("throw_error"); # to avoid incrementing depth by 1
78 my ($class, $name, %options) = @_;
79 $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
80 return $class->SUPER::new($name, %options);
83 sub interpolate_class_and_new {
84 my ($class, $name, @args) = @_;
86 my ( $new_class, @traits ) = $class->interpolate_class(@args);
88 $new_class->new($name, @args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
91 sub interpolate_class {
92 my ($class, %options) = @_;
94 $class = ref($class) || $class;
96 if ( my $metaclass_name = delete $options{metaclass} ) {
97 my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
99 if ( $class ne $new_class ) {
100 if ( $new_class->can("interpolate_class") ) {
101 return $new_class->interpolate_class(%options);
110 if (my $traits = $options{traits}) {
111 if ( @traits = grep { not $class->does($_) } map {
112 Moose::Util::resolve_metatrait_alias( Attribute => $_ )
116 my $anon_class = Moose::Meta::Class->create_anon_class(
117 superclasses => [ $class ],
118 roles => [ @traits ],
122 $class = $anon_class->name;
126 return ( wantarray ? ( $class, @traits ) : $class );
131 my @legal_options_for_inheritance = qw(
132 default coerce required
133 documentation lazy handles
134 builder type_constraint
137 sub legal_options_for_inheritance { @legal_options_for_inheritance }
140 # This method *must* be able to handle
141 # Class::MOP::Attribute instances as
142 # well. Yes, I know that is wrong, but
143 # apparently we didn't realize it was
144 # doing that and now we have some code
145 # which is dependent on it. The real
146 # solution of course is to push this
147 # feature back up into Class::MOP::Attribute
148 # but I not right now, I am too lazy.
149 # However if you are reading this and
150 # looking for something to do,.. please
153 sub clone_and_inherit_options {
154 my ($self, %options) = @_;
161 # we may want to extends a Class::MOP::Attribute
162 # in which case we need to be able to use the
163 # core set of legal options that have always
164 # been here. But we allows Moose::Meta::Attribute
165 # instances to changes them.
167 my @legal_options = $self->can('legal_options_for_inheritance')
168 ? $self->legal_options_for_inheritance
169 : @legal_options_for_inheritance;
171 foreach my $legal_option (@legal_options) {
172 if (exists $options{$legal_option}) {
173 $actual_options{$legal_option} = $options{$legal_option};
174 delete $options{$legal_option};
180 if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
181 $type_constraint = $options{isa};
184 $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
185 (defined $type_constraint)
186 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
189 $actual_options{type_constraint} = $type_constraint;
190 delete $options{isa};
193 if ($options{does}) {
195 if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
196 $type_constraint = $options{does};
199 $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
200 (defined $type_constraint)
201 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
204 $actual_options{type_constraint} = $type_constraint;
205 delete $options{does};
209 # this doesn't apply to Class::MOP::Attributes,
210 # so we can ignore it for them.
212 if ($self->can('interpolate_class')) {
213 ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(%options);
216 my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
217 $actual_options{traits} = \@all_traits if @all_traits;
219 delete @options{qw(metaclass traits)};
222 (scalar keys %options == 0)
223 || $self->throw_error("Illegal inherited options => (" . (join ', ' => keys %options) . ")", data => \%options);
226 $self->clone(%actual_options);
230 my ( $self, %params ) = @_;
232 my $class = $params{metaclass} || ref $self;
234 if ( 0 and $class eq ref $self ) {
235 return $self->SUPER::clone(%params);
237 my ( @init, @non_init );
239 foreach my $attr ( grep { $_->has_value($self) } $self->meta->compute_all_applicable_attributes ) {
240 push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
243 my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
245 my $name = delete $new_params{name};
247 my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
249 foreach my $attr ( @non_init ) {
250 $attr->set_value($clone, $attr->get_value($self));
258 sub _process_options {
259 my ($class, $name, $options) = @_;
261 if (exists $options->{is}) {
263 ### -------------------------
264 ## is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
265 ## is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
266 ## is => rw, accessor => _foo # turns into (accessor => _foo)
267 ## is => ro, accessor => _foo # error, accesor is rw
268 ### -------------------------
270 if ($options->{is} eq 'ro') {
271 $class->throw_error("Cannot define an accessor name on a read-only attribute, accessors are read/write", data => $options)
272 if exists $options->{accessor};
273 $options->{reader} ||= $name;
275 elsif ($options->{is} eq 'rw') {
276 if ($options->{writer}) {
277 $options->{reader} ||= $name;
280 $options->{accessor} ||= $name;
284 $class->throw_error("I do not understand this option (is => " . $options->{is} . ") on attribute ($name)", data => $options->{is});
288 if (exists $options->{isa}) {
289 if (exists $options->{does}) {
290 if (eval { $options->{isa}->can('does') }) {
291 ($options->{isa}->does($options->{does}))
292 || $class->throw_error("Cannot have an isa option and a does option if the isa does not do the does on attribute ($name)", data => $options);
295 $class->throw_error("Cannot have an isa option which cannot ->does() on attribute ($name)", data => $options);
299 # allow for anon-subtypes here ...
300 if (blessed($options->{isa}) && $options->{isa}->isa('Moose::Meta::TypeConstraint')) {
301 $options->{type_constraint} = $options->{isa};
304 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options->{isa});
307 elsif (exists $options->{does}) {
308 # allow for anon-subtypes here ...
309 if (blessed($options->{does}) && $options->{does}->isa('Moose::Meta::TypeConstraint')) {
310 $options->{type_constraint} = $options->{does};
313 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options->{does});
317 if (exists $options->{coerce} && $options->{coerce}) {
318 (exists $options->{type_constraint})
319 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)", data => $options);
320 $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)", data => $options)
321 if $options->{weak_ref};
324 if (exists $options->{trigger}) {
325 ('CODE' eq ref $options->{trigger})
326 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
329 if (exists $options->{auto_deref} && $options->{auto_deref}) {
330 (exists $options->{type_constraint})
331 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)", data => $options);
332 ($options->{type_constraint}->is_a_type_of('ArrayRef') ||
333 $options->{type_constraint}->is_a_type_of('HashRef'))
334 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)", data => $options);
337 if (exists $options->{lazy_build} && $options->{lazy_build} == 1) {
338 $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options)
339 if exists $options->{default};
340 $options->{lazy} = 1;
341 $options->{required} = 1;
342 $options->{builder} ||= "_build_${name}";
344 $options->{clearer} ||= "_clear${name}";
345 $options->{predicate} ||= "_has${name}";
348 $options->{clearer} ||= "clear_${name}";
349 $options->{predicate} ||= "has_${name}";
353 if (exists $options->{lazy} && $options->{lazy}) {
354 (exists $options->{default} || defined $options->{builder} )
355 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it", data => $options);
358 if ( $options->{required} && !( ( !exists $options->{init_arg} || defined $options->{init_arg} ) || exists $options->{default} || defined $options->{builder} ) ) {
359 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg", data => $options);
364 sub initialize_instance_slot {
365 my ($self, $meta_instance, $instance, $params) = @_;
366 my $init_arg = $self->init_arg();
367 # try to fetch the init arg from the %params ...
371 if ( defined($init_arg) and exists $params->{$init_arg}) {
372 $val = $params->{$init_arg};
376 # skip it if it's lazy
377 return if $self->is_lazy;
378 # and die if it's required and doesn't have a default value
379 $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
380 if $self->is_required && !$self->has_default && !$self->has_builder;
382 # if nothing was in the %params, we can use the
383 # attribute's default value (if it has one)
384 if ($self->has_default) {
385 $val = $self->default($instance);
388 elsif ($self->has_builder) {
389 $val = $self->_call_builder($instance);
394 return unless $value_is_set;
396 if ($self->has_type_constraint) {
397 my $type_constraint = $self->type_constraint;
398 if ($self->should_coerce && $type_constraint->has_coercion) {
399 $val = $type_constraint->coerce($val);
401 $type_constraint->check($val)
402 || $self->throw_error("Attribute ("
404 . ") does not pass the type constraint because: "
405 . $type_constraint->get_message($val), data => $val, object => $instance);
408 $self->set_initial_value($instance, $val);
409 $meta_instance->weaken_slot_value($instance, $self->name)
410 if ref $val && $self->is_weak_ref;
414 my ( $self, $instance ) = @_;
416 my $builder = $self->builder();
418 return $instance->$builder()
419 if $instance->can( $self->builder );
421 $self->throw_error( blessed($instance)
422 . " does not support builder method '"
424 . "' for attribute '"
434 # this duplicates too much code from
435 # Class::MOP::Attribute, we need to
436 # refactor these bits eventually.
438 sub _set_initial_slot_value {
439 my ($self, $meta_instance, $instance, $value) = @_;
441 my $slot_name = $self->name;
443 return $meta_instance->set_slot_value($instance, $slot_name, $value)
444 unless $self->has_initializer;
446 my ($type_constraint, $can_coerce);
447 if ($self->has_type_constraint) {
448 $type_constraint = $self->type_constraint;
449 $can_coerce = ($self->should_coerce && $type_constraint->has_coercion);
454 if ($type_constraint) {
455 $val = $type_constraint->coerce($val)
457 $type_constraint->check($val)
458 || $self->throw_error("Attribute ("
460 . ") does not pass the type constraint because: "
461 . $type_constraint->get_message($val), data => $val, object => $instance);
463 $meta_instance->set_slot_value($instance, $slot_name, $val);
466 my $initializer = $self->initializer;
468 # most things will just want to set a value, so make it first arg
469 $instance->$initializer($value, $callback, $self);
473 my ($self, $instance, @args) = @_;
474 my $value = $args[0];
476 my $attr_name = $self->name;
478 if ($self->is_required and not @args) {
479 $self->throw_error("Attribute ($attr_name) is required", object => $instance);
482 if ($self->has_type_constraint) {
484 my $type_constraint = $self->type_constraint;
486 if ($self->should_coerce) {
487 $value = $type_constraint->coerce($value);
489 $type_constraint->_compiled_type_constraint->($value)
490 || $self->throw_error("Attribute ("
492 . ") does not pass the type constraint because "
493 . $type_constraint->get_message($value), object => $instance, data => $value);
496 my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
499 $meta_instance->set_slot_value($instance, $attr_name, $value);
501 if (ref $value && $self->is_weak_ref) {
502 $meta_instance->weaken_slot_value($instance, $attr_name);
505 if ($self->has_trigger) {
506 $self->trigger->($instance, $value, $self);
511 my ($self, $instance) = @_;
513 if ($self->is_lazy) {
514 unless ($self->has_value($instance)) {
516 if ($self->has_default) {
517 $value = $self->default($instance);
518 } elsif ( $self->has_builder ) {
519 $value = $self->_call_builder($instance);
521 if ($self->has_type_constraint) {
522 my $type_constraint = $self->type_constraint;
523 $value = $type_constraint->coerce($value)
524 if ($self->should_coerce);
525 $type_constraint->check($value)
526 || $self->throw_error("Attribute (" . $self->name
527 . ") does not pass the type constraint because: "
528 . $type_constraint->get_message($value), type_constraint => $type_constraint, data => $value);
530 $self->set_initial_value($instance, $value);
534 if ($self->should_auto_deref) {
536 my $type_constraint = $self->type_constraint;
538 if ($type_constraint->is_a_type_of('ArrayRef')) {
539 my $rv = $self->SUPER::get_value($instance);
540 return unless defined $rv;
541 return wantarray ? @{ $rv } : $rv;
543 elsif ($type_constraint->is_a_type_of('HashRef')) {
544 my $rv = $self->SUPER::get_value($instance);
545 return unless defined $rv;
546 return wantarray ? %{ $rv } : $rv;
549 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
555 return $self->SUPER::get_value($instance);
559 ## installing accessors
561 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
563 sub install_accessors {
565 $self->SUPER::install_accessors(@_);
566 $self->install_delegation if $self->has_handles;
570 sub remove_accessors {
572 $self->SUPER::remove_accessors(@_);
573 $self->remove_delegation if $self->has_handles;
577 sub install_delegation {
581 # Here we canonicalize the 'handles' option
582 # this will sort out any details and always
583 # return an hash of methods which we want
584 # to delagate to, see that method for details
585 my %handles = $self->_canonicalize_handles;
588 # install the delegation ...
589 my $associated_class = $self->associated_class;
590 foreach my $handle (keys %handles) {
591 my $method_to_call = $handles{$handle};
592 my $class_name = $associated_class->name;
593 my $name = "${class_name}::${handle}";
595 (!$associated_class->has_method($handle))
596 || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle);
599 # handles is not allowed to delegate
600 # any of these methods, as they will
601 # override the ones in your class, which
602 # is almost certainly not what you want.
604 # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
605 #cluck("Not delegating method '$handle' because it is a core method") and
606 next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
608 my $method = $self->_make_delegation_method($handle, $method_to_call);
610 $self->associated_class->add_method($method->name, $method);
614 sub remove_delegation {
616 my %handles = $self->_canonicalize_handles;
617 my $associated_class = $self->associated_class;
618 foreach my $handle (keys %handles) {
619 $self->associated_class->remove_method($handle);
623 # private methods to help delegation ...
625 sub _canonicalize_handles {
627 my $handles = $self->handles;
628 if (my $handle_type = ref($handles)) {
629 if ($handle_type eq 'HASH') {
632 elsif ($handle_type eq 'ARRAY') {
633 return map { $_ => $_ } @{$handles};
635 elsif ($handle_type eq 'Regexp') {
636 ($self->has_type_constraint)
637 || $self->throw_error("Cannot delegate methods based on a RegExpr without a type constraint (isa)", data => $handles);
638 return map { ($_ => $_) }
639 grep { /$handles/ } $self->_get_delegate_method_list;
641 elsif ($handle_type eq 'CODE') {
642 return $handles->($self, $self->_find_delegate_metaclass);
645 $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
649 Class::MOP::load_class($handles)
650 unless Class::MOP::is_class_loaded($handles);
652 my $role_meta = eval { $handles->meta };
654 $self->throw_error("Unable to canonicalize the 'handles' option with $handles because : $@", data => $handles, error => $@);
657 (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
658 || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because ->meta is not a Moose::Meta::Role", data => $handles);
660 return map { $_ => $_ } (
661 $role_meta->get_method_list,
662 $role_meta->get_required_method_list
667 sub _find_delegate_metaclass {
669 if (my $class = $self->_isa_metadata) {
670 # if the class does have
671 # a meta method, use it
672 return $class->meta if $class->can('meta');
673 # otherwise we might be
674 # dealing with a non-Moose
675 # class, and need to make
677 return Moose::Meta::Class->initialize($class);
679 elsif (my $role = $self->_does_metadata) {
680 # our role will always have
685 $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
689 sub _get_delegate_method_list {
691 my $meta = $self->_find_delegate_metaclass;
692 if ($meta->isa('Class::MOP::Class')) {
693 return map { $_->name } # NOTE: !never! delegate &meta
694 grep { $_->package_name ne 'Moose::Object' && $_->name ne 'meta' }
695 $meta->get_all_methods;
697 elsif ($meta->isa('Moose::Meta::Role')) {
698 return $meta->get_method_list;
701 $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
705 sub _make_delegation_method {
706 my ( $self, $handle_name, $method_to_call ) = @_;
710 $method_body = $method_to_call
711 if 'CODE' eq ref($method_to_call);
713 return Moose::Meta::Method::Delegation->new(
714 name => $handle_name,
715 package_name => $self->associated_class->name,
717 delegate_to_method => $method_to_call,
721 package Moose::Meta::Attribute::Custom::Moose;
722 sub register_implementation { 'Moose::Meta::Attribute' }
732 Moose::Meta::Attribute - The Moose attribute metaclass
736 This is a subclass of L<Class::MOP::Attribute> with Moose specific
739 For the most part, the only time you will ever encounter an
740 instance of this class is if you are doing some serious deep
741 introspection. To really understand this class, you need to refer
742 to the L<Class::MOP::Attribute> documentation.
746 =head2 Overridden methods
748 These methods override methods in L<Class::MOP::Attribute> and add
749 Moose specific features. You can safely assume though that they
750 will behave just as L<Class::MOP::Attribute> does.
760 =item B<initialize_instance_slot>
762 =item B<install_accessors>
764 =item B<remove_accessors>
766 =item B<install_delegation>
768 =item B<remove_delegation>
770 =item B<accessor_metaclass>
776 eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') };
781 I<Attribute (x) does not pass the type constraint (Int) with 'fourty-two'>
783 Before setting the value, a check is made on the type constraint of
784 the attribute, if it has one, to see if the value passes it. If the
785 value fails to pass, the set operation dies with a L<throw_error>.
787 Any coercion to convert values is done before checking the type constraint.
789 To check a value against a type constraint before setting it, fetch the
790 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
791 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
792 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
797 =head2 Additional Moose features
799 Moose attributes support type-constraint checking, weak reference
800 creation and type coercion.
806 Delegates to C<associated_class> or C<Moose::Meta::Class> if there is none.
808 =item B<interpolate_class_and_new>
810 =item B<interpolate_class>
812 When called as a class method causes interpretation of the C<metaclass> and
815 =item B<clone_and_inherit_options>
817 This is to support the C<has '+foo'> feature, it clones an attribute
818 from a superclass and allows a very specific set of changes to be made
821 =item B<legal_options_for_inheritance>
823 Whitelist with options you can change. You can overload it in your custom
824 metaclass to allow your options be inheritable.
826 =item B<has_type_constraint>
828 Returns true if this meta-attribute has a type constraint.
830 =item B<type_constraint>
832 A read-only accessor for this meta-attribute's type constraint. For
833 more information on what you can do with this, see the documentation
834 for L<Moose::Meta::TypeConstraint>.
838 Returns true if this meta-attribute performs delegation.
842 This returns the value which was passed into the handles option.
846 Returns true if this meta-attribute produces a weak reference.
850 Returns true if this meta-attribute is required to have a value.
854 Returns true if this meta-attribute should be initialized lazily.
856 NOTE: lazy attributes, B<must> have a C<default> or C<builder> field set.
858 =item B<is_lazy_build>
860 Returns true if this meta-attribute should be initialized lazily through
861 the builder generated by lazy_build. Using C<lazy_build =E<gt> 1> will
862 make your attribute required and lazy. In addition it will set the builder, clearer
863 and predicate options for you using the following convention.
865 #If your attribute name starts with an underscore:
866 has '_foo' => (lazy_build => 1);
868 has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo');
870 has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo});
872 #If your attribute name does not start with an underscore:
873 has 'foo' => (lazy_build => 1);
875 has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo');
877 has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo});
879 The reason for the different naming of the C<builder> is that the C<builder>
880 method is a private method while the C<clearer> and C<predicate> methods
883 NOTE: This means your class should provide a method whose name matches the value
884 of the builder part, in this case _build__foo or _build_foo.
886 =item B<should_coerce>
888 Returns true if this meta-attribute should perform type coercion.
890 =item B<should_auto_deref>
892 Returns true if this meta-attribute should perform automatic
895 NOTE: This can only be done for attributes whose type constraint is
896 either I<ArrayRef> or I<HashRef>.
900 Returns true if this meta-attribute has a trigger set.
904 This is a CODE reference which will be executed every time the
905 value of an attribute is assigned. The CODE ref will get two values,
906 the invocant and the new value. This can be used to handle I<basic>
907 bi-directional relations.
909 =item B<documentation>
911 This is a string which contains the documentation for this attribute.
912 It serves no direct purpose right now, but it might in the future
913 in some kind of automated documentation system perhaps.
915 =item B<has_documentation>
917 Returns true if this meta-attribute has any documentation.
919 =item B<applied_traits>
921 This will return the ARRAY ref of all the traits applied to this
922 attribute, or if no traits have been applied, it returns C<undef>.
924 =item B<has_applied_traits>
926 Returns true if this meta-attribute has any traits applied.
932 All complex software has bugs lurking in it, and this module is no
933 exception. If you find a bug please either email me, or add the bug
938 Stevan Little E<lt>stevan@iinteractive.comE<gt>
940 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
942 =head1 COPYRIGHT AND LICENSE
944 Copyright 2006-2008 by Infinity Interactive, Inc.
946 L<http://www.iinteractive.com>
948 This library is free software; you can redistribute it and/or modify
949 it under the same terms as Perl itself.