2 package Class::MOP::Attribute;
16 (defined $name && $name ne '')
17 || confess "You must provide a name for the attribute";
21 accessor => $options{accessor},
22 reader => $options{reader},
23 writer => $options{writer},
24 init_arg => $options{init_arg},
25 default => $options{default}
29 sub name { (shift)->{name} }
31 sub has_accessor { (shift)->{accessor} ? 1 : 0 }
32 sub accessor { (shift)->{accessor} }
34 sub has_reader { (shift)->{reader} ? 1 : 0 }
35 sub reader { (shift)->{reader} }
37 sub has_writer { (shift)->{writer} ? 1 : 0 }
38 sub writer { (shift)->{writer} }
40 sub has_init_arg { (shift)->{init_arg} ? 1 : 0 }
41 sub init_arg { (shift)->{init_arg} }
43 sub has_default { (shift)->{default} ? 1 : 0 }
44 sub default { (shift)->{default} }
46 sub generate_accessor {
59 Class::MOP::Attribute - Attribute Meta Object
63 Class::MOP::Attribute->new('$foo' => (
64 accessor => 'foo', # dual purpose get/set accessor
65 init_arg => '-foo', # class->new will look for a -foo key
66 default => 'BAR IS BAZ!' # if no -foo key is provided, use this
69 Class::MOP::Attribute->new('$.bar' => (
70 reader => 'bar', # getter
71 writer => 'set_bar', # setter
72 init_arg => '-bar', # class->new will look for a -bar key
73 # no default value means it is undef
78 The Attribute Protocol is almost entirely an invention of this module. This is
79 because Perl 5 does not have consistent notion of what is an attribute
80 of a class. There are so many ways in which this is done, and very few
81 (if any) are discoverable by this module.
83 So, all that said, this module attempts to inject some order into this
84 chaos, by introducing a more consistent approach.
92 =item B<new ($name, %accessor_description, $class_initialization_arg, $default_value)>
114 =head2 Informational predicates
118 =item B<has_accessor>
120 Returns true if this attribute uses a get/set accessor, and false
125 Returns true if this attribute has a reader, and false otherwise
129 Returns true if this attribute has a writer, and false otherwise
131 =item B<has_init_arg>
133 Returns true if this attribute has a class intialization argument, and
138 Returns true if this attribute has a default value, and false
143 =head2 Attribute Accessor generation
147 =item B<generate_accessor>
149 This allows the attribute to generate code for it's own accessor
150 methods. This is mostly part of an internal protocol between the class
151 and it's own attributes, see the C<create_all_accessors> method above.
157 Stevan Little E<gt>stevan@iinteractive.comE<lt>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2006 by Infinity Interactive, Inc.
163 L<http://www.iinteractive.com>
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself.