convert the rest of the simple readers in cmop to xs
[gitmo/Class-MOP.git] / lib / Class / MOP / Attribute.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Attribute;
3
4use strict;
5use warnings;
6
ba38bf08 7use Class::MOP::Method::Accessor;
8
2eb717d5 9use Carp 'confess';
9b522fc4 10use Scalar::Util 'blessed', 'weaken';
a8344505 11use Try::Tiny;
2eb717d5 12
a9f48b4b 13our $VERSION = '1.11';
d519662a 14$VERSION = eval $VERSION;
f0480c45 15our $AUTHORITY = 'cpan:STEVAN';
8b978dd5 16
9b871d79 17use base 'Class::MOP::Object', 'Class::MOP::Mixin::AttributeCore';
b1897d4d 18
727919c5 19# NOTE: (meta-circularity)
1d68af04 20# This method will be replaced in the
21# boostrap section of Class::MOP, by
22# a new version which uses the
727919c5 23# &Class::MOP::Class::construct_instance
24# method to build an attribute meta-object
25# which itself is described with attribute
1d68af04 26# meta-objects.
727919c5 27# - Ain't meta-circularity grand? :)
8b978dd5 28sub new {
649efb63 29 my ( $class, @args ) = @_;
30
31 unshift @args, "name" if @args % 2 == 1;
32 my %options = @args;
33
34 my $name = $options{name};
1d68af04 35
d9330488 36 (defined $name)
8b978dd5 37 || confess "You must provide a name for the attribute";
1d68af04 38
39 $options{init_arg} = $name
5659d76e 40 if not exists $options{init_arg};
1d68af04 41 if(exists $options{builder}){
42 confess("builder must be a defined scalar value which is a method name")
43 if ref $options{builder} || !(defined $options{builder});
44 confess("Setting both default and builder is not allowed.")
45 if exists $options{default};
8fe581e5 46 } else {
9b871d79 47 ($class->is_default_a_coderef(\%options))
8fe581e5 48 || confess("References are not allowed as default values, you must ".
3c0a8087 49 "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
8fe581e5 50 if exists $options{default} && ref $options{default};
1d68af04 51 }
2e877f58 52 if( $options{required} and not( defined($options{builder}) || defined($options{init_arg}) || exists $options{default} ) ) {
53 confess("A required attribute must have either 'init_arg', 'builder', or 'default'");
54 }
8683db0e 55
cb75020f 56 $class->_new(\%options);
4b698b1a 57}
58
59sub _new {
0bfc85b8 60 my $class = shift;
ec9e38e5 61
62 return Class::MOP::Class->initialize($class)->new_object(@_)
812d58f9 63 if $class ne __PACKAGE__;
ec9e38e5 64
0bfc85b8 65 my $options = @_ == 1 ? $_[0] : {@_};
4b698b1a 66
8b978dd5 67 bless {
d9d99689 68 'name' => $options->{name},
69 'accessor' => $options->{accessor},
70 'reader' => $options->{reader},
71 'writer' => $options->{writer},
72 'predicate' => $options->{predicate},
73 'clearer' => $options->{clearer},
74 'builder' => $options->{builder},
75 'init_arg' => $options->{init_arg},
8343d501 76 exists $options->{default}
77 ? ('default' => $options->{default})
78 : (),
d9d99689 79 'initializer' => $options->{initializer},
80 'definition_context' => $options->{definition_context},
1d68af04 81 # keep a weakened link to the
9ec169fe 82 # class we are associated with
8683db0e 83 'associated_class' => undef,
1d68af04 84 # and a list of the methods
3545c727 85 # associated with this attr
8683db0e 86 'associated_methods' => [],
dc9d420c 87 # this let's us keep track of
88 # our order inside the associated
89 # class
90 'insertion_order' => undef,
0bfc85b8 91 }, $class;
8b978dd5 92}
93
7b31baf4 94# NOTE:
1d68af04 95# this is a primative (and kludgy) clone operation
16e960bd 96# for now, it will be replaced in the Class::MOP
1d68af04 97# bootstrap with a proper one, however we know
5659d76e 98# that this one will work fine for now.
99sub clone {
100 my $self = shift;
101 my %options = @_;
102 (blessed($self))
103 || confess "Can only clone an instance";
b3fa93c7 104 return bless { %{$self}, %options } => ref($self);
5659d76e 105}
106
bd4e03f9 107sub initialize_instance_slot {
f892c0f0 108 my ($self, $meta_instance, $instance, $params) = @_;
8683db0e 109 my $init_arg = $self->{'init_arg'};
110
bd4e03f9 111 # try to fetch the init arg from the %params ...
8d2d4c67 112
1d68af04 113 # if nothing was in the %params, we can use the
bd4e03f9 114 # attribute's default value (if it has one)
2e877f58 115 if(defined $init_arg and exists $params->{$init_arg}){
8ee74136 116 $self->_set_initial_slot_value(
117 $meta_instance,
0ab65f99 118 $instance,
0ab65f99 119 $params->{$init_arg},
0ab65f99 120 );
b7bdffc3 121 }
8343d501 122 elsif (exists $self->{'default'}) {
8ee74136 123 $self->_set_initial_slot_value(
124 $meta_instance,
0ab65f99 125 $instance,
0ab65f99 126 $self->default($instance),
0ab65f99 127 );
b7bdffc3 128 }
8683db0e 129 elsif (defined( my $builder = $self->{'builder'})) {
b7bdffc3 130 if ($builder = $instance->can($builder)) {
8ee74136 131 $self->_set_initial_slot_value(
132 $meta_instance,
0ab65f99 133 $instance,
0ab65f99 134 $instance->$builder,
0ab65f99 135 );
b7bdffc3 136 }
137 else {
b3fa93c7 138 confess(ref($instance)." does not support builder method '". $self->{'builder'} ."' for attribute '" . $self->name . "'");
8fe581e5 139 }
1d68af04 140 }
bd4e03f9 141}
142
8ee74136 143sub _set_initial_slot_value {
144 my ($self, $meta_instance, $instance, $value) = @_;
145
146 my $slot_name = $self->name;
147
148 return $meta_instance->set_slot_value($instance, $slot_name, $value)
149 unless $self->has_initializer;
150
151 my $callback = sub {
152 $meta_instance->set_slot_value($instance, $slot_name, $_[0]);
153 };
154
155 my $initializer = $self->initializer;
156
157 # most things will just want to set a value, so make it first arg
158 $instance->$initializer($value, $callback, $self);
159}
160
d14f6cbe 161sub get_read_method {
162 my $self = shift;
163 my $reader = $self->reader || $self->accessor;
164 # normal case ...
165 return $reader unless ref $reader;
166 # the HASH ref case
167 my ($name) = %$reader;
168 return $name;
169}
170
171sub get_write_method {
172 my $self = shift;
173 my $writer = $self->writer || $self->accessor;
174 # normal case ...
175 return $writer unless ref $writer;
176 # the HASH ref case
177 my ($name) = %$writer;
178 return $name;
179}
b25109b1 180
5da16d1b 181sub get_read_method_ref {
182 my $self = shift;
742fb371 183 if ((my $reader = $self->get_read_method) && $self->associated_class) {
5da16d1b 184 return $self->associated_class->get_method($reader);
185 }
186 else {
def5c0b5 187 my $code = sub { $self->get_value(@_) };
188 if (my $class = $self->associated_class) {
189 return $class->method_metaclass->wrap(
190 $code,
191 package_name => $class->name,
192 name => '__ANON__'
193 );
194 }
195 else {
196 return $code;
197 }
5da16d1b 198 }
199}
200
201sub get_write_method_ref {
202 my $self = shift;
d14f6cbe 203 if ((my $writer = $self->get_write_method) && $self->associated_class) {
742fb371 204 return $self->associated_class->get_method($writer);
5da16d1b 205 }
206 else {
def5c0b5 207 my $code = sub { $self->set_value(@_) };
208 if (my $class = $self->associated_class) {
209 return $class->method_metaclass->wrap(
210 $code,
211 package_name => $class->name,
212 name => '__ANON__'
213 );
214 }
215 else {
216 return $code;
217 }
5da16d1b 218 }
219}
220
c57c8b10 221# slots
222
223sub slots { (shift)->name }
224
1d68af04 225# class association
727919c5 226
9ec169fe 227sub attach_to_class {
228 my ($self, $class) = @_;
229 (blessed($class) && $class->isa('Class::MOP::Class'))
230 || confess "You must pass a Class::MOP::Class instance (or a subclass)";
8683db0e 231 weaken($self->{'associated_class'} = $class);
9ec169fe 232}
233
234sub detach_from_class {
235 my $self = shift;
8683db0e 236 $self->{'associated_class'} = undef;
9ec169fe 237}
238
1d68af04 239# method association
3545c727 240
241sub associate_method {
242 my ($self, $method) = @_;
8683db0e 243 push @{$self->{'associated_methods'}} => $method;
3545c727 244}
245
16e960bd 246## Slot management
247
ef91a0e2 248sub set_initial_value {
249 my ($self, $instance, $value) = @_;
e76b01fb 250 $self->_set_initial_slot_value(
b3fa93c7 251 Class::MOP::Class->initialize(ref($instance))->get_meta_instance,
8ee74136 252 $instance,
253 $value
254 );
ef91a0e2 255}
256
7e5efe15 257sub set_value { shift->set_raw_value(@_) }
258sub get_value { shift->get_raw_value(@_) }
259
260sub set_raw_value {
1396f86b 261 my ($self, $instance, $value) = @_;
16e960bd 262
b3fa93c7 263 Class::MOP::Class->initialize(ref($instance))
da34f054 264 ->get_meta_instance
265 ->set_slot_value($instance, $self->name, $value);
16e960bd 266}
267
7e5efe15 268sub get_raw_value {
1396f86b 269 my ($self, $instance) = @_;
16e960bd 270
b3fa93c7 271 Class::MOP::Class->initialize(ref($instance))
da34f054 272 ->get_meta_instance
273 ->get_slot_value($instance, $self->name);
16e960bd 274}
275
3545c727 276sub has_value {
277 my ($self, $instance) = @_;
1d68af04 278
b3fa93c7 279 Class::MOP::Class->initialize(ref($instance))
da34f054 280 ->get_meta_instance
281 ->is_slot_initialized($instance, $self->name);
3545c727 282}
283
284sub clear_value {
285 my ($self, $instance) = @_;
1d68af04 286
b3fa93c7 287 Class::MOP::Class->initialize(ref($instance))
da34f054 288 ->get_meta_instance
289 ->deinitialize_slot($instance, $self->name);
3545c727 290}
291
ba38bf08 292## load em up ...
c0cbf4d9 293
ba38bf08 294sub accessor_metaclass { 'Class::MOP::Method::Accessor' }
c0cbf4d9 295
45a183fb 296sub _process_accessors {
c0cbf4d9 297 my ($self, $type, $accessor, $generate_as_inline_methods) = @_;
d9d99689 298
299 my $method_ctx;
300
301 if ( my $ctx = $self->definition_context ) {
302 $method_ctx = { %$ctx };
303 }
304
9b522fc4 305 if (ref($accessor)) {
306 (ref($accessor) eq 'HASH')
7d28758b 307 || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
4d47b77f 308 my ($name, $method) = %{$accessor};
4c105333 309 $method = $self->accessor_metaclass->wrap(
310 $method,
311 package_name => $self->associated_class->name,
312 name => $name,
d9d99689 313 definition_context => $method_ctx,
4c105333 314 );
3545c727 315 $self->associate_method($method);
1d68af04 316 return ($name, $method);
2eb717d5 317 }
9ec169fe 318 else {
1d68af04 319 my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
ba38bf08 320 my $method;
a8344505 321 try {
d9d99689 322 if ( $method_ctx ) {
323 my $desc = "accessor $accessor";
324 if ( $accessor ne $self->name ) {
325 $desc .= " of attribute " . $self->name;
326 }
327
328 $method_ctx->{description} = $desc;
329 }
330
ba38bf08 331 $method = $self->accessor_metaclass->new(
332 attribute => $self,
d90b42a6 333 is_inline => $inline_me,
ba38bf08 334 accessor_type => $type,
4c105333 335 package_name => $self->associated_class->name,
336 name => $accessor,
d9d99689 337 definition_context => $method_ctx,
1d68af04 338 );
a8344505 339 }
340 catch {
341 confess "Could not create the '$type' method for " . $self->name . " because : $_";
ba38bf08 342 };
3545c727 343 $self->associate_method($method);
ba38bf08 344 return ($accessor, $method);
1d68af04 345 }
9ec169fe 346}
347
348sub install_accessors {
c0cbf4d9 349 my $self = shift;
350 my $inline = shift;
351 my $class = $self->associated_class;
1d68af04 352
9ec169fe 353 $class->add_method(
45a183fb 354 $self->_process_accessors('accessor' => $self->accessor(), $inline)
9ec169fe 355 ) if $self->has_accessor();
356
1d68af04 357 $class->add_method(
45a183fb 358 $self->_process_accessors('reader' => $self->reader(), $inline)
9ec169fe 359 ) if $self->has_reader();
360
361 $class->add_method(
45a183fb 362 $self->_process_accessors('writer' => $self->writer(), $inline)
9ec169fe 363 ) if $self->has_writer();
364
365 $class->add_method(
45a183fb 366 $self->_process_accessors('predicate' => $self->predicate(), $inline)
9ec169fe 367 ) if $self->has_predicate();
1d68af04 368
7d28758b 369 $class->add_method(
45a183fb 370 $self->_process_accessors('clearer' => $self->clearer(), $inline)
7d28758b 371 ) if $self->has_clearer();
1d68af04 372
9ec169fe 373 return;
2eb717d5 374}
375
b51af7f9 376{
377 my $_remove_accessor = sub {
378 my ($accessor, $class) = @_;
9b522fc4 379 if (ref($accessor) && ref($accessor) eq 'HASH') {
c50c603e 380 ($accessor) = keys %{$accessor};
1d68af04 381 }
382 my $method = $class->get_method($accessor);
383 $class->remove_method($accessor)
b3fa93c7 384 if (ref($method) && $method->isa('Class::MOP::Method::Accessor'));
b51af7f9 385 };
1d68af04 386
b51af7f9 387 sub remove_accessors {
9ec169fe 388 my $self = shift;
2367814a 389 # TODO:
1d68af04 390 # we really need to make sure to remove from the
391 # associates methods here as well. But this is
392 # such a slimly used method, I am not worried
2367814a 393 # about it right now.
9ec169fe 394 $_remove_accessor->($self->accessor(), $self->associated_class()) if $self->has_accessor();
395 $_remove_accessor->($self->reader(), $self->associated_class()) if $self->has_reader();
396 $_remove_accessor->($self->writer(), $self->associated_class()) if $self->has_writer();
397 $_remove_accessor->($self->predicate(), $self->associated_class()) if $self->has_predicate();
7d28758b 398 $_remove_accessor->($self->clearer(), $self->associated_class()) if $self->has_clearer();
1d68af04 399 return;
b51af7f9 400 }
401
8b978dd5 402}
403
03a3092d 404sub inline_get {
405 my $self = shift;
406 my ($instance) = @_;
407
408 return $self->associated_class->get_meta_instance->inline_get_slot_value(
409 $instance, $self->name );
410}
411
412sub inline_set {
413 my $self = shift;
414 my ( $instance, $value ) = @_;
415
416 return $self->associated_class->get_meta_instance->inline_set_slot_value(
417 $instance, $self->name, $value );
418}
419
420sub inline_has {
421 my $self = shift;
422 my ($instance) = @_;
423
424 return
425 $self->associated_class->get_meta_instance
426 ->inline_is_slot_initialized( $instance, $self->name );
427}
428
429sub inline_clear {
430 my $self = shift;
431 my ($instance) = @_;
432
433 return
434 $self->associated_class->get_meta_instance
435 ->inline_deinitialize_slot( $instance, $self->name );
436}
437
8b978dd5 4381;
439
440__END__
441
442=pod
443
1d68af04 444=head1 NAME
8b978dd5 445
446Class::MOP::Attribute - Attribute Meta Object
447
448=head1 SYNOPSIS
1d68af04 449
2e23f7dc 450 Class::MOP::Attribute->new(
451 foo => (
452 accessor => 'foo', # dual purpose get/set accessor
453 predicate => 'has_foo', # predicate check for defined-ness
454 init_arg => '-foo', # class->new will look for a -foo key
455 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
456 )
457 );
458
459 Class::MOP::Attribute->new(
460 bar => (
461 reader => 'bar', # getter
462 writer => 'set_bar', # setter
463 predicate => 'has_bar', # predicate check for defined-ness
464 init_arg => ':bar', # class->new will look for a :bar key
465 # no default value means it is undef
466 )
467 );
8b978dd5 468
469=head1 DESCRIPTION
470
2e23f7dc 471The Attribute Protocol is almost entirely an invention of
472C<Class::MOP>. Perl 5 does not have a consistent notion of
473attributes. There are so many ways in which this is done, and very few
474(if any) are easily discoverable by this module.
552e3d24 475
2e23f7dc 476With that said, this module attempts to inject some order into this
1d68af04 477chaos, by introducing a consistent API which can be used to create
fe122940 478object attributes.
552e3d24 479
480=head1 METHODS
481
482=head2 Creation
483
484=over 4
485
2e23f7dc 486=item B<< Class::MOP::Attribute->new($name, ?%options) >>
fe122940 487
1d68af04 488An attribute must (at the very least), have a C<$name>. All other
2e23f7dc 489C<%options> are added as key-value pairs.
fe122940 490
2e23f7dc 491=over 8
fe122940 492
76187047 493=item * init_arg
fe122940 494
2e23f7dc 495This is a string value representing the expected key in an
496initialization hash. For instance, if we have an C<init_arg> value of
497C<-foo>, then the following code will Just Work.
fe122940 498
d69fb6b3 499 MyClass->meta->new_object( -foo => 'Hello There' );
fe122940 500
2e23f7dc 501If an init_arg is not assigned, it will automatically use the
502attribute's name. If C<init_arg> is explicitly set to C<undef>, the
503attribute cannot be specified during initialization.
7b31baf4 504
76187047 505=item * builder
1d68af04 506
2e23f7dc 507This provides the name of a method that will be called to initialize
508the attribute. This method will be called on the object after it is
509constructed. It is expected to return a valid value for the attribute.
fe122940 510
76187047 511=item * default
4c4a6c41 512
2e23f7dc 513This can be used to provide an explicit default for initializing the
514attribute. If the default you provide is a subroutine reference, then
515this reference will be called I<as a method> on the object.
4c4a6c41 516
2e23f7dc 517If the value is a simple scalar (string or number), then it can be
518just passed as is. However, if you wish to initialize it with a HASH
519or ARRAY ref, then you need to wrap that inside a subroutine
520reference:
fe122940 521
2e23f7dc 522 Class::MOP::Attribute->new(
523 'foo' => (
524 default => sub { [] },
525 )
526 );
1d68af04 527
528 # or ...
529
2e23f7dc 530 Class::MOP::Attribute->new(
531 'foo' => (
532 default => sub { {} },
533 )
534 );
535
536If you wish to initialize an attribute with a subroutine reference
537itself, then you need to wrap that in a subroutine as well:
538
539 Class::MOP::Attribute->new(
540 'foo' => (
541 default => sub {
542 sub { print "Hello World" }
543 },
544 )
545 );
546
547And lastly, if the value of your attribute is dependent upon some
548other aspect of the instance structure, then you can take advantage of
549the fact that when the C<default> value is called as a method:
550
551 Class::MOP::Attribute->new(
552 'object_identity' => (
553 default => sub { Scalar::Util::refaddr( $_[0] ) },
554 )
555 );
556
557Note that there is no guarantee that attributes are initialized in any
558particular order, so you cannot rely on the value of some other
559attribute when generating the default.
fe122940 560
76187047 561=item * initializer
0ef07b33 562
2e23f7dc 563This option can be either a method name or a subroutine
564reference. This method will be called when setting the attribute's
565value in the constructor. Unlike C<default> and C<builder>, the
566initializer is only called when a value is provided to the
567constructor. The initializer allows you to munge this value during
568object construction.
569
570The initializer is called as a method with three arguments. The first
571is the value that was passed to the constructor. The second is a
572subroutine reference that can be called to actually set the
573attribute's value, and the last is the associated
574C<Class::MOP::Attribute> object.
575
576This contrived example shows an initializer that sets the attribute to
577twice the given value.
578
579 Class::MOP::Attribute->new(
580 'doubled' => (
581 initializer => sub {
ea62c8ab 582 my ( $self, $value, $set, $attr ) = @_;
2e23f7dc 583 $set->( $value * 2 );
584 },
585 )
586 );
587
588Since an initializer can be a method name, you can easily make
0ef07b33 589attribute initialization use the writer:
590
2e23f7dc 591 Class::MOP::Attribute->new(
592 'some_attr' => (
593 writer => 'some_attr',
594 initializer => 'some_attr',
595 )
596 );
0ef07b33 597
2e23f7dc 598Your writer will need to examine C<@_> and determine under which
599context it is being called.
127d39a7 600
fe122940 601=back
602
2e23f7dc 603The C<accessor>, C<reader>, C<writer>, C<predicate> and C<clearer>
604options all accept the same parameters. You can provide the name of
605the method, in which case an appropriate default method will be
606generated for you. Or instead you can also provide hash reference
607containing exactly one key (the method name) and one value. The value
608should be a subroutine reference, which will be installed as the
609method itself.
59e7697f 610
76187047 611=over 8
59e7697f 612
76187047 613=item * accessor
59e7697f 614
2e23f7dc 615An C<accessor> is a standard Perl-style read/write accessor. It will
616return the value of the attribute, and if a value is passed as an
617argument, it will assign that value to the attribute.
fe122940 618
2e23f7dc 619Note that C<undef> is a legitimate value, so this will work:
fe122940 620
621 $object->set_something(undef);
622
76187047 623=item * reader
59e7697f 624
2e23f7dc 625This is a basic read-only accessor. It returns the value of the
626attribute.
fe122940 627
76187047 628=item * writer
59e7697f 629
1d68af04 630This is a basic write accessor, it accepts a single argument, and
2e23f7dc 631assigns that value to the attribute.
59e7697f 632
2e23f7dc 633Note that C<undef> is a legitimate value, so this will work:
59e7697f 634
2e23f7dc 635 $object->set_something(undef);
fe122940 636
76187047 637=item * predicate
fe122940 638
2e23f7dc 639The predicate method returns a boolean indicating whether or not the
640attribute has been explicitly set.
07dca7e3 641
2e23f7dc 642Note that the predicate returns true even if the attribute was set to
643a false value (C<0> or C<undef>).
07dca7e3 644
76187047 645=item * clearer
7d28758b 646
2e23f7dc 647This method will uninitialize the attribute. After an attribute is
648cleared, its C<predicate> will return false.
7d28758b 649
76187047 650=item * definition_context
f8813817 651
652Mostly, this exists as a hook for the benefit of Moose.
653
654This option should be a hash reference containing several keys which
655will be used when inlining the attribute's accessors. The keys should
656include C<line>, the line number where the attribute was created, and
657either C<file> or C<description>.
658
659This information will ultimately be used when eval'ing inlined
660accessor code so that error messages report a useful line and file
661name.
662
59e7697f 663=back
552e3d24 664
2e23f7dc 665=item B<< $attr->clone(%options) >>
bd4e03f9 666
2e23f7dc 667This clones the attribute. Any options you provide will override the
668settings of the original attribute. You can change the name of the new
669attribute by passing a C<name> key in C<%options>.
127d39a7 670
2e23f7dc 671=back
bd4e03f9 672
2e23f7dc 673=head2 Informational
127d39a7 674
2e23f7dc 675These are all basic read-only accessors for the values passed into
676the constructor.
552e3d24 677
2e23f7dc 678=over 4
16e960bd 679
2e23f7dc 680=item B<< $attr->name >>
2367814a 681
76187047 682Returns the attribute's name.
683
2e23f7dc 684=item B<< $attr->accessor >>
2367814a 685
2e23f7dc 686=item B<< $attr->reader >>
16e960bd 687
2e23f7dc 688=item B<< $attr->writer >>
16e960bd 689
2e23f7dc 690=item B<< $attr->predicate >>
16e960bd 691
2e23f7dc 692=item B<< $attr->clearer >>
c0921932 693
2e23f7dc 694The C<accessor>, C<reader>, C<writer>, C<predicate>, and C<clearer>
695methods all return exactly what was passed to the constructor, so it
a6710c60 696can be either a string containing a method name, or a hash reference.
c0921932 697
2e23f7dc 698=item B<< $attr->initializer >>
16e960bd 699
a6710c60 700Returns the initializer as passed to the constructor, so this may be
2e23f7dc 701either a method name or a subroutine reference.
16e960bd 702
2e23f7dc 703=item B<< $attr->init_arg >>
3545c727 704
2e23f7dc 705=item B<< $attr->is_default_a_coderef >>
2367814a 706
2e23f7dc 707=item B<< $attr->default($instance) >>
3545c727 708
2e23f7dc 709The C<$instance> argument is optional. If you don't pass it, the
710return value for this method is exactly what was passed to the
711constructor, either a simple scalar or a subroutine reference.
2367814a 712
2e23f7dc 713If you I<do> pass an C<$instance> and the default is a subroutine
714reference, then the reference is called as a method on the
715C<$instance> and the generated value is returned.
16e960bd 716
2e23f7dc 717=item B<< $attr->slots >>
552e3d24 718
2e23f7dc 719Return a list of slots required by the attribute. This is usually just
720one, the name of the attribute.
fe122940 721
2e23f7dc 722A slot is the name of the hash key used to store the attribute in an
723object instance.
552e3d24 724
2e23f7dc 725=item B<< $attr->get_read_method >>
552e3d24 726
2e23f7dc 727=item B<< $attr->get_write_method >>
552e3d24 728
2e23f7dc 729Returns the name of a method suitable for reading or writing the value
730of the attribute in the associated class.
552e3d24 731
2e23f7dc 732If an attribute is read- or write-only, then these methods can return
733C<undef> as appropriate.
552e3d24 734
2e23f7dc 735=item B<< $attr->has_read_method >>
c50c603e 736
2e23f7dc 737=item B<< $attr->has_write_method >>
7d28758b 738
2e23f7dc 739This returns a boolean indicating whether the attribute has a I<named>
740read or write method.
0ab65f99 741
2e23f7dc 742=item B<< $attr->get_read_method_ref >>
552e3d24 743
2e23f7dc 744=item B<< $attr->get_write_method_ref >>
495af518 745
2e23f7dc 746Returns the subroutine reference of a method suitable for reading or
747writing the attribute's value in the associated class. These methods
748always return a subroutine reference, regardless of whether or not the
749attribute is read- or write-only.
750
eeff7496 751=item B<< $attr->insertion_order >>
752
753If this attribute has been inserted into a class, this returns a zero
754based index regarding the order of insertion.
755
2e23f7dc 756=back
fe122940 757
2e23f7dc 758=head2 Informational predicates
92d2abfa 759
2e23f7dc 760These are all basic predicate methods for the values passed into C<new>.
552e3d24 761
2e23f7dc 762=over 4
c57c8b10 763
2e23f7dc 764=item B<< $attr->has_accessor >>
c57c8b10 765
2e23f7dc 766=item B<< $attr->has_reader >>
b25109b1 767
2e23f7dc 768=item B<< $attr->has_writer >>
b25109b1 769
2e23f7dc 770=item B<< $attr->has_predicate >>
5da16d1b 771
2e23f7dc 772=item B<< $attr->has_clearer >>
5da16d1b 773
2e23f7dc 774=item B<< $attr->has_initializer >>
5da16d1b 775
2e23f7dc 776=item B<< $attr->has_init_arg >>
5da16d1b 777
2e23f7dc 778This will be I<false> if the C<init_arg> was set to C<undef>.
b25109b1 779
2e23f7dc 780=item B<< $attr->has_default >>
9e517e01 781
2e23f7dc 782This will be I<false> if the C<default> was set to C<undef>, since
783C<undef> is the default C<default> anyway.
9e517e01 784
2e23f7dc 785=item B<< $attr->has_builder >>
9e517e01 786
eeff7496 787=item B<< $attr->has_insertion_order >>
788
789This will be I<false> if this attribute has not be inserted into a class
790
552e3d24 791=back
792
2e23f7dc 793=head2 Value management
552e3d24 794
a6710c60 795These methods are basically "back doors" to the instance, and can be
2e23f7dc 796used to bypass the regular accessors, but still stay within the MOP.
797
798These methods are not for general use, and should only be used if you
799really know what you are doing.
fe122940 800
552e3d24 801=over 4
802
2e23f7dc 803=item B<< $attr->initialize_instance_slot($meta_instance, $instance, $params) >>
804
805This method is used internally to initialize the attribute's slot in
806the object C<$instance>.
807
808The C<$params> is a hash reference of the values passed to the object
809constructor.
810
811It's unlikely that you'll need to call this method yourself.
552e3d24 812
2e23f7dc 813=item B<< $attr->set_value($instance, $value) >>
552e3d24 814
2e23f7dc 815Sets the value without going through the accessor. Note that this
816works even with read-only attributes.
552e3d24 817
7e5efe15 818=item B<< $attr->set_raw_value($instance, $value) >>
819
820Sets the value with no side effects such as a trigger.
821
822This doesn't actually apply to Class::MOP attributes, only to subclasses.
823
2e23f7dc 824=item B<< $attr->set_initial_value($instance, $value) >>
c50c603e 825
2e23f7dc 826Sets the value without going through the accessor. This method is only
827called when the instance is first being initialized.
7d28758b 828
2e23f7dc 829=item B<< $attr->get_value($instance) >>
0ab65f99 830
2e23f7dc 831Returns the value without going through the accessor. Note that this
832works even with write-only accessors.
552e3d24 833
25b3fdad 834=item B<< $attr->get_raw_value($instance) >>
7e5efe15 835
836Returns the value without any side effects such as lazy attributes.
837
838Doesn't actually apply to Class::MOP attributes, only to subclasses.
839
2e23f7dc 840=item B<< $attr->has_value($instance) >>
552e3d24 841
2e23f7dc 842Return a boolean indicating whether the attribute has been set in
843C<$instance>. This how the default C<predicate> method works.
844
845=item B<< $attr->clear_value($instance) >>
846
847This will clear the attribute's value in C<$instance>. This is what
848the default C<clearer> calls.
849
850Note that this works even if the attribute does not have any
851associated read, write or clear methods.
bf731086 852
552e3d24 853=back
854
9ec169fe 855=head2 Class association
856
1d68af04 857These methods allow you to manage the attributes association with
858the class that contains it. These methods should not be used
2367814a 859lightly, nor are they very magical, they are mostly used internally
860and by metaclass instances.
861
9ec169fe 862=over 4
863
2e23f7dc 864=item B<< $attr->associated_class >>
865
866This returns the C<Class::MOP::Class> with which this attribute is
867associated, if any.
868
869=item B<< $attr->attach_to_class($metaclass) >>
9ec169fe 870
2e23f7dc 871This method stores a weakened reference to the C<$metaclass> object
872internally.
2367814a 873
2e23f7dc 874This method does not remove the attribute from its old class,
875nor does it create any accessors in the new class.
9ec169fe 876
2e23f7dc 877It is probably best to use the L<Class::MOP::Class> C<add_attribute>
878method instead.
2367814a 879
2e23f7dc 880=item B<< $attr->detach_from_class >>
9ec169fe 881
2e23f7dc 882This method removes the associate metaclass object from the attribute
883it has one.
884
885This method does not remove the attribute itself from the class, or
886remove its accessors.
887
888It is probably best to use the L<Class::MOP::Class>
889C<remove_attribute> method instead.
2367814a 890
9ec169fe 891=back
892
552e3d24 893=head2 Attribute Accessor generation
894
895=over 4
896
2e23f7dc 897=item B<< $attr->accessor_metaclass >>
ba38bf08 898
2e23f7dc 899Accessor methods are generated using an accessor metaclass. By
900default, this is L<Class::MOP::Method::Accessor>. This method returns
2367814a 901the name of the accessor metaclass that this attribute uses.
902
2e23f7dc 903=item B<< $attr->associate_method($method) >>
2367814a 904
2e23f7dc 905This associates a L<Class::MOP::Method> object with the
906attribute. Typically, this is called internally when an attribute
907generates its accessors.
3545c727 908
2e23f7dc 909=item B<< $attr->associated_methods >>
3545c727 910
2e23f7dc 911This returns the list of methods which have been associated with the
912attribute.
2367814a 913
2e23f7dc 914=item B<< $attr->install_accessors >>
2eb717d5 915
2e23f7dc 916This method generates and installs code the attributes various
917accessors. It is typically called from the L<Class::MOP::Class>
918C<add_attribute> method.
2eb717d5 919
2e23f7dc 920=item B<< $attr->remove_accessors >>
2eb717d5 921
2e23f7dc 922This method removes all of the accessors associated with the
923attribute.
2eb717d5 924
2e23f7dc 925This does not currently remove methods from the list returned by
926C<associated_methods>.
2367814a 927
03a3092d 928=item B<< $attr->inline_get >>
929
930=item B<< $attr->inline_set >>
931
932=item B<< $attr->inline_has >>
933
934=item B<< $attr->inline_clear >>
935
936These methods return a code snippet suitable for inlining the relevant
937operation. They expect strings containing variable names to be used in the
938inlining, like C<'$self'> or C<'$_[1]'>.
939
2eb717d5 940=back
941
942=head2 Introspection
943
944=over 4
552e3d24 945
45b4c423 946=item B<< Class::MOP::Attribute->meta >>
552e3d24 947
2e23f7dc 948This will return a L<Class::MOP::Class> instance for this class.
fe122940 949
2e23f7dc 950It should also be noted that L<Class::MOP> will actually bootstrap
951this module by installing a number of attribute meta-objects into its
952metaclass.
fe122940 953
552e3d24 954=back
955
1a09d9cc 956=head1 AUTHORS
8b978dd5 957
a2e85e6c 958Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 959
960=head1 COPYRIGHT AND LICENSE
961
3e2c8600 962Copyright 2006-2010 by Infinity Interactive, Inc.
8b978dd5 963
964L<http://www.iinteractive.com>
965
966This library is free software; you can redistribute it and/or modify
1d68af04 967it under the same terms as Perl itself.
8b978dd5 968
16e960bd 969=cut
970
7d28758b 971