2 package Moose::Meta::Attribute;
7 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.57';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use Moose::Meta::Method::Accessor;
15 use Moose::Util::TypeConstraints ();
17 use base 'Class::MOP::Attribute';
19 # options which are not directly used
20 # but we store them for metadata purposes
21 __PACKAGE__->meta->add_attribute('isa' => (reader => '_isa_metadata'));
22 __PACKAGE__->meta->add_attribute('does' => (reader => '_does_metadata'));
23 __PACKAGE__->meta->add_attribute('is' => (reader => '_is_metadata'));
25 # these are actual options for the attrs
26 __PACKAGE__->meta->add_attribute('required' => (reader => 'is_required' ));
27 __PACKAGE__->meta->add_attribute('lazy' => (reader => 'is_lazy' ));
28 __PACKAGE__->meta->add_attribute('lazy_build' => (reader => 'is_lazy_build' ));
29 __PACKAGE__->meta->add_attribute('coerce' => (reader => 'should_coerce' ));
30 __PACKAGE__->meta->add_attribute('weak_ref' => (reader => 'is_weak_ref' ));
31 __PACKAGE__->meta->add_attribute('auto_deref' => (reader => 'should_auto_deref'));
32 __PACKAGE__->meta->add_attribute('type_constraint' => (
33 reader => 'type_constraint',
34 predicate => 'has_type_constraint',
36 __PACKAGE__->meta->add_attribute('trigger' => (
38 predicate => 'has_trigger',
40 __PACKAGE__->meta->add_attribute('handles' => (
42 predicate => 'has_handles',
44 __PACKAGE__->meta->add_attribute('documentation' => (
45 reader => 'documentation',
46 predicate => 'has_documentation',
48 __PACKAGE__->meta->add_attribute('traits' => (
49 reader => 'applied_traits',
50 predicate => 'has_applied_traits',
53 # we need to have a ->does method in here to
54 # more easily support traits, and the introspection
55 # of those traits. We extend the does check to look
56 # for metatrait aliases.
58 my ($self, $role_name) = @_;
60 Moose::Util::resolve_metatrait_alias(Attribute => $role_name)
62 return 0 if !defined($name); # failed to load class
63 return Moose::Object::does($self, $name);
68 my $class = ( ref $self && $self->associated_class ) || "Moose::Meta::Class";
69 unshift @_, "message" if @_ % 2 == 1;
70 unshift @_, attr => $self if ref $self;
72 goto $class->can("throw_error"); # to avoid incrementing depth by 1
76 my ($class, $name, %options) = @_;
77 $class->_process_options($name, \%options) unless $options{__hack_no_process_options}; # used from clone()... YECHKKK FIXME ICKY YUCK GROSS
78 return $class->SUPER::new($name, %options);
81 sub interpolate_class_and_new {
82 my ($class, $name, @args) = @_;
84 my ( $new_class, @traits ) = $class->interpolate_class(@args);
86 $new_class->new($name, @args, ( scalar(@traits) ? ( traits => \@traits ) : () ) );
89 sub interpolate_class {
90 my ($class, %options) = @_;
92 $class = ref($class) || $class;
94 if ( my $metaclass_name = delete $options{metaclass} ) {
95 my $new_class = Moose::Util::resolve_metaclass_alias( Attribute => $metaclass_name );
97 if ( $class ne $new_class ) {
98 if ( $new_class->can("interpolate_class") ) {
99 return $new_class->interpolate_class(%options);
108 if (my $traits = $options{traits}) {
109 if ( @traits = grep { not $class->does($_) } map {
110 Moose::Util::resolve_metatrait_alias( Attribute => $_ )
114 my $anon_class = Moose::Meta::Class->create_anon_class(
115 superclasses => [ $class ],
116 roles => [ @traits ],
120 $class = $anon_class->name;
124 return ( wantarray ? ( $class, @traits ) : $class );
129 my @legal_options_for_inheritance = qw(
130 default coerce required
131 documentation lazy handles
132 builder type_constraint
135 sub legal_options_for_inheritance { @legal_options_for_inheritance }
138 # This method *must* be able to handle
139 # Class::MOP::Attribute instances as
140 # well. Yes, I know that is wrong, but
141 # apparently we didn't realize it was
142 # doing that and now we have some code
143 # which is dependent on it. The real
144 # solution of course is to push this
145 # feature back up into Class::MOP::Attribute
146 # but I not right now, I am too lazy.
147 # However if you are reading this and
148 # looking for something to do,.. please
151 sub clone_and_inherit_options {
152 my ($self, %options) = @_;
159 # we may want to extends a Class::MOP::Attribute
160 # in which case we need to be able to use the
161 # core set of legal options that have always
162 # been here. But we allows Moose::Meta::Attribute
163 # instances to changes them.
165 my @legal_options = $self->can('legal_options_for_inheritance')
166 ? $self->legal_options_for_inheritance
167 : @legal_options_for_inheritance;
169 foreach my $legal_option (@legal_options) {
170 if (exists $options{$legal_option}) {
171 $actual_options{$legal_option} = $options{$legal_option};
172 delete $options{$legal_option};
178 if (blessed($options{isa}) && $options{isa}->isa('Moose::Meta::TypeConstraint')) {
179 $type_constraint = $options{isa};
182 $type_constraint = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options{isa});
183 (defined $type_constraint)
184 || $self->throw_error("Could not find the type constraint '" . $options{isa} . "'", data => $options{isa});
187 $actual_options{type_constraint} = $type_constraint;
188 delete $options{isa};
191 if ($options{does}) {
193 if (blessed($options{does}) && $options{does}->isa('Moose::Meta::TypeConstraint')) {
194 $type_constraint = $options{does};
197 $type_constraint = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options{does});
198 (defined $type_constraint)
199 || $self->throw_error("Could not find the type constraint '" . $options{does} . "'", data => $options{does});
202 $actual_options{type_constraint} = $type_constraint;
203 delete $options{does};
207 # this doesn't apply to Class::MOP::Attributes,
208 # so we can ignore it for them.
210 if ($self->can('interpolate_class')) {
211 ( $actual_options{metaclass}, my @traits ) = $self->interpolate_class(%options);
214 my @all_traits = grep { $seen{$_}++ } @{ $self->applied_traits || [] }, @traits;
215 $actual_options{traits} = \@all_traits if @all_traits;
217 delete @options{qw(metaclass traits)};
220 (scalar keys %options == 0)
221 || $self->throw_error("Illegal inherited options => (" . (join ', ' => keys %options) . ")", data => \%options);
224 $self->clone(%actual_options);
228 my ( $self, %params ) = @_;
230 my $class = $params{metaclass} || ref $self;
232 if ( 0 and $class eq ref $self ) {
233 return $self->SUPER::clone(%params);
235 my ( @init, @non_init );
237 foreach my $attr ( grep { $_->has_value($self) } $self->meta->compute_all_applicable_attributes ) {
238 push @{ $attr->has_init_arg ? \@init : \@non_init }, $attr;
241 my %new_params = ( ( map { $_->init_arg => $_->get_value($self) } @init ), %params );
243 my $name = delete $new_params{name};
245 my $clone = $class->new($name, %new_params, __hack_no_process_options => 1 );
247 foreach my $attr ( @non_init ) {
248 $attr->set_value($clone, $attr->get_value($self));
256 sub _process_options {
257 my ($class, $name, $options) = @_;
259 if (exists $options->{is}) {
261 ### -------------------------
262 ## is => ro, writer => _foo # turns into (reader => foo, writer => _foo) as before
263 ## is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
264 ## is => rw, accessor => _foo # turns into (accessor => _foo)
265 ## is => ro, accessor => _foo # error, accesor is rw
266 ### -------------------------
268 if ($options->{is} eq 'ro') {
269 $class->throw_error("Cannot define an accessor name on a read-only attribute, accessors are read/write", data => $options)
270 if exists $options->{accessor};
271 $options->{reader} ||= $name;
273 elsif ($options->{is} eq 'rw') {
274 if ($options->{writer}) {
275 $options->{reader} ||= $name;
278 $options->{accessor} ||= $name;
282 $class->throw_error("I do not understand this option (is => " . $options->{is} . ") on attribute ($name)", data => $options->{is});
286 if (exists $options->{isa}) {
287 if (exists $options->{does}) {
288 if (eval { $options->{isa}->can('does') }) {
289 ($options->{isa}->does($options->{does}))
290 || $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);
293 $class->throw_error("Cannot have an isa option which cannot ->does() on attribute ($name)", data => $options);
297 # allow for anon-subtypes here ...
298 if (blessed($options->{isa}) && $options->{isa}->isa('Moose::Meta::TypeConstraint')) {
299 $options->{type_constraint} = $options->{isa};
302 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($options->{isa});
305 elsif (exists $options->{does}) {
306 # allow for anon-subtypes here ...
307 if (blessed($options->{does}) && $options->{does}->isa('Moose::Meta::TypeConstraint')) {
308 $options->{type_constraint} = $options->{does};
311 $options->{type_constraint} = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($options->{does});
315 if (exists $options->{coerce} && $options->{coerce}) {
316 (exists $options->{type_constraint})
317 || $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)", data => $options);
318 $class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)", data => $options)
319 if $options->{weak_ref};
322 if (exists $options->{trigger}) {
323 ('CODE' eq ref $options->{trigger})
324 || $class->throw_error("Trigger must be a CODE ref on attribute ($name)", data => $options->{trigger});
327 if (exists $options->{auto_deref} && $options->{auto_deref}) {
328 (exists $options->{type_constraint})
329 || $class->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)", data => $options);
330 ($options->{type_constraint}->is_a_type_of('ArrayRef') ||
331 $options->{type_constraint}->is_a_type_of('HashRef'))
332 || $class->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)", data => $options);
335 if (exists $options->{lazy_build} && $options->{lazy_build} == 1) {
336 $class->throw_error("You can not use lazy_build and default for the same attribute ($name)", data => $options)
337 if exists $options->{default};
338 $options->{lazy} = 1;
339 $options->{required} = 1;
340 $options->{builder} ||= "_build_${name}";
342 $options->{clearer} ||= "_clear${name}";
343 $options->{predicate} ||= "_has${name}";
346 $options->{clearer} ||= "clear_${name}";
347 $options->{predicate} ||= "has_${name}";
351 if (exists $options->{lazy} && $options->{lazy}) {
352 (exists $options->{default} || defined $options->{builder} )
353 || $class->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it", data => $options);
356 if ( $options->{required} && !( ( !exists $options->{init_arg} || defined $options->{init_arg} ) || exists $options->{default} || defined $options->{builder} ) ) {
357 $class->throw_error("You cannot have a required attribute ($name) without a default, builder, or an init_arg", data => $options);
362 sub initialize_instance_slot {
363 my ($self, $meta_instance, $instance, $params) = @_;
364 my $init_arg = $self->init_arg();
365 # try to fetch the init arg from the %params ...
369 if ( defined($init_arg) and exists $params->{$init_arg}) {
370 $val = $params->{$init_arg};
374 # skip it if it's lazy
375 return if $self->is_lazy;
376 # and die if it's required and doesn't have a default value
377 $self->throw_error("Attribute (" . $self->name . ") is required", object => $instance, data => $params)
378 if $self->is_required && !$self->has_default && !$self->has_builder;
380 # if nothing was in the %params, we can use the
381 # attribute's default value (if it has one)
382 if ($self->has_default) {
383 $val = $self->default($instance);
386 elsif ($self->has_builder) {
387 $val = $self->_call_builder($instance);
392 return unless $value_is_set;
394 if ($self->has_type_constraint) {
395 my $type_constraint = $self->type_constraint;
396 if ($self->should_coerce && $type_constraint->has_coercion) {
397 $val = $type_constraint->coerce($val);
399 $type_constraint->check($val)
400 || $self->throw_error("Attribute ("
402 . ") does not pass the type constraint because: "
403 . $type_constraint->get_message($val), data => $val, object => $instance);
406 $self->set_initial_value($instance, $val);
407 $meta_instance->weaken_slot_value($instance, $self->name)
408 if ref $val && $self->is_weak_ref;
412 my ( $self, $instance ) = @_;
414 my $builder = $self->builder();
416 return $instance->$builder()
417 if $instance->can( $self->builder );
419 $self->throw_error( blessed($instance)
420 . " does not support builder method '"
422 . "' for attribute '"
432 # this duplicates too much code from
433 # Class::MOP::Attribute, we need to
434 # refactor these bits eventually.
436 sub _set_initial_slot_value {
437 my ($self, $meta_instance, $instance, $value) = @_;
439 my $slot_name = $self->name;
441 return $meta_instance->set_slot_value($instance, $slot_name, $value)
442 unless $self->has_initializer;
444 my ($type_constraint, $can_coerce);
445 if ($self->has_type_constraint) {
446 $type_constraint = $self->type_constraint;
447 $can_coerce = ($self->should_coerce && $type_constraint->has_coercion);
452 if ($type_constraint) {
453 $val = $type_constraint->coerce($val)
455 $type_constraint->check($val)
456 || $self->throw_error("Attribute ("
458 . ") does not pass the type constraint because: "
459 . $type_constraint->get_message($val), data => $val, object => $instance);
461 $meta_instance->set_slot_value($instance, $slot_name, $val);
464 my $initializer = $self->initializer;
466 # most things will just want to set a value, so make it first arg
467 $instance->$initializer($value, $callback, $self);
471 my ($self, $instance, @args) = @_;
472 my $value = $args[0];
474 my $attr_name = $self->name;
476 if ($self->is_required and not @args) {
477 $self->throw_error("Attribute ($attr_name) is required", object => $instance);
480 if ($self->has_type_constraint) {
482 my $type_constraint = $self->type_constraint;
484 if ($self->should_coerce) {
485 $value = $type_constraint->coerce($value);
487 $type_constraint->_compiled_type_constraint->($value)
488 || $self->throw_error("Attribute ("
490 . ") does not pass the type constraint because "
491 . $type_constraint->get_message($value), object => $instance, data => $value);
494 my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
497 $meta_instance->set_slot_value($instance, $attr_name, $value);
499 if (ref $value && $self->is_weak_ref) {
500 $meta_instance->weaken_slot_value($instance, $attr_name);
503 if ($self->has_trigger) {
504 $self->trigger->($instance, $value, $self);
509 my ($self, $instance) = @_;
511 if ($self->is_lazy) {
512 unless ($self->has_value($instance)) {
514 if ($self->has_default) {
515 $value = $self->default($instance);
516 } elsif ( $self->has_builder ) {
517 $value = $self->_call_builder($instance);
519 if ($self->has_type_constraint) {
520 my $type_constraint = $self->type_constraint;
521 $value = $type_constraint->coerce($value)
522 if ($self->should_coerce);
523 $type_constraint->check($value)
524 || c$self->throw_error("Attribute (" . $self->name
525 . ") does not pass the type constraint because: "
526 . $type_constraint->get_message($value), type_constraint => $type_constraint, data => $value);
528 $self->set_initial_value($instance, $value);
532 if ($self->should_auto_deref) {
534 my $type_constraint = $self->type_constraint;
536 if ($type_constraint->is_a_type_of('ArrayRef')) {
537 my $rv = $self->SUPER::get_value($instance);
538 return unless defined $rv;
539 return wantarray ? @{ $rv } : $rv;
541 elsif ($type_constraint->is_a_type_of('HashRef')) {
542 my $rv = $self->SUPER::get_value($instance);
543 return unless defined $rv;
544 return wantarray ? %{ $rv } : $rv;
547 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", object => $instance, type_constraint => $type_constraint);
553 return $self->SUPER::get_value($instance);
557 ## installing accessors
559 sub accessor_metaclass { 'Moose::Meta::Method::Accessor' }
561 sub install_accessors {
563 $self->SUPER::install_accessors(@_);
564 $self->install_delegation if $self->has_handles;
568 sub install_delegation {
572 # Here we canonicalize the 'handles' option
573 # this will sort out any details and always
574 # return an hash of methods which we want
575 # to delagate to, see that method for details
576 my %handles = $self->_canonicalize_handles;
578 # find the accessor method for this attribute
579 my $accessor = $self->_get_delegate_accessor;
581 # install the delegation ...
582 my $associated_class = $self->associated_class;
583 foreach my $handle (keys %handles) {
584 my $method_to_call = $handles{$handle};
585 my $class_name = $associated_class->name;
586 my $name = "${class_name}::${handle}";
588 (!$associated_class->has_method($handle))
589 || $self->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation", method_name => $handle);
592 # handles is not allowed to delegate
593 # any of these methods, as they will
594 # override the ones in your class, which
595 # is almost certainly not what you want.
597 # FIXME warn when $handle was explicitly specified, but not if the source is a regex or something
598 #cluck("Not delegating method '$handle' because it is a core method") and
599 next if $class_name->isa("Moose::Object") and $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
601 if ('CODE' eq ref($method_to_call)) {
602 $associated_class->add_method($handle => Class::MOP::subname($name, $method_to_call));
606 # we used to do a goto here, but the
607 # goto didn't handle failure correctly
608 # (it just returned nothing), so I took
609 # that out. However, the more I thought
610 # about it, the less I liked it doing
611 # the goto, and I prefered the act of
612 # delegation being actually represented
613 # in the stack trace.
615 $associated_class->add_method($handle => Class::MOP::subname($name, sub {
616 my $instance = shift;
617 my $proxy = $instance->$accessor();
619 || $self->throw_error("Cannot delegate $handle to $method_to_call because " .
620 "the value of " . $self->name . " is not defined", method_name => $method_to_call, object => $instance);
621 $proxy->$method_to_call(@_);
627 # private methods to help delegation ...
629 sub _get_delegate_accessor {
631 # find the accessor method for this attribute
632 my $accessor = $self->get_read_method_ref;
633 # then unpack it if we need too ...
634 $accessor = $accessor->body if blessed $accessor;
635 # return the accessor
639 sub _canonicalize_handles {
641 my $handles = $self->handles;
642 if (my $handle_type = ref($handles)) {
643 if ($handle_type eq 'HASH') {
646 elsif ($handle_type eq 'ARRAY') {
647 return map { $_ => $_ } @{$handles};
649 elsif ($handle_type eq 'Regexp') {
650 ($self->has_type_constraint)
651 || $self->throw_error("Cannot delegate methods based on a RegExpr without a type constraint (isa)", data => $handles);
652 return map { ($_ => $_) }
653 grep { /$handles/ } $self->_get_delegate_method_list;
655 elsif ($handle_type eq 'CODE') {
656 return $handles->($self, $self->_find_delegate_metaclass);
659 $self->throw_error("Unable to canonicalize the 'handles' option with $handles", data => $handles);
663 my $role_meta = eval { $handles->meta };
665 $self->throw_error("Unable to canonicalize the 'handles' option with $handles because : $@", data => $handles, error => $@);
668 (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
669 || $self->throw_error("Unable to canonicalize the 'handles' option with $handles because ->meta is not a Moose::Meta::Role", data => $handles);
671 return map { $_ => $_ } (
672 $role_meta->get_method_list,
673 $role_meta->get_required_method_list
678 sub _find_delegate_metaclass {
680 if (my $class = $self->_isa_metadata) {
681 # if the class does have
682 # a meta method, use it
683 return $class->meta if $class->can('meta');
684 # otherwise we might be
685 # dealing with a non-Moose
686 # class, and need to make
688 return Moose::Meta::Class->initialize($class);
690 elsif (my $role = $self->_does_metadata) {
691 # our role will always have
696 $self->throw_error("Cannot find delegate metaclass for attribute " . $self->name);
700 sub _get_delegate_method_list {
702 my $meta = $self->_find_delegate_metaclass;
703 if ($meta->isa('Class::MOP::Class')) {
704 return map { $_->name } # NOTE: !never! delegate &meta
705 grep { $_->package_name ne 'Moose::Object' && $_->name ne 'meta' }
706 $meta->get_all_methods;
708 elsif ($meta->isa('Moose::Meta::Role')) {
709 return $meta->get_method_list;
712 $self->throw_error("Unable to recognize the delegate metaclass '$meta'", data => $meta);
716 package Moose::Meta::Attribute::Custom::Moose;
717 sub register_implementation { 'Moose::Meta::Attribute' }
727 Moose::Meta::Attribute - The Moose attribute metaclass
731 This is a subclass of L<Class::MOP::Attribute> with Moose specific
734 For the most part, the only time you will ever encounter an
735 instance of this class is if you are doing some serious deep
736 introspection. To really understand this class, you need to refer
737 to the L<Class::MOP::Attribute> documentation.
741 =head2 Overridden methods
743 These methods override methods in L<Class::MOP::Attribute> and add
744 Moose specific features. You can safely assume though that they
745 will behave just as L<Class::MOP::Attribute> does.
755 =item B<initialize_instance_slot>
757 =item B<install_accessors>
759 =item B<install_delegation>
761 =item B<accessor_metaclass>
767 eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') };
772 I<Attribute (x) does not pass the type constraint (Int) with 'fourty-two'>
774 Before setting the value, a check is made on the type constraint of
775 the attribute, if it has one, to see if the value passes it. If the
776 value fails to pass, the set operation dies with a L<throw_error>.
778 Any coercion to convert values is done before checking the type constraint.
780 To check a value against a type constraint before setting it, fetch the
781 attribute instance using L<Class::MOP::Class/find_attribute_by_name>,
782 fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
783 and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::Basics::Recipe4>
788 =head2 Additional Moose features
790 Moose attributes support type-constraint checking, weak reference
791 creation and type coercion.
797 Delegates to C<associated_class> or C<Moose::Meta::Class> if there is none.
799 =item B<interpolate_class_and_new>
801 =item B<interpolate_class>
803 When called as a class method causes interpretation of the C<metaclass> and
806 =item B<clone_and_inherit_options>
808 This is to support the C<has '+foo'> feature, it clones an attribute
809 from a superclass and allows a very specific set of changes to be made
812 =item B<legal_options_for_inheritance>
814 Whitelist with options you can change. You can overload it in your custom
815 metaclass to allow your options be inheritable.
817 =item B<has_type_constraint>
819 Returns true if this meta-attribute has a type constraint.
821 =item B<type_constraint>
823 A read-only accessor for this meta-attribute's type constraint. For
824 more information on what you can do with this, see the documentation
825 for L<Moose::Meta::TypeConstraint>.
829 Returns true if this meta-attribute performs delegation.
833 This returns the value which was passed into the handles option.
837 Returns true if this meta-attribute produces a weak reference.
841 Returns true if this meta-attribute is required to have a value.
845 Returns true if this meta-attribute should be initialized lazily.
847 NOTE: lazy attributes, B<must> have a C<default> or C<builder> field set.
849 =item B<is_lazy_build>
851 Returns true if this meta-attribute should be initialized lazily through
852 the builder generated by lazy_build. Using C<lazy_build =E<gt> 1> will
853 make your attribute required and lazy. In addition it will set the builder, clearer
854 and predicate options for you using the following convention.
856 #If your attribute name starts with an underscore:
857 has '_foo' => (lazy_build => 1);
859 has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo');
861 has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo});
863 #If your attribute name does not start with an underscore:
864 has 'foo' => (lazy_build => 1);
866 has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo');
868 has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo});
870 The reason for the different naming of the C<builder> is that the C<builder>
871 method is a private method while the C<clearer> and C<predicate> methods
874 NOTE: This means your class should provide a method whose name matches the value
875 of the builder part, in this case _build__foo or _build_foo.
877 =item B<should_coerce>
879 Returns true if this meta-attribute should perform type coercion.
881 =item B<should_auto_deref>
883 Returns true if this meta-attribute should perform automatic
886 NOTE: This can only be done for attributes whose type constraint is
887 either I<ArrayRef> or I<HashRef>.
891 Returns true if this meta-attribute has a trigger set.
895 This is a CODE reference which will be executed every time the
896 value of an attribute is assigned. The CODE ref will get two values,
897 the invocant and the new value. This can be used to handle I<basic>
898 bi-directional relations.
900 =item B<documentation>
902 This is a string which contains the documentation for this attribute.
903 It serves no direct purpose right now, but it might in the future
904 in some kind of automated documentation system perhaps.
906 =item B<has_documentation>
908 Returns true if this meta-attribute has any documentation.
910 =item B<applied_traits>
912 This will return the ARRAY ref of all the traits applied to this
913 attribute, or if no traits have been applied, it returns C<undef>.
915 =item B<has_applied_traits>
917 Returns true if this meta-attribute has any traits applied.
923 All complex software has bugs lurking in it, and this module is no
924 exception. If you find a bug please either email me, or add the bug
929 Stevan Little E<lt>stevan@iinteractive.comE<gt>
931 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
933 =head1 COPYRIGHT AND LICENSE
935 Copyright 2006-2008 by Infinity Interactive, Inc.
937 L<http://www.iinteractive.com>
939 This library is free software; you can redistribute it and/or modify
940 it under the same terms as Perl itself.